1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:07:34 +00:00

LibM: Fix ceil() and ceilf() for negative numbers

These functions are using a naive approach: casting double/float to int
and returning the result + 1. That increment by one must only happen for
positive input values though.
This commit is contained in:
Linus Groh 2020-04-06 14:23:09 +01:00 committed by Andreas Kling
parent 7cda0b9027
commit c7b4b5fe00

View file

@ -384,8 +384,12 @@ float ceilf(float value)
{ {
// FIXME: Please fix me. I am naive. // FIXME: Please fix me. I am naive.
int as_int = (int)value; int as_int = (int)value;
if (value == (float)as_int) { if (value == (float)as_int)
return (float)as_int; return as_int;
if (value < 0) {
if (as_int == 0)
return -0;
return as_int;
} }
return as_int + 1; return as_int + 1;
} }
@ -394,8 +398,12 @@ double ceil(double value)
{ {
// FIXME: Please fix me. I am naive. // FIXME: Please fix me. I am naive.
int as_int = (int)value; int as_int = (int)value;
if (value == (double)as_int) { if (value == (double)as_int)
return (double)as_int; return as_int;
if (value < 0) {
if (as_int == 0)
return -0;
return as_int;
} }
return as_int + 1; return as_int + 1;
} }