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


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