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
commit c7b4b5fe00
Notes: sideshowbarker 2024-07-19 07:51:07 +09:00

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;
} }