ID:161468
 
Is there a way to force byond to do integer rounding, so you don't end up with a decimal number after a division? Rounding is kind of expensive.
Use the round() proc. For example:

round(22/7) returns 3.

Here's another example that's more practical:

mob
var
attack = 10
defense = 3
verb/Attack(mob/M in oview(1))
var/damage = attack - M.defense //Returns 3.333...
M.health -= round(damage) //M loses var damage rounded, which equals 3.
In response to Darkmag1c1an11
I said rounding was expensive. Meaning I would rather avoid using that for something that has to run very frequently.
In response to Obs
Well, depending on what you're dividing by, integer division is possible. For example, if dividing by a power of 2, you can use bit-shifting:
64 >> 1    // 64 / 2
64 >> 2 // 64 / 4
64 >> 3 // 64 / 8
64 >> 4 // 64 / 16
64 >> 5 // 64 / 32
64 >> 6 // 64 / 64

...and so on. Otherwise, round() isn't overly expensive.

Hiead
In response to Obs
I don't think rounding is actually as expensive as you think; probably less so than the division itself. If you're running this extremely frequently and dividing by powers of 2, then bit-shifting will definitely help you.

Lummox JR
In response to Hiead
ah I should have thought of that.

Good. I will make use of this