Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re (tilly) 2: Filthy Floats

by merlyn (Sage)
on Jul 16, 2001 at 23:37 UTC ( [id://97154]=note: print w/replies, xml ) Need Help??


in reply to Re (tilly) 2: Filthy Floats
in thread Filthy Floats

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

Replies are listed 'Best First'.
Filthy BCD Floats?
by spudzeppelin (Pilgrim) on Jul 17, 2001 at 21:50 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://97154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found