2007-03-29 22:09:25 +00:00
|
|
|
var abs = func(n) { n < 0 ? -n : n }
|
|
|
|
|
2009-05-05 18:34:53 +00:00
|
|
|
var sgn = func(x) { x < 0 ? -1 : x > 0 }
|
2023-09-19 22:45:06 +00:00
|
|
|
var sign = sgn;
|
2009-05-05 18:34:53 +00:00
|
|
|
|
2011-04-20 23:25:41 +00:00
|
|
|
var max = func(x) {
|
|
|
|
var n = x;
|
|
|
|
for (var i = 0; i < size(arg); i += 1) {
|
|
|
|
if (arg[i] > n) n = arg[i];
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
var min = func(x) {
|
|
|
|
var n = x;
|
|
|
|
for (var i = 0; i < size(arg); i += 1) {
|
|
|
|
if (arg[i] < n) n = arg[i];
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
var avg = func {
|
|
|
|
var x = 0;
|
|
|
|
for (var i = 0; i < size(arg); i += 1) {
|
|
|
|
x += arg[i];
|
|
|
|
}
|
|
|
|
x /= size(arg);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-08-19 10:59:14 +00:00
|
|
|
# this follows std::clamp for argument order, as opposed to
|
|
|
|
# qBound which uses (min, value, max)
|
|
|
|
var clamp = func(value, min, max) {
|
|
|
|
return (value < min) ? min : (value > max) ? max : value;
|
|
|
|
}
|
|
|
|
|
2013-10-03 16:42:20 +00:00
|
|
|
# note - mathlib defines an fmod function (added after this was written)
|
|
|
|
# It uses C-library fmod(), which has different rounding behaviour to
|
|
|
|
# this code (eg, fmod(-5, 4) gives -1, whereas this code gives 3)
|
2007-03-29 22:09:25 +00:00
|
|
|
var mod = func(n, m) {
|
|
|
|
var x = n - m * int(n/m); # int() truncates to zero, not -Inf
|
|
|
|
return x < 0 ? x + abs(m) : x; # ...so must handle negative n's
|
|
|
|
}
|
|
|
|
|
|
|
|
var _iln10 = 1/ln(10);
|
2009-05-05 18:34:53 +00:00
|
|
|
var log10 = func(x) { ln(x) * _iln10 }
|
2020-04-07 20:45:35 +00:00
|
|
|
|
|
|
|
var approx_eq = func (a,b, d = 0.000001) {
|
|
|
|
return (abs(a-b) < d);
|
2023-09-19 22:45:06 +00:00
|
|
|
}
|