Perl-Sensitive Sunglasses 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??

```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?

Replies are listed 'Best First'.
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

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?

Create A New User
Node Status?
node history
Node Type: note [id://401147]
help
Chatterbox?
 [johngg]: Yes, must be nearly 30 years ago. [hippo]: Loudest gig I can recall was Wolfsbane at the Astoria in the early 90s. Head rang for about 3 days afterwards. They were top, though. [johngg]: My ears are still ringing! Discipulus was outside The Clash concert in 1985.. [marto]: Wolfsbane , now I'm having flashbacks [choroba]: Isn't Using PerlPod Creatively rather a meditation? [choroba]: I don't see a question

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2017-03-24 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (301 votes). Check out past polls.