Subject: Re: (* 2.4 3) => 7.2000003 WTF?! Let's Fix Lisp! Noob Programming Challenge
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 20 Dec 2006 18:57:45 -0600
Newsgroups: comp.lang.lisp
Message-ID: <G6udnRdy7pmUQRTYnZ2dnUVZ_uuqnZ2d@speakeasy.net>
Ron Garret  <rNOSPAMon@flownet.com> wrote:
+---------------
| Frode Vatvedt Fjeld <frodef@cs.uit.no> wrote:
| > Ron Garret <rNOSPAMon@flownet.com> writes:
| > > Because people get confused when they see $199/50 instead of $3.98.
| > 
| > But isn't the presentation of something is separate from its internal
| > representation? If you present something using format and ~$, as would
| > be natural for dollar amounts, it doesn't matter if the representation
| > is float or rational. (format t "~$" 199/50) prints 3.98.
| 
| Yes, I didn't know about the ~$ format directive.
| I'm still pondering the ramifications of allowing e.g. 1/3 of a dollar.
+---------------

The precision of ~$ only *defaults* to 2; you can specify another:

    (format nil "~$" 8/3)     ==> "2.67"
    (format nil "~1$" 8/3)    ==> "2.7"
    (format nil "~4$" 8/3)    ==> "2.6667"

Small "gotcha" to watch for: The ~$ format will [or, is allowed to]
coerce rationals to *single* floats, e.g.:

    (format nil "~20$" 8/3)   ==> "2.66666670000000000000"
    (format nil "~20$" (coerce 8/3 'double-float))
			      ==> "2.66666666666666650000"


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607