diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-15 22:05:59 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-15 22:05:59 +0200 |
commit | de7827faf7decb93268c55da022721b6e2cf0904 (patch) | |
tree | ef8d4d9c753de1afdebd04994401ae70cfeb6dc5 /Libraries/LibM | |
parent | 8f293b7543d58a2685dcfd2c2ebc2708eae70932 (diff) | |
download | serenity-de7827faf7decb93268c55da022721b6e2cf0904.zip |
LibM: Fix floor() and floorf() for negative numbers
And add a LibJS test to exercise the code. :^)
Diffstat (limited to 'Libraries/LibM')
-rw-r--r-- | Libraries/LibM/math.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index 38d9edec1d..021993e9d4 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -412,12 +412,18 @@ float roundf(float value) float floorf(float value) { - return (int)value; + if (value >= 0) + return (int)value; + int intvalue = (int)value; + return ((float)intvalue == value) ? intvalue : intvalue - 1; } double floor(double value) { - return (int)value; + if (value >= 0) + return (int)value; + int intvalue = (int)value; + return ((double)intvalue == value) ? intvalue : intvalue - 1; } double rint(double value) |