views:

248

answers:

8

How do I fix this code so that 1.1 + 2.2 == 3.3? What is actually happening here that's causing this behavior? I'm vaguely familiar with rounding problems and floating point math, but I thought that applied to division and multiplication only and would be visible in the output.

[me@unixbox1:~/perltests]> cat testmathsimple.pl 
#!/usr/bin/perl

use strict;
use warnings;

check_math(1, 2, 3);
check_math(1.1, 2.2, 3.3);

sub check_math {
        my $one = shift;
        my $two = shift;
        my $three = shift;

        if ($one + $two == $three) {
                print "$one + $two == $three\n";
        } else {
                print "$one + $two != $three\n";
        }
}

[me@unixbox1:~/perltests]> perl testmathsimple.pl 
1 + 2 == 3
1.1 + 2.2 != 3.3

Edit:

Most of the answers thus far are along the lines of "it's a floating point problem, duh" and are providing workarounds for it. I already suspect that to be the problem. How do I demonstrate it? How do I get Perl to output the long form of the variables? Storing the $one + $two computation in a temp variable and printing it doesn't demonstrate the problem.

Edit:

Using the sprintf technique demonstrated by aschepler, I'm now able to "see" the problem. Further, using bignum, as recommended by mscha and rafl, fixes the problem of the comparison not being equal. However, the sprintf output still indicates that the numbers aren't "correct". That's leaving a modicum of doubt about this solution.

Is bignum a good way to resolve this? Are there any possible side effects of bignum that we should look out for when integrating this into a larger, existing, program?

+1  A: 

Use sprintf to convert your variable into a formatted string, and then compare the resulting string.

# equal( $x, $y, $d );
# compare the equality of $x and $y with precision of $d digits below the decimal point.
sub equal {
    my ($x, $y, $d) = @_;
    return sprintf("%.${d}g", $x) eq sprintf("%.${d}g", $y);   
}

This kind of problem occurs because there is no perfect fixed-point representation for your fractions (0.1, 0.2, etc). So the value 1.1 and 2.2 are actually stored as something like 1.10000000000000...1 and 2.2000000....1, respectively (I am not sure if it becomes slightly bigger or slightly smaller. In my example I assume they become slightly bigger). When you add them together, it becomes 3.300000000...3, which is larger than 3.3 which is converted to 3.300000...1.

YYC
Alternatively, do math with higher precicion, as provided by the `Math::Big*`-family of modules (`Math::BigInt`, `Math::BigFloat`, `Math::BigRat`, `bignum`, `bigint`, `bigrat`, ...)
rafl
Do not link to unlicensed copies of books.
Sinan Ünür
Sorry. Didn't aware of that. Link removed.
YYC
The issue I have with this is that Perl seems to round differently depending on the requested precision.#!/usr/bin/perluse strict;use warnings;my $three = 3.3;print sprintf("%.2f", $three) . "\n";print sprintf("%.6f", $three) . "\n";print sprintf("%.10f", $three) . "\n";print sprintf("%.20f", $three) . "\n";print sprintf("%.40f", $three) . "\n";print sprintf("%.60f", $three) . "\n";3.303.3000003.30000000003.299999999999999822363.29999999999999982236431605997495353221893.299999999999999822364316059974953532218933105468750000000000
+2  A: 

abs($three - ($one + $two)) < $some_Very_small_number

Matt Kane
+14  A: 

See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

None of this is Perl specific: There are an uncountably infinite number of real numbers and, obviously, all of them cannot be represented using only a finite number of bits.

The specific "solution" to use depends on your specific problem. Are you trying to track monetary amounts? If so, use the arbitrary precision numbers (use more memory and more CPU, get more accurate results) provided by bignum. Are you doing numeric analysis? Then, decide on the precision you want to use, and use sprintf (as shown below) and eq to compare.

You can always use:

use strict; use warnings;

check_summation(1, $_) for [1, 2, 3], [1.1, 2.2, 3.3];

sub check_summation {
    my $precision = shift;
    my ($x, $y, $expected) = @{ $_[0] };
    my $result = $x + $y;

    for my $n ( $x, $y, $expected, $result) {
        $n = sprintf('%.*f', $precision, $n);
    }

    if ( $expected eq $result ) {
        printf "%s + %s = %s\n", $x, $y, $expected;
    }
    else {
        printf "%s + %s != %s\n", $x, $y, $expected;
    }
    return;
}

Output:

1.0 + 2.0 = 3.0
1.1 + 2.2 = 3.3
Sinan Ünür
I'm going to accept this as the answer because it's the most complete so far. However, I also found the Perl-specific material that brian d foy provided in a comment to be beneficial.
+6  A: 

"What Every Computer Scientist Should Know About Floating-Point Arithmetic"

Basically, Perl is dealing with floating-point numbers, while you are probably expecting it to use fixed-point. The simplest way to handle this situation is to modify your code so that you are using whole integers everywhere except, perhaps, in a final display routine. For example, if you're dealing with USD currency, store all dollar amounts in pennies. 123 dollars and 45 cents becomes "12345". That way there is no floating point ambiguity during add and subtract operations.

If that's not an option, consider Matt Kane's comment. Find a good epsilon value and use it whenever you need to compare values.

I'd venture to guess that most tasks don't really need floating point, however, and I'd strongly suggest carefully considering whether or not it is the right tool for your task.

dpk
The problem is *not* floating point vs. fixed point at all. The problem is binary vs. decimal and limited precision.
Michael Borgwardt
There are an uncountably infinite number of real numbers. Clearly, you cannot represent all of them exactly with a finite number of bits.
Sinan Ünür
+3  A: 

From The Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

What can I do to avoid this problem?

That depends on what kind of calculations you’re doing.

  • If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.
  • If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
  • If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.

Youz could also use a "fuzzy compare" to determine whether two numbers are close enough to assume they'd be the same using exact math.

Michael Borgwardt
+4  A: 

To see precise values for your floating-point scalars, give a big precision to sprintf:

print sprintf("%.60f", 1.1), $/;
print sprintf("%.60f", 2.2), $/;
print sprintf("%.60f", 3.3), $/;

I get:

1.100000000000000088817841970012523233890533447265625000000000
2.200000000000000177635683940025046467781066894531250000000000
3.299999999999999822364316059974953532218933105468750000000000

Unfortunately C99's %a conversion doesn't seem to work. perlvar mentions an obsolete variable $# which changes the default format for printing a number, but it breaks if I give it a %f, and %g refuses to print "non-significant" digits.

aschepler
+4  A: 

A quick way to fix floating points is to use bignum. Simply add a line

use bignum;

to the top of your script. There are performance implications, obviously, so this may not be a good solution for you.

A more localized solution is to use Math::BigFloat explicitly where you need better accuracy.

mscha
+1  A: 

Number::Fraction lets you work with rational numbers (fractions) instead of decimals, something like this (':constants' is imported to automatically convert strings like '11/10' into Number::Fraction objects):

use strict;
use warnings;
use Number::Fraction ':constants';

check_math(1, 2, 3);
check_math('11/10', '22/10', '33/10');

sub check_math {
        my $one = shift;
        my $two = shift;
        my $three = shift;

        if ($one + $two == $three) {
                print "$one + $two == $three\n";
        } else {
                print "$one + $two != $three\n";
        }
}

which prints:

1 + 2 == 3
11/10 + 11/5 == 33/10
MkV