Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

Why does the below give two different answers? The first usage returns the expected correct value (1), while the second returns 0. Perl 5.8.8 Win32.

Thanks in advance.

Very confused!

use strict; my $num=3891.0; my $answer=$num % 5; print "num is $num answer is $answer \n"; my $num=38.91 * 100; my $answer=$num % 5; print "num is $num answer is $answer \n";

Replies are listed 'Best First'.
Re: Modulus strangeness (%)
by ikegami (Patriarch) on Aug 30, 2007 at 17:34 UTC

    38.91 is a periodic number in binary, so some truncating occurs when the computer tries to store that number as a float. That means that 38.91 * 100 is not equal to 3891.

    >perl -le"printf '%.20e', 38.91 3.89099999999999970000e+001 >perl -le"printf '%.20e', 38.91 * 100 3.89099999999999950000e+003

    That means you are calculating the modulus of 3890, not 3891.

    Solution 1: You could round the number: (1024 is a power of two, so no error is introduced. But 1000 would be fine too since the error introduced by 1000 would be so small that it would be removed by the rounding.)

    >perl -le"$n=38.91*100; $n=sprintf('%f',$n*1024)/1024; print $n%5; 1

    Solution 2: Work in cents (or even smaller units) instead of dollars, so that your numbers are all integers.

    >perl -le"$n=3891; print $n%5; 1

    Update: Elaborated a little.

Re: Modulus strangeness (%)
by dwm042 (Priest) on Aug 30, 2007 at 18:10 UTC
    ikegami is right on. If you modify the program to round $num after the second assignment of num, as so:

    use strict; my $num=3891.0; my $answer=$num % 5; print "num is $num answer is $answer \n"; my $num=38.91 * 100; my $answer=int($num + 0.5) % 5; print "num is $num answer is $answer \n";
    You get these answers:

    C:\Code>perl modtest.pl num is 3891 answer is 1 num is 3891 answer is 1