From: bryane
Subject: format floats rationals question
Date: 
Message-ID: <1c3ee330-a440-4ec3-8428-251e1e98dbfd@h18g2000yqj.googlegroups.com>
With all of the other things format can do, anyone know why

(format nil "~:D" x)

doesn't handle thousands separators for floats and rationals?

E.g.
(format nil "~:D" 1000) returns "1,000"
(format nil "~:D" 1000.1) returns "1000.1" (same as (format nil "~D"
1000.1) format without colon)
(format nil "~:D" (rational 1000.1)) returns "8192819/8192"

I understand that is the spec, but why not have a format directive
that returns "1,000.1" for floats and rationals?

From: Robert Uhl
Subject: Re: format floats rationals question
Date: 
Message-ID: <m363ebaunu.fsf@latakia.octopodial-chrome.com>
bryane <···········@gmail.com> writes:

> With all of the other things format can do, anyone know why
>
> (format nil "~:D" x)
>
> doesn't handle thousands separators for floats and rationals?

Because ~D takes an integer; with a non-integer it defaults to ~A.

> I understand that is the spec, but why not have a format directive
> that returns "1,000.1" for floats and rationals?

I do not know; I suppose that the folks who wrote the standard didn't
think that it was useful.  I happen to disagree, but oddly enough they
didn't consult me *grin*  Perhaps Kent Pitman has some light to shed.

You could of course always do this:

         (let* ((float 1003423.32423)
		(integer (floor float))
		(decimal (- float integer)))
	   (format t "~:d~6,5f" integer decimal))

Note that the output is not 1,003,423.32423; this is due to floating
point's limited ability to represent numbers (I hadn't realised that it
was this bad though).  Perhaps this has something to do with why
commafication of floating point wasn't standardised--floating point
numbers might be too imprecise to normally use commas.  Or something.

-- 
Robert Uhl <http://public.xdi.org/=ruhl>
Old Russian Proverb: If you see a Bulgarian on the street, beat him;
                     he will know why.
From: bryane
Subject: Re: format floats rationals question
Date: 
Message-ID: <3c2c4257-db34-4263-b0c8-5854add8a1b0@2g2000prl.googlegroups.com>
On Jul 2, 9:58 am, Robert Uhl <·········@NOSPAMgmail.com> wrote:
> bryane <···········@gmail.com> writes:
> > With all of the other things format can do, anyone know why
>
> > (format nil "~:D" x)
>
> > doesn't handle thousands separators for floats and rationals?
>
> Because ~D takes an integer; with a non-integer it defaults to ~A.
>
> > I understand that is the spec, but why not have a format directive
> > that returns "1,000.1" for floats and rationals?
>
> I do not know; I suppose that the folks who wrote the standard didn't
> think that it was useful.  I happen to disagree, but oddly enough they
> didn't consult me *grin*  Perhaps Kent Pitman has some light to shed.
>
> You could of course always do this:
>
>          (let* ((float 1003423.32423)
>                 (integer (floor float))
>                 (decimal (- float integer)))
>            (format t "~:d~6,5f" integer decimal))
>
> Note that the output is not 1,003,423.32423; this is due to floating
> point's limited ability to represent numbers (I hadn't realised that it
> was this bad though).  Perhaps this has something to do with why
> commafication of floating point wasn't standardised--floating point
> numbers might be too imprecise to normally use commas.  Or something.
>
> --
> Robert Uhl <http://public.xdi.org/=ruhl>
> Old Russian Proverb: If you see a Bulgarian on the street, beat him;
>                      he will know why.

Loses precision pretty fast. Unfortunately, using rationals for more
precision doesn't seem to work either.
From: Pillsy
Subject: Re: format floats rationals question
Date: 
Message-ID: <0a61f9e0-fa68-42e9-a2bb-49fe8cc529fa@d4g2000yqa.googlegroups.com>
On Jul 2, 12:58 pm, Robert Uhl <·········@NOSPAMgmail.com> wrote:
[...]
> You could of course always do this:

>          (let* ((float 1003423.32423)
>                 (integer (floor float))
>                 (decimal (- float integer)))
>            (format t "~:d~6,5f" integer decimal))

This is a perfect place to use multiple return values:

(let ((float 1003423.32423))
  (multiple-value-bind (integer decimal) (truncate float)
    (format t "~:d~6,5f" integer (abs decimal))))

Using TRUNCATE and ABS instead of FLOOR means this works just as well
with negative numbers.

Regards,
Pillsy
From: Pascal J. Bourguignon
Subject: Re: format floats rationals question
Date: 
Message-ID: <87hbxuogo1.fsf@galatea.local>
Robert Uhl <·········@NOSPAMgmail.com> writes:

> bryane <···········@gmail.com> writes:
>
>> With all of the other things format can do, anyone know why
>>
>> (format nil "~:D" x)
>>
>> doesn't handle thousands separators for floats and rationals?
>
> Because ~D takes an integer; with a non-integer it defaults to ~A.
>
>> I understand that is the spec, but why not have a format directive
>> that returns "1,000.1" for floats and rationals?
>
> I do not know; I suppose that the folks who wrote the standard didn't
> think that it was useful.  I happen to disagree, but oddly enough they
> didn't consult me *grin*  Perhaps Kent Pitman has some light to shed.
>
> You could of course always do this:
>
>          (let* ((float 1003423.32423)
> 		(integer (floor float))
> 		(decimal (- float integer)))
> 	   (format t "~:d~6,5f" integer decimal))
>
> Note that the output is not 1,003,423.32423; this is due to floating
> point's limited ability to represent numbers (I hadn't realised that it
> was this bad though). 

32-bit IEEE floats have 23(+1) bits of mantissa, that's only 7
significant decimal digits.  So your integral part eats the whole
precision.

C/USER[13]> (let* ((float 3423.32423)
                   (integer (floor float))
                   (decimal (- float integer)))
              (format t "~:d~6,5f" integer decimal))

3,423.32422
NIL


> Perhaps this has something to do with why
> commafication of floating point wasn't standardised--floating point
> numbers might be too imprecise to normally use commas.  Or something.

Remains double-float and long-float.

-- 
__Pascal Bourguignon__
From: Thomas A. Russ
Subject: Re: format floats rationals question
Date: 
Message-ID: <ymiws6qd4ky.fsf@blackcat.isi.edu>
bryane <···········@gmail.com> writes:

> I understand that is the spec, but why not have a format directive
> that returns "1,000.1" for floats and rationals?

I guess it's an oversight.
Especially since the ~F format doesn't even use the : (colon) modifier.
So it would have been available.

~$ also doesn't do it (where it might have been expected), but it did
find a (to my mind less useful) application for the : modifier.

-- 
Thomas A. Russ,  USC/Information Sciences Institute