Skip to content

Incorrect floatfract behaviour at negative values. #150

Closed
@MuthaX

Description

@MuthaX

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions