Velocity Reviews > Perl > Modulus Operator (%)

# Modulus Operator (%)

Mike Flannigan
Guest
Posts: n/a

 09-22-2004

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

A. Sinan Unur
Guest
Posts: n/a

 09-22-2004
Mike Flannigan <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

>
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

Paul Lalli
Guest
Posts: n/a

 09-22-2004
"Mike Flannigan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

Mike Flannigan
Guest
Posts: n/a

 09-22-2004

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

Brian McCauley
Guest
Posts: n/a

 09-22-2004

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

Andres Monroy-Hernandez
Guest
Posts: n/a

 09-22-2004
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...tive-Operators

Regards,

- Andrés Monroy-Hernández

Mike Flannigan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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

Eric Amick
Guest
Posts: n/a

 09-23-2004
On Wed, 22 Sep 2004 18:56:13 GMT, Mike Flannigan
<(E-Mail Removed)> 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

ctcgag@hotmail.com
Guest
Posts: n/a

 09-23-2004
"Paul Lalli" <(E-Mail Removed)> wrote:
> "Mike Flannigan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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

--
Usenet Newsgroup Service \$9.95/Month 30GB