From: ·····@gnu.org
Subject: Re: *print-readably* and printing numbers
Date: 
Message-ID: <y7c4ruv8f58.fsf@sindri.juniper.net>
> On the cmucl mailing list, we are having a discussion on what
> *print-readably* should when printing numbers.
> Assume *read-default-float-format* is 'single-float and we do this:
> (let ((*print-readably* t))
>   (print 1.0f0)
>   (print 1.0d0))
> CMUCL produces
> 1.0
> 1.0d0
> Clisp says
> 1.0f0
> 1.0d0
> CMUCL produces different results if *read-default-float-format* is
> 'double-float:
> 1.0f0
> 1.0
> I think Clisp is doing the right thing, although the others seem to be
> compatible with CLHS.

While we're on the topic of printing numbers, perhaps somebody can
offer some advice.

I was using Steele's PICTURE function (CLtL2, 12.5.3, p339; the HTML
version has two omitted lines (which have vital closing parens, so you
can find them easily), and adds spaces at the end which will mess up a
FORMAT string).  I realize that it's not definitive, and has a few
minor bugs; for instance, if called without parameters, it tries to
coerce #'sqrt to a string.

What gets me is in the MOBY-GRID, when it tries to pick out the point
#c((/ pi 2) 0) for a tick mark.  Under CMUCL, this causes the value
"1.5707963267948966d0" to be emitted, which is obviously not parsable
by PostScript (which doesn't support the "1d0" syntax).

I ended up coercing the values to single-floats in the printing
function, but that just doesn't seem to be the Right Way to handle the
issue.  Besides the fact that it truncates the precision (which
admittedly isn't a problem for this particular application), it just
has a very icky feel to it.

I tried a format ~G, which was slightly better, but it still would
emit "-9.00000035762786900d-2" later.

Is there any way to tell CL to just display the number, but don't ever
use an exponential notation?

Sorry about the newbie question.

Thanks,
joelh

From: Pekka P. Pirinen
Subject: Re: *print-readably* and printing numbers
Date: 
Message-ID: <ix66fa66rf.fsf@harlequin.co.uk>
> this causes the value "1.5707963267948966d0" to be emitted, which is
> obviously not parsable by PostScript (which doesn't support the
> "1d0" syntax). [...]  I ended up coercing the values to
> single-floats in the printing function, but that just doesn't seem
> to be the Right Way to handle the issue.

No, it doesn't.  Sounds like you have *READ-DEFAULT-FLOAT-FORMAT* set
to something other than DOUBLE-FLOAT.  Setting that will fix the pi/2
case and any other numbers between 10^-3 (inclusive) and 10^7
(exclusive), which is probably enough for the PICTURE code.

> Is there any way to tell CL to just display the number, but don't
> ever use an exponential notation?

FORMAT ~F is for that purpose.  (Even ~F is allowed to switch to
exponential if more than 100 digits would have to be printed, but
that's usually impractical, anyway.)
-- 
Pekka P. Pirinen, Harlequin Limited/Global Graphics
A race is a group of people united by a common misconception of
ancestry.
From: ·····@gnu.org
Subject: Re: *print-readably* and printing numbers
Date: 
Message-ID: <y7ck83qdw1a.fsf@sindri.juniper.net>
>> this causes the value "1.5707963267948966d0" to be emitted, which is
>> obviously not parsable by PostScript (which doesn't support the
>> "1d0" syntax). [...]  I ended up coercing the values to
>> single-floats in the printing function, but that just doesn't seem
>> to be the Right Way to handle the issue.
> No, it doesn't.  Sounds like you have *READ-DEFAULT-FLOAT-FORMAT* set
> to something other than DOUBLE-FLOAT.  Setting that will fix the pi/2
> case and any other numbers between 10^-3 (inclusive) and 10^7
> (exclusive), which is probably enough for the PICTURE code.

Yes, this is true, but this causes a lot of other floats to be printed
as "0.0f0", etc, which is the same problem with its reversable coat
turned around.

>> Is there any way to tell CL to just display the number, but don't
>> ever use an exponential notation?
> FORMAT ~F is for that purpose.  (Even ~F is allowed to switch to
> exponential if more than 100 digits would have to be printed, but
> that's usually impractical, anyway.)

Perhaps I was unclear.  I tried that (I may have omitted that in my
original post), but a normal ~F will print an exponentiation type
under CMUCL in this particular case:

* (format nil "~F" (/ pi 2))
"1.5707963267948966d0"

Specifying some parameters to ~F will address that:

* (format nil "~5F" (/ pi 2))
"1.571"

but at a loss of precision.  While this loss is acceptable in this
application, it may not be in others.  Granted, I could specify a
field length so wide that any float can fit:

* (format nil "~69F" (/ pi 2))
"                                                   1.5707963267948966"

but that seems to be a bit ridiculous, not to mention depend on the
implementation.

Really, I'm just perplexed.  CL's FORMAT facility is very powerful, so
it seems like there would be a way to just print a number, with no
exponentiation notation (probably with an "if possible within N
digits" clause) or padding.

Thanks,
joelh

PS: Just out of curiosity, does anybody know what implementation
Steele was using when he wrote that code?
From: Francis Leboutte
Subject: Re: *print-readably* and printing numbers
Date: 
Message-ID: <dnkkft0aidr2auh429e7denhavfjb9ah9a@4ax.com>
·····@gnu.org wrote:

>...
>Perhaps I was unclear.  I tried that (I may have omitted that in my
>original post), but a normal ~F will print an exponentiation type
>under CMUCL in this particular case:
>
>* (format nil "~F" (/ pi 2))
>"1.5707963267948966d0"
>
>Specifying some parameters to ~F will address that:
>
>* (format nil "~5F" (/ pi 2))
>"1.571"
>
>but at a loss of precision.  While this loss is acceptable in this
>application, it may not be in others.  Granted, I could specify a
>field length so wide that any float can fit:
>
>* (format nil "~69F" (/ pi 2))
>"                                                   1.5707963267948966"
>
>but that seems to be a bit ridiculous, not to mention depend on the
>implementation.
>
>Really, I'm just perplexed.  CL's FORMAT facility is very powerful, so
>it seems like there would be a way to just print a number, with no
>exponentiation notation (probably with an "if possible within N
>digits" clause) or padding.
>

Same thing with ACL5

(format nil "~F" 1.5707963267948966d0)
;;;> "1.5707963267948966d0"

(format nil "~,20F" 1.5707963267948966d0)
;;;> "1.57079632679489660000"

This seems contradictory to the following (from the 22.3.3.1 ANSI CL entry,
Tilde F: Fixed-Format Floating-Point):
If both w and d are omitted, then the effect is to print the value using
ordinary free-format output; prin1 uses this format for any number whose
magnitude is either zero or between 10-3 (inclusive) and 107 (exclusive).

BTW

(format nil "~F" 1.5707963267948966)
;;;> "1.5707964"
--
www.algo.be
Logo programming : www.algo.be/logo.html