Modulus Operator (%)

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

  1. 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
     
    Mike Flannigan, Sep 22, 2004
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Mike Flannigan

    Paul Lalli Guest

    "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
    #3
  4. 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
    #4
  5. 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
    #5
  6. 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
    > 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
     
    Andres Monroy-Hernandez, Sep 23, 2004
    #6
  7. Mike Flannigan

    Eric Amick Guest

    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
    #7
  8. Mike Flannigan

    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Sep 23, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Andr=E9s?=

    Modulus 12

    =?ISO-8859-1?Q?Andr=E9s?=, Feb 14, 2005, in forum: VHDL
    Replies:
    4
    Views:
    3,866
    Tuukka Toivonen
    Feb 14, 2005
  2. Tasperian Jigs

    Problems with floating point and modulus

    Tasperian Jigs, Apr 18, 2006, in forum: Java
    Replies:
    2
    Views:
    2,120
    Patricia Shanahan
    Apr 18, 2006
  3. priya

    MODULUS operator

    priya, Nov 15, 2006, in forum: VHDL
    Replies:
    3
    Views:
    9,909
    priya
    Nov 16, 2006
  4. exquisitus

    modulus operator "%" versus fmod

    exquisitus, Apr 24, 2005, in forum: C Programming
    Replies:
    3
    Views:
    424
    Joe Wright
    Apr 24, 2005
  5. mohi
    Replies:
    3
    Views:
    557
    Greg Herlihy
    Mar 30, 2008
Loading...

Share This Page