http://www.perlmonks.org?node_id=401147


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

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

      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?