Problems? Is your data what you think it is? 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?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-01-16 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (176 votes). Check out past polls.

Notices?