Incorrect floatfract behaviour at negative values.
MuthaX opened this issue · 2 comments
Abstract
The floatfract(Float:value); function is most likely implemented in backend (C) as
float floatfract(float value) {
return (value - floor(value));
}
Which is incorrect when value is negative.
The proper way is (c):
float floatfract(float value) {
return (value - trunc(value));
}
or (Pawn):
Float:floatfract_odd(Float:value) {
return (value - floatround(value, floatround_tozero));
}
Proof/Issue reason
When you try this function at positive values - everything is ok, but at negative values you get:
floatfract(0.000000) = 0.000000; ok
floatfract(-0.200000) = 0.800000; instead of -0.2
floatfract(-0.500000) = 0.500000; instead of -0.5
floatfract(-0.800000) = 0.199999; instead of -0.2
floatfract(-1.000000) = 0.000000; ok
floatfract(-1.300000) = 0.699999; instead of -0.7
floatfract(-1.500000) = 0.500000; instead of -0.5
floatfract(-1.800000) = 0.199999; instead of -0.2
floatfract(-1.899999) = 0.100000; instead of -0.1
floatfract(-2.000000) = 0.000000; ok
floatfract(-2.100000) = 0.899999; instead of -0.9
Note
By definition "whole_value = truncated_value +fractional_value".
There is also reason in fact of inaccurate definition of floatfract (it is exist another 2 ways to get fractional part).
While I agree that those results look wrong, I'm not sure your versions are better. Surely the fractional part of -1.80
is -0.80
not 0.2
(current) or -0.2
(yours).
Though that does seem to match the code you posted, which is using floatround_tozero
as I'd expect.