in reply to Re: Filthy Floats
in thread Filthy Floats

It is the fault of mathematics, and not any particular one the groups you named.

Floating point numbers simply cannot be stored precisely on a computer. And the most efficient representations to use do not round off to nice-looking numbers in base 10. So what is probably happening is that Access is storing the floating point number as a float. Perl stores it as a double. And since Perl stores it with much more accuracy than it was stored in the database with, when you display it in Perl you can see the round-off error.

UPDATE
merlyn is of course right. There are a finite number of exceptions. How many and what they are depends on the particular hardware you have, and the particular physical representation of a floating point number that you choose to use.

Replies are listed 'Best First'.
Re: Re (tilly) 2: Filthy Floats
by merlyn (Sage) on Jul 16, 2001 at 23:37 UTC
    Floating point numbers simply cannot be stored precisely on a computer.
    I know what you mean, but it's not what you said. {grin}

    Insert Some at the beginning of that sentence to make it true. I can certainly represent "0.5" precisely in IEEE floating point. And actually, it might make more sense to say:

    Most fixed-decimal values cannot be represented precisely as binary floating-point numbers, no matter what the precision, because 1/10th is an infinite repeating fraction in binary. Unless the number is an integer divided by a power of two, you'll get some sort of truncation error.

    -- Randal L. Schwartz, Perl hacker

      Most fixed-decimal values cannot be represented precisely as binary floating-point numbers, no matter what the precision, because 1/10th is an infinite repeating fraction in binary. Unless the number is an integer divided by a power of two, you'll get some sort of truncation error.

      Kinda makes me long for the days of BCD arithmetic on an old Motorola 6502c -- there was a chip that knew how to handle base-10 arithmetic! *g*

      Seriously, though, it makes me wonder about constructing a "BCD-based float" object; the significant digits are stored in BCD (binary coded decimal, for those of you too young to remember) format, and the exponent is stored as a signed short -- you'd get 11 digits of precision out of a packed 8-byte structure. The unfortunate part is that you would have to emulate the BCD arithmetic in software, increasing the computation times considerably.

      hehehe just to be picky, one more thing: Tilly said there were a "finite number" of exceptions. If you want, I can demonstrate that the number of exceptions (ie. the number of reals without terminating base-2 representations) is not only infinite, it is also uncountable. *g*

      Spud Zeppelin * spud@spudzeppelin.com