scientific numbers to string without scientific notation

Discussion of Common Lisp
Post Reply
Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

scientific numbers to string without scientific notation

Post by Suroy » Sun Jun 27, 2010 9:13 pm

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 :?

Kohath
Posts: 61
Joined: Mon Jul 07, 2008 8:06 pm
Location: Toowoomba, Queensland, Australia
Contact:

Re: scientific numbers to string without scientific notation

Post by Kohath » Sun Jun 27, 2010 10:43 pm

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...

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: scientific numbers to string without scientific notation

Post by Suroy » Mon Jun 28, 2010 5:55 am

~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?

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: scientific numbers to string without scientific notation

Post by Suroy » Mon Jun 28, 2010 6:10 am

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

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: scientific numbers to string without scientific notation

Post by ramarren » Mon Jun 28, 2010 6:10 am

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.

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: scientific numbers to string without scientific notation

Post by Suroy » Mon Jun 28, 2010 9:29 pm

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!

Post Reply