Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5075c87c46
Fetching contributors…

Cannot retrieve contributors at this time

28 lines (20 sloc) 0.655 kb

Numbers

Test for equality of floating point numbers carefully.

IEEE floating point numbers are wrongly over-trusted in calculations. Observe:

    print "---\n";
    print "A: ", 2.4, "\n";
    print "B: ", 0.2*12, "\n";
    if ( 0.2*12 == 2.4 ) {
        print "These are equal.\n";
    }
    else {
        print "These are not equal.\n";
    }

    A: 2.4
    B: 2.4
    These are not equal.

This is the result of the fact that 0.2 ( 1/5 ) cannot be represented as a binary fraction in IEEE space.

Thus, if you're checking floating-point equality, use sprintf or similar.

See http://perldoc.perl.org/perlfaq4.html for details.

Jump to Line
Something went wrong with that request. Please try again.