
scientific numbers to string without scientific notation
scientific numbers to string without scientific notation
im trying to convert a scientific number, say 4.3e7, to the string "43000000" using (format nil "~A" 4.3e7). Is there any way to prevent format from using scientific notation in its string conversion? Similarly ive tried something like (format nil "~f" 29029987) but i get weird results like "29029988.0", which is clearly not the original number 

-
- Posts: 61
- Joined: Mon Jul 07, 2008 8:06 pm
- Location: Toowoomba, Queensland, Australia
- Contact:
Re: scientific numbers to string without scientific notation
For big numbers, you could look up Section 22.3.2 "FORMAT Radix Control" of the Hyperspec, and then go to the decimal format directive: "~D". You need to give it an integer, so you could probably use ROUND to get an integer. If you want to print the decimal part, you'd be able to use ROUND with a multiple-value-bind form, but that seems a bit more complex...
Re: scientific numbers to string without scientific notation
~D still keeps scientific notation and while round gets rid of scientific notation it gives an incorrect result. This really puzzles me, i thought lisp had bignum math, what is up with this. (round 2.5e10) =
24999999488
0.0
I tried with ecl and sbcl and they both give the same answer! huh?
24999999488
0.0

I tried with ecl and sbcl and they both give the same answer! huh?
Re: scientific numbers to string without scientific notation
The joys of unaccurate results - ok, so figured it out, it appears common lisp remains the true identity of the number internally somehow, but i need to coerce it into a double float or (setq *READ-DEFAULT-FLOAT-FORMAT* 'double-float) to get it automatically. then the ~f option works just fine and the ~D for integers
Re: scientific numbers to string without scientific notation
What Every Computer Scientist Should Know About Floating-Point Arithmetic. The number read from string "2.5e10" by the lisp reader is a floating point number, and usually a single float at that. The number you get from round is an integer closest to a number that can be represented as floating point.
I don't think it is possible to make the reader parse scientific notation to integers, since this is not a very common case. Either express those numbers differently, or parse them manually.
I don't think it is possible to make the reader parse scientific notation to integers, since this is not a very common case. Either express those numbers differently, or parse them manually.
Re: scientific numbers to string without scientific notation
Thanks! Fortunately i didn't really need to parse a number from a string as non-scientific just the opposite, and coercing it to double-float and using ~f did the trick, at least for the size of numbers i was dealing with. yay!