Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: float values and operators

by soulchild (Acolyte)
on Aug 11, 2004 at 20:25 UTC ( #382088=note: print w/replies, xml ) Need Help??

in reply to Re: float values and operators
in thread float values and operators

You're right. Using sprintf to round the values before comparison does the job. Nevertheless I didn't quite understand why this is necessary. Could somebody explain this a little bit more verbose? Thanks a lot, guys!

Replies are listed 'Best First'.
Re^3: float values and operators
by mpeg4codec (Pilgrim) on Aug 12, 2004 at 03:55 UTC
    Recipe 2.2 in the Perl Cookbook, Second Edition is excellent reading on this subject.

    Basically, when you use a floating point number, such as 36.6, the computer doesn't store that exact value. Remember, computers are binary. When you get down to it, there's nothing more than transistors. The memory storage only really works well for integers. So the computer has to figure out some way to store approximations of the number.

    To paraphrase the cookbook, the only numbers well-representable in binary are those involving powers of two. For example, .125 is 1/8. That's exactly representable in binary notation of floating point numbers.

    However, take a number like 36.6. In fractional form, it would be 183/5, nothing to do with powers of two. The way the computer really stores the number can be seen:

    my $a = 36.6; printf "%s is %.30g\n", ($_) x 3 for $a;
    (Code shamelessly copied from the Perl Cookbook.)

    That gives us 36.600000000000001. Similarly, 36.8 is given as 36.799999999999997.

    So now you see why the comparisons failed. You weren't working with exact values.

    Perl's (and indeed, the C library's) rounding function are not exactly ``round up at five,'' but actually ``round towards even.'' So that way, sprintf will round 36.6 to an even 36.6 for calculating. So now, numerical comparisons work with nice and even numbers.

    Take a look at the Perl Cookbook on this one. It's a very good read. I've just summarised what it has written very well.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2019-02-23 19:02 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (120 votes). Check out past polls.