http://www.perlmonks.org?node_id=981240

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

Is there any way that we can get the quotient and remainder for a modulus operation in perl ?

For example : I have a variable $var = 4999. I perform a modulo operation on it (%100) and get values of 49(quotient) and 99 (remainder) and assign it to two different variables.

Is there any module or way to do this in perl ?

  • Comment on Modulo operation to return quotient and remainder

Replies are listed 'Best First'.
Re: Modulo operation to return quotient and remainder
by kcott (Archbishop) on Jul 11, 2012 at 19:52 UTC

    I'm not aware of any module that does this but it's easy enough to roll your own:

    $ perl -Mstrict -Mwarnings -E ' my ($var, $mod) = (4999, 100); my ($quot, $rem) = (int $var / $mod, $var % $mod); say $quot; say $rem; ' 49 99

    -- Ken

      Just a note if you're using negative values: depending on your application, you might want to use floor() instead of int(). This bit me a while back.
Re: Modulo operation to return quotient and remainder
by davido (Cardinal) on Jul 11, 2012 at 22:30 UTC

    The module you seek is actually just a pragma: integer.

    sub quotient_remainder { use integer; my( $dividend, $divisor ) = @_; my $quotient = $dividend / $divisor; my $remainder = $dividend % $divisor; return ( $quotient, $remainder ); } say quotient_remainder( 3, 2 );

    The integer pragma is lexically scoped, and can be a way of reducing your need to pepper your calculations with int.


    Dave

Re: Modulo operation to return quotient and remainder
by dasgar (Priest) on Jul 11, 2012 at 19:56 UTC

    As pointed out, it's not difficult to do it with just a few lines of code. If you wanted to, you could also just create a subroutine like the untested code below:

    use strict; use warnings; sub modulo { my $number = shift; my $divisor = shift; my $remainder = $number % $divisor; my $quotient = ($number - $remainder) / $divisor; return ($remainder, $quotient); }
Re: Modulo operation to return quotient and remainder
by moritz (Cardinal) on Jul 12, 2012 at 11:54 UTC

    While many modern CPU instruction sets support getting quotient and remainder in a single operation, nearly no high-level programming languages support it. Not even C (unless via embedded assembler).

    In Perl, the overhead of a single subroutine call is probably much bigger than simply performing two separate operations, so from a performance point of view, you shouldn't bother.

    If you are after convenience, it's easy enough to write a subroutine yourself that does it.

      nearly no high-level programming languages support it. Not even C

      Sure it does. stdlib has the div family of functions, which return a div_t structure with quot and rem fields

      Such an operation would be invaluable for things like converting seconds to hours, minutes, and seconds, binary to decimal conversion, or base62 encoding, and it's essential to any kind of rational (as in fractional) data type. With modern languages that support tuples I think it's a shame that we have regressed to writing two expressions to extract both components of what is essentially a value pair

      This is similar to the characteristic/mantissa pair which form a logarithm (and is in some ways the exact same issue). It would be frustrating to have to call charac(15) to get 1 and mant(15) to get .1761, and the same is true of integer division

Re: Modulo operation to return quotient and remainder
by bart (Canon) on Jul 11, 2012 at 23:30 UTC
    It'll be faster to just do it in 2 operations...

    Please refrain from misguided (premature) optimizations. This isn't 1986 any more.