# Modulus Operator (%)

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Got an easy one here:

use strict;
use warnings;

my \$num1 = 10.564;
my \$num2 = 4;
my \$num3 = \$num1 % \$num2;

print "\n\$num1 - \$num2 - \$num3\n\n";

When I run that I get
10.564 - 4 - 2

I expected
10.564 - 4 - 2.564

The documentation says:
Binary ``%'' computes the modulus of two numbers. Given integer operands
\$a
and \$b: If \$b is positive, then \$a % \$b is \$a minus the largest multiple
of
\$b that is not greater than \$a.

snip

Note than when use integer is in scope, ``%'' gives you
direct access to the modulus operator as implemented by your C compiler.
This
operator is not as well defined for negative operands, but it will
execute faster.

What am I not seeing in all this?

Mike Flannigan

## Re: Modulus Operator (%)

Mike,

If you read carefully it says "given integer operands", therefore,
strictly speaking, the behaviour for non-integer operands is not
defined.

Most likely the floats get int()ed.

http://www.perldoc.com/perl5.8.0/pod/perlop.html#Multiplicative-Operators

Regards,

- Andrés Monroy-Hernández

> Got an easy one here:
>
> use strict;
> use warnings;
>
> my \$num1 = 10.564;
> my \$num2 = 4;
> my \$num3 = \$num1 % \$num2;
>
> print "\n\$num1 - \$num2 - \$num3\n\n";
>
>
> When I run that I get
> 10.564 - 4 - 2
>
> I expected
> 10.564 - 4 - 2.564
>
>
> The documentation says:
> Binary ``%'' computes the modulus of two numbers. Given integer operands
> \$a
> and \$b: If \$b is positive, then \$a % \$b is \$a minus the largest multiple
> of
> \$b that is not greater than \$a.
>
> snip
>
> Note than when use integer is in scope, ``%'' gives you
> direct access to the modulus operator as implemented by your C compiler.
> This
> operator is not as well defined for negative operands, but it will
> execute faster.
>
>
> What am I not seeing in all this?
>
>
> Mike Flannigan

## Re: Modulus Operator (%)

>
> Got an easy one here:
>
> use strict;
> use warnings;
>
> my \$num1 = 10.564;
> my \$num2 = 4;
> my \$num3 = \$num1 % \$num2;
>
> print "\n\$num1 - \$num2 - \$num3\n\n";
>
>
> When I run that I get
> 10.564 - 4 - 2
>
> I expected
> 10.564 - 4 - 2.564
>
>
> The documentation says:
> Binary ``%'' computes the modulus of two numbers. Given integer
> operands \$a and \$b:

....

> What am I not seeing in all this?

"Integer operands".

Sinan.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)

## Re: Modulus Operator (%)

> use strict;
> use warnings;
>
> my \$num1 = 10.564;
> my \$num2 = 4;
> my \$num3 = \$num1 % \$num2;
>
> print "\n\$num1 - \$num2 - \$num3\n\n";
> When I run that I get
> 10.564 - 4 - 2
>
> I expected
> 10.564 - 4 - 2.564
>
> The documentation says:
> Binary ``%'' computes the modulus of two numbers. Given integer
operands
> \$a and \$b: If \$b is positive, then \$a % \$b is \$a minus the largest
multiple
> of \$b that is not greater than \$a.
>
>
> What am I not seeing in all this?

The part that said "given integer operands".  That means that the
operands to % are converted to integers, regardless of what they
actually are.  This is a lesser example of Perl's well known type
conversions (ex, converting the string "5\n" to the number 5 when used
in an addition operation).  The arguments are converted from whatever
they are (strings, decimals, etc) to integers, and the results of those
conversions are used in the operation.

Paul Lalli

## Re: Modulus Operator (%)

Paul Lalli wrote:

> The part that said "given integer operands".  That means that the
> operands to % are converted to integers, regardless of what they
> actually are.  This is a lesser example of Perl's well known type
> conversions (ex, converting the string "5\n" to the number 5 when used
> in an addition operation).  The arguments are converted from whatever
> they are (strings, decimals, etc) to integers, and the results of those
> conversions are used in the operation.
>
> Paul Lalli

Ah yes - I thought I'd be hitting myself in the head.
Thanks alot guys.

Mike

## Re: Modulus Operator (%)

Mike Flannigan wrote:

> my \$num1 = 10.564;
> my \$num2 = 4;
> my \$num3 = \$num1 % \$num2;
>
> print "\n\$num1 - \$num2 - \$num3\n\n";
>
>
> When I run that I get
> 10.564 - 4 - 2
>
> I expected
> 10.564 - 4 - 2.564
>
>
> The documentation says:
> Binary ``%'' computes the modulus of two numbers. Given integer operands
> \$a and \$b:

> What am I not seeing in all this?

The implication that "given integer operands" really means "given
operands that will be converted to integers".

## Re: Modulus Operator (%)

On Wed, 22 Sep 2004 18:56:13 GMT, Mike Flannigan

>
>Got an easy one here:
>
>use strict;
>use warnings;
>
>my \$num1 = 10.564;
>my \$num2 = 4;
>my \$num3 = \$num1 % \$num2;
>
>print "\n\$num1 - \$num2 - \$num3\n\n";
>
>
>When I run that I get
>10.564 - 4 - 2
>
>I expected
>10.564 - 4 - 2.564

If you want that behavior, use fmod:

use POSIX qw(fmod);
\$num3 = fmod(\$num1, \$num2);

--
Eric Amick
Columbia, MD