• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Smallest integer multiple of a number >= another number

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

Here is an example:

a = 4

b = 7

foo(4, 7) = 2, because 2 is the smallest multiple of 4 which is greater than or equal to 7 (2 * 4 = 8 >= 7)

Another example:

a = 2

b = 5

foo(2, 5) = 3, because 3 is the smallest multiple of 2 which is greater than or equal to 5 (3 * 2 = 6 >= 5)

How to get this function in JASS?
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
JASS:
function foo takes integer a, integer b returns integer
    /* 
    error check if you want
    if a <= 0 or b <= 0 then
        return 0
    elseif a > b then
        return foo(b, a)
    endif
    */

    if (b/a * a == b) then
        return b/a
    else
        return b/a + 1
    endif
endfunction
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
I doubt a, b will ever be higher than 100 (I don't see them even reaching ~20). So it shouldn't be an issue, but thank you for pointing this out as a note when using muzzel's solution.
 
Status
Not open for further replies.
Top