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

Can anyone explain this:

my $mod = 10 % 2; print "mod = $mod\n";

mod = 0

and this:

my $mod = 119.5 % 59.75; print "mod = $mod\n";

mod = 1

I do not see why there should be a difference... Anyone knows an answer?
Am I missing something primitive?

Replies are listed 'Best First'.
Re: Operator % (mod) bug ?
by Corion (Patriarch) on Feb 09, 2012 at 13:41 UTC

    The modulo operator % is only defined for integer numbers. The documentation in perlop could be more clear about this.

    My advice is to use whole numbers, for example by multiplying all entities by 100 in your example.

      Yes, thank you! I used the division (/) and then check for:

      m/^\d+\z/
Re: Operator % (mod) bug ?
by choroba (Cardinal) on Feb 09, 2012 at 14:00 UTC
    The % operator works on integers only. The second expression becoms 119 % 59, which is indeed 1. See int.