• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] ModuloReal()

Status
Not open for further replies.
Level 16
Joined
Oct 12, 2008
Messages
1,570
Hello Hivers.

I have seen this function in use, and i cannot understand how this can ever work!?
JASS:
function ModuloReal takes real dividend, real divisor returns real
    local real modulus = dividend - I2R(R2I(dividend / divisor)) * divisor
    // If the dividend was negative, the above modulus calculation will
    // be negative, but within (-divisor..0).  We can add (divisor) to
    // shift this result into the desired range of (0..divisor).
    if (modulus < 0) then
        set modulus = modulus + divisor
    endif
    return modulus
endfunction
Now i have been thinking, and with the basic math rules, i can only get an answer around 0. Multiplying comes before addition and substraction.
That will make dividend/divisor * divisor the same as 'dividend'. Or almost, because the they use I2R(R2I()).
Now when we substract (almost) dividend from dividend, we get 0. Right?
Or does Jass not follow the basic math rules?
I am confused, can you explain?

-Yixx,,-
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
/ and * have the same "strength", i.e. you read from left to right without changing the order of the operations. It's a basic math rule. For example:

3/5 * 5 = 0, because 3/5 is 0 (round down in jass) and 0*5 = 0.
3 - 0 = 3. Thus, the modulus of 3/5 is 3.

Another example:
6/5 = 1
6 - 6/5 * 5 = 6 - (6/5) * 5 = 6 - (1) * 5 = 1. Thus, 6/5 = 1 and the modulus is 1 as well.

Basic math rules are:
() first (from left to right)
roots/powers (from left to right)
mult/div (from left to right)
add/sub (from left to right)

When you're working with real numbers, 6/5*5 indeed is 6, and 1/5 * 5 can be removed from your calculation. But when you're working with integers, you must calculate everything from left to right, because when you're working with integers 6/5*5 is not 6 but 5.
 
Status
Not open for further replies.
Top