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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

One thing I have always wanted (in every language I've used except assembler) is a function that returned both result and remainder of an integer division.

Both are usually calculated and left in seperate cpu registers after a DIV instruction, but I am forced by to use "/" and "%" to get at them, which means that the division is being done twice.

I did once create a function using in-line assembler in C to do this, but the single return value from a C function meant that the function was messy to use.

Perl's natural ability to return a list and assign lists mean that the idea resurrected itself in my brain when I saw this post and the answers to it.

My solution to this was:

use strict; sub div_mod { return int( $_[0]/$_[1]) , ($_[0] % $_[1]); } my $i = 1332443; my ( $s, $m, $h, $d ); ($m,$s) = div_mod($i, 60); ($h,$m) = div_mod($m, 60); ($d,$h) = div_mod($h, 24); print "$d days, $h hours, $m minutes, $s seconds.\n";

Which I think is rather elegant, but still possible less efficient than the other solutions, and less efficient than it could be as I am still performing the division twice to get at the information.

I took a look at the inline-c stuff, but that doesn't help for the same reasons.

I also took a quick look at the overloading capabilities but I am not familiar enough with Perl to understand that yet.

I am wondering if it is possible to define a new operator for Perl? Maybe "/%" that would do the same thing as my div_mod() sub in the code above but accessing the appropriate registers to save a division?

Possibly better (and more Perlish) would be to have both values returned only if the mod (%) operator was called in a LIST context, especially as mod (%) is an inherently integer operator?

Does this have any merit? If so, would this have to be done deep in the guts of the compiler/interpreter or could it be added as a module in some way?


In reply to A better mod (%) operator? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible editor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2021-05-11 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (116 votes). Check out past polls.

    Notices?