# Modulus Operator (%)

Discussion in 'Perl Misc' started by Mike Flannigan, Sep 22, 2004.

1. ### Mike FlanniganGuest

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
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

Mike Flannigan, Sep 22, 2004

2. ### A. Sinan UnurGuest

Mike Flannigan <> wrote in
news::

>
> 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
d
(remove '.invalid' and reverse each component for email address)

A. Sinan Unur, Sep 22, 2004

3. ### Paul LalliGuest

"Mike Flannigan" <> wrote in message
news:...
> 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

Paul Lalli, Sep 22, 2004
4. ### Mike FlanniganGuest

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

Mike Flannigan, Sep 22, 2004
5. ### Brian McCauleyGuest

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".

Brian McCauley, Sep 22, 2004
6. ### Andres Monroy-HernandezGuest

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

Mike Flannigan <> wrote in message news:<>...
> 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
> 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

Andres Monroy-Hernandez, Sep 23, 2004
7. ### Eric AmickGuest

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

>
>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

Eric Amick, Sep 23, 2004
8. ### Guest

"Paul Lalli" <> wrote:
> "Mike Flannigan" <> wrote in message
> news:...
> > 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.

That should be described as "operands will be interpreted as integers".

"Given integer operands" suggest that will be other sections of
documentation to cover the other possibilities.

Xho

--