Page 1 of 1

Strange behaviour of format

Posted: Sat Jul 02, 2011 6:56 am
by kandre
Hello all,
This is my first post on this forum so please be gentle :)
Format is doing my head in:
CL-USER> (format nil "~f" 5321.6802)
"5321.68"
CL-USER> (format nil "~,3f" 5321.6802)
"5321.680"
CL-USER> (format nil "~,4f" 5321.6802)
"5321.6800"
CL-USER> (format nil "~,5f" 5321.6802)
"5321.68000"

Where did my 2 go?

Now if I am doing this:
(format nil "~,6f" 5321.6805)
"5321.680700"

What am I missing?
Thanks in advance for your help.
Kind Regards
Andreas

Re: Strange behaviour of format

Posted: Sat Jul 02, 2011 9:43 am
by ramarren
By default the Common Lisp reader reads floating point numerals at single precision. This is controlled by special variable *read-default-float-format*. Single floats are not sufficient to accurately represent the numbers in your examples and hence approximations are used. Use double floats with 0d0 syntax or rational numbers with 1/2 syntax for increased precision. Although do note that FORMAT is specified to convert rationals to single floats for floating point output.