[Openmcl-devel] Printing/Reading floats

Gary Byers gb at clozure.com
Mon Mar 17 08:51:08 PST 2003


There's been a bug in OpenMCL's reader for .... well, since forever.  Some
constants that the reader uses are intended to be double-floats but were
written without qualification (in MCL, the lisp SINGLE-FLOAT and DOUBLE-FLOAT
types are equivalent.)  The reader winds up doing arithmetic on operands
that're less precise than it thinks they are ...

Yannick Versley submitted a patch a few weeks ago; when I manually checked
it in to the 0.13 CVS branch, I cleverly turned a decimal point into a
comma.  (It's a good thing that I didn't try to compile it first or anything;
that would have taken a minute or so  ...)

Ram Krishnan submitted a patch to correct my typo yesterday, and hopefully
the version of "lib;numbers.lisp" now in CVS will compile and has the
correct exponent markers where required.

With that patch in place

? (= pi (read-from-string (prin1-to-string pi)))
T

There certainly are cases where roundoff error is enough of a factor
for an equality test to fail, but I think that the failure's usually
on the order of a bit or two (instead of ~29-30 bits.).

I can't think of a more portable way to read/write floats in CL: if
there's a CL function that does the inverse of INTEGER-DECODE-FLOAT,
I can't remember what it's called.

? (multiple-value-call #'hypothetical-integer-encode-float
     (integer-decode-float f))

would (hypothetically) be guaranteed to be EQL to f (at least for
normalized cases) and the integer triples could be read/written
accurately.


On Mon, 17 Mar 2003, Sven Van Caekenberghe wrote:

> This is probably a silly question, but what is the right (portable cl)
> way to print and read floats ?
>
> ? pi
> 3.141592653589793D0
> ? (prin1-to-string pi)
> "3.141592653589793D0"
> ? (read-from-string *)
> 3.1415926944584456D0
> 19
> ?
>
> As the previous transcript shows, a straightforward approach introduces
> quite a large rounding error, is there another way to do the same thing
> ?
>
> Thx,
>
> Sven
>
> --
> Sven Van Caekenberghe - mailto:sven at beta9.be
> Beta Nine - software engineering - http://www.beta9.be
> .Mac - svc at mac.com - http://homepage.mac.com/svc
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel
>
>


_______________________________________________
Openmcl-devel mailing list
Openmcl-devel at clozure.com
http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel



More information about the Openmcl-devel mailing list