Re: Equivalent to Gamemaker's sign() function?
Posted: Sun Feb 11, 2018 2:13 am
Thanks for the benchmark. Wow, I didn't expect the branch version is actually this much slower than those with floating point * / , assuming the benchmark is done correctly.
The clamp version won't work in cases that the number is very close to 0. This could be important for some algorithms. The minimum positive number representable in double is approximately 4.9 * 10^(−324), while the maximum is approximately 1.7976931348623157 * 10^308. (https://en.wikipedia.org/wiki/Double-pr ... int_format) It could still be the case that math.abs(n*math.huge) < 1. I think n*math.huge*math.huge would suffice.
I'd suggest to rewrite well-isolated Lua code in C, if the performance really matters. It will give you a tighter control over everything.
The clamp version won't work in cases that the number is very close to 0. This could be important for some algorithms. The minimum positive number representable in double is approximately 4.9 * 10^(−324), while the maximum is approximately 1.7976931348623157 * 10^308. (https://en.wikipedia.org/wiki/Double-pr ... int_format) It could still be the case that math.abs(n*math.huge) < 1. I think n*math.huge*math.huge would suffice.
I'd suggest to rewrite well-isolated Lua code in C, if the performance really matters. It will give you a tighter control over everything.