Strange behaviour of format

Discussion of Common Lisp

Strange behaviour of format

Postby kandre » Sat Jul 02, 2011 6:56 am

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)
CL-USER> (format nil "~,3f" 5321.6802)
CL-USER> (format nil "~,4f" 5321.6802)
CL-USER> (format nil "~,5f" 5321.6802)

Where did my 2 go?

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

What am I missing?
Thanks in advance for your help.
Kind Regards
Posts: 1
Joined: Sat Jul 02, 2011 6:52 am

Re: Strange behaviour of format

Postby Ramarren » Sat Jul 02, 2011 9:43 am

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.
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 6 guests