Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Modulo operation to return quotient and remainder

by darklord_999 (Acolyte)
on Jul 11, 2012 at 19:42 UTC ( #981240=perlquestion: print w/ replies, xml ) Need Help??
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
Re: Modulo operation to return quotient and remainder
by kcott (Abbot) 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

Re: Modulo operation to return quotient and remainder
by dasgar (Deacon) 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 davido (Archbishop) 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 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.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981240]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-12-22 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls