Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Modulo operation to return quotient and remainder

by moritz (Cardinal)
on Jul 12, 2012 at 11:54 UTC ( #981384=note: print w/replies, xml ) Need Help??


in reply to Modulo operation to return quotient and remainder

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.

  • Comment on Re: Modulo operation to return quotient and remainder

Replies are listed 'Best First'.
Re^2: Modulo operation to return quotient and remainder
by Borodin (Sexton) on Jun 07, 2017 at 21:49 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://981384]
help
Chatterbox?
Discipulus is managing to update/crack his first garmin navigtor: until now just paper maps
Discipulus and discoverd it use sqlite db.. DBI can read it!

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-09-25 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (280 votes). Check out past polls.

    Notices?