Re: (* 2.4 3) => 7.2000003 WTF?! Let's Fix Lisp! Noob Programming Challenge



Kirk Sluder wrote:
perl -e "printf '%.20f', 7.20000000000001;"; echo ""
7.20000000000000994760

It looks like it is scaling the precision to the input.

Um, floats in perl are IEEE 754 double precision, which means that you
get < 16 decimal digits of precision (1 + epsilon == 1 for positive
epsilon < about 2.23 x 10^{-16}). So if you try to print anything
after that, you'll probably get garbage. printf should be smart enough
to realize that and stop you, but it doesn't.

Come on guys and gals, learn some stuff about floating-point, it's good
for you :) There are a number of options besides the traditional IEEE
754 style binary floating point:

* Extended (variable) precision floating point: see e.g. MPFR (Prof.
Fateman and I were working on a Lisp interface for MPFR -- anybody want
to help us finish it?)
* so-called "exact real arithmetic" based on continued fractions (see
e.g. Gosper; a while back there was a Lisp implementation available,
not sure about the status of that)
* FLI arithmetic (good for problems with huge numerical ranges, but
poor precision at the extreme ends of the representable range)

All of these have implementations; it's a programming exercise to link
them to Lisp if they aren't in Lisp already. Clisp already uses
something like MPFR for its LONG-FLOAT objects.

I'd suggest that if you want arithmetic to work sort of "like you
expect," just use MPFR objects, crank the precision really high and
only display the first few digits. That's the easy way to get around
not knowing much numerical analysis. (Hey, there's no shame in that;
Prof. Kahan of IEEE 754 fame estimated at a recent talk this year that
about one qualified numerical analyst is produced in the entire WORLD
each year.)

Poke me if you want some references on floating-point; I'd be glad to
give them to you.

mfh

.



Relevant Pages

  • Re: (* 2.4 3) => 7.2000003 WTF?! Lets Fix Lisp! Noob Programming Challenge
    ... It looks like it is scaling the precision to the input. ... Um, floats in perl are IEEE 754 double precision, which means that you ... Fateman and I were working on a Lisp interface for MPFR -- anybody want ... not knowing much numerical analysis. ...
    (comp.lang.lisp)
  • machine precision and non-normalised numbers
    ... In a number if books (See e.g. Kincaid & Cheney, Numerical Analysis) ... the precision of the floating point arithmetic is defined as ... the machine precision appears to be smaller. ... in the case of the IEEE 754 standard? ...
    (sci.math.num-analysis)
  • Re: Arent decimals safely lumped under rationals?
    ... I don't think it's true that Lisp avoids things like floats, ... And we'd need to fight over the digits of precision problem, ... print your own user-defined DECIMAL-RATIO type shouldn't be that hard. ... Alan Turing ...
    (comp.lang.lisp)
  • Re: Whats so great about lisp?
    ... > Jon> Which Lisp implementations are good at adhering to IEEE FP on x86 and AMD64? ... > Duane> if you don't ensure that the precision is set. ...
    (comp.lang.lisp)
  • Re: Ruby cant subtract ?
    ... IEEE 754 does an inadequate job of representing arbitrary rational ... The problem is that the 754 representation has finite precision. ... provides only binary floating point representations, ... implementations of IEEE 754 existed before it was a standard, ...
    (comp.lang.ruby)