Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func smart_mod(dividend, divisor, sided_toward_zero : bool = false):
- # DECIMAL DIVIDEND, INTEGER DIVISOR
- # smart_mod(4.7, 3) = 1.7
- # DECIMAL DIVIDEND, DECIMAL DIVISOR
- # smart_mod(423.9, 7.49) = 11.95
- # NEGATIVE DIVIDEND
- # smart_mod(-4.7, 3, false) = 1.3
- # smart_mod(-4.7, 3, true) = -1.7
- var out : float
- if divisor == 0:
- printerr("Useful.gd, smart_mod | Attempted division by zero! Returning null")
- return null
- elif dividend == 0:
- return 0
- else:
- var s = sign(dividend * divisor)
- var num = abs(dividend)
- var den = abs(divisor)
- var quo = floor(num / den)
- out = s * (num - (quo * den))
- if !sided_toward_zero:
- out += den
- if out == num:
- out = 0
- return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement