This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Strange behaviour of format

2 posts · 971 views

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

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.