Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: A better mod (%) operator?

by hdb (Monsignor)
on Jan 10, 2014 at 20:13 UTC ( [id://1070185]=note: print w/replies, xml ) Need Help??


in reply to A better mod (%) operator?

If you are happy to lose the original numerator, you could do it with side effects on the arguments. The original numerator would become the result of div and the function itself would return the remainder. (Just for the fun of it in autoboxing notation...)

use strict; use warnings; my $rdiv = sub { my $num = $_[0]; $_[0] = int( $_[0]/$_[1] ); $num - $ +_[0]*$_[1] }; my $x = 11; my $y = 4; my $r = $x->$rdiv( $y ); print "$x, $r\n";

Replies are listed 'Best First'.
Re^2: A better mod (%) operator?
by BrowserUk (Patriarch) on Jan 10, 2014 at 20:36 UTC
    you could do it with side effects on the arguments.

    That kind of defeats the original purpose of utilising the fact that -- for every processor I'm knowledgeable of; though that's not a huge sample -- at the machine code level, DIV instructions produce both quotient and remainder as the result of a single operation.

    From the Intel manual:

    DIV—Unsigned Divide

    Description: Divides unsigned the value in the AX, DX:AX, EDX:EAX, or RDX:RAX registers (divi- dend) by the source operand (divisor) and stores the result in the AX (AH:AL), DX:AX, EDX:EAX, or RDX:RAX registers.

    The two operations of finding quotient and remainder of one number divided by another are so often used in concert with each other, it seems a waste to have to perform the division twice to get at both parts.

    (Just for the fun of it in autoboxing notation...)

    Yuck! What a waste of cpu :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1070185]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-19 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found