Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect floatfract behaviour at negative values. #150

Closed
MuthaX opened this issue Aug 14, 2021 · 2 comments
Closed

Incorrect floatfract behaviour at negative values. #150

MuthaX opened this issue Aug 14, 2021 · 2 comments

Comments

@MuthaX
Copy link

MuthaX commented Aug 14, 2021

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).

@Y-Less
Copy link
Member

Y-Less commented Sep 12, 2021

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).

@Y-Less
Copy link
Member

Y-Less commented Sep 12, 2021

Though that does seem to match the code you posted, which is using floatround_tozero as I'd expect.

@Y-Less Y-Less closed this as completed Sep 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants