Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Fix floats like you do in your head

by mojotoad (Monsignor)
on Dec 24, 2002 at 21:32 UTC ( #222136=note: print w/replies, xml ) Need Help??

in reply to Fix floats like you do in your head

This is a clever way to deal with the problem, but not very consistent (in that some numbers are treated differently than others) -- this inconsistency will make most mathematicians and scientists quake in their boots.

The proper solution is to define an epsilon that represents the precision used to compensate for accumulated rounding errors.

Indeed, this is the driving force behind the IEEE 754 standard.

Also of interest: What Every Computer Scientist Should Know About Floating-Point Arithmetic (1991) by David Golberg, is a great discussion of the IEEE 754 standard and its practical application.


  • Comment on Re: Fix floats like you do in your head

Replies are listed 'Best First'.
Re: Re: Fix floats like you do in your head
by tachyon (Chancellor) on Dec 25, 2002 at 00:11 UTC

    Great links. As you say this is not a ceil() or a round() function which behaves in strict mathematical terms, nor does it attempt to solve any of the problems associated with doing decimal fp arithmetic in binary.

    The beauty/purpose of it is that is does what a human tends to do. I see 0.9999999 and think 1 or 0.00999999 and think 0.01 which is what you get with the approx() function. I did call it approx() rather than exact(), actually() or really() ;-). I had it in mind for simple display functions not anything serious.

    It also only works on the decimal fraction but could be extended to work on the integer component so 999999.9 or 1000000.1 would become 1,000,000.




      No worries. After I went and perused your Math::Trig::Units work, it became evident that this wasn't an oversight on your part. Ah well, the links are there for posterity, in case anyone else wants to learn about it.

      As for names...approx() works. How about fudge()?

      At any rate, at least you know that we're dealing with a psychological tendency, here. With 6 sig figs, for example, 1.99999 is no more special than 1.46793 on a mathematical basis. On a psychological basis it is somewhat irritating because of our preference for nice, neat numbers.


        fudge() is good. I will add it as an alias. I couldn't think of anything better at the time. might_be() looks_like() probably() possibly() neaten() all seemed...well a little wishy washy.

        You are of course right about the psychological tendency. That's why the marketers love $9.99 etc because it just seems less than a 10 spot. Still don't get much useful change though.

        Do you know if there is anything in Perl that implements the standards. A Math::AcurateFloatingPoint class would be quite possible using Math::BigInt without too much work. You just need to delta to integers for the operations and then return the result as a float object (stringified of course to preserve accuracy). I don't know if there would be much demand though.

        The stats about > 50% of database table numerical columns using floats (including financials) was interesting.




Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2023-09-22 15:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found