Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

•Re: Best way to make sure a number is an even multiple of another?

by merlyn (Sage)
on Oct 21, 2004 at 13:08 UTC ( #401147=note: print w/ replies, xml ) Need Help??


in reply to Best way to make sure a number is an even multiple of another?

sub fix { my $n = shift; # must be greater or equal to this (assert >= 0) my $m = shift; # must be multiple of this (assert > 0) $m*int(($n+$m-1)/$m); }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on •Re: Best way to make sure a number is an even multiple of another?
Download Code
Re^2: Best way to make sure a number is an even multiple of another?
by demerphq (Chancellor) on Oct 21, 2004 at 13:14 UTC

    Yeah, thats it. Thanks and ++ merlyn. I knew it was possible, I just couldn't see past the damn if. :-)


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi

      Flux8


      I may have misunderstood this, but with $n = 19 and $m = 3, fix gives 21 which is not an even multiple of 3.

      Why not just use $m * $n * 2 ?

        7 * 3 = 21?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-12-29 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (184 votes), past polls