Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: float values and operators

by mpeg4codec (Pilgrim)
on Aug 12, 2004 at 03:55 UTC ( #382160=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: float values and operators
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-11-01 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (228 votes), past polls