-
Notifications
You must be signed in to change notification settings - Fork 2
/
_round.gml
40 lines (36 loc) · 1.14 KB
/
_round.gml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// @func _round(x[, step[, down]])
/// @desc Rounds a number to the nearest multiple of a given step size. This generalizes the built-in round() function, which uses a step size of 1.
/// @param {real} x - Number to round.
/// @param {real} [step=1.0] - Step size to round to.
/// @param {bool} [down=true] - Whether to round down in case of a tie. If false, rounds up instead.
/// @return {real} Nearest multiple of step to x.
function _round(xx)
{
// Get step size and direction arguments
var step = (argument_count > 1 ? argument[1] : 1.0);
var down = (argument_count > 2 ? argument[2] : true);
// Verify that step size is positive
if (step <= 0)
return undefined;
// Get normalized distance between consecutive multiples of the step
var dist = (xx mod step)/step;
// Use distance to decide rounding direction
if (dist < 0.5)
{
// Round down if less than half
return xx - (xx mod step);
}
else if (dist > 0.5)
{
// Round up if more than half
return xx - (xx mod step) + step;
}
else
{
// Break ties based on direction argument
if (down == true)
return xx - (xx mod step);
else
return xx - (xx mod step) + step;
}
}