From c7b4b5fe001136d13331d4a9aa63b4b43d20c452 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Apr 2020 14:23:09 +0100 Subject: [PATCH] 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. --- Libraries/LibM/math.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index a43a156612..b3c89e4a26 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -384,8 +384,12 @@ float ceilf(float value) { // FIXME: Please fix me. I am naive. int as_int = (int)value; - if (value == (float)as_int) { - return (float)as_int; + if (value == (float)as_int) + return as_int; + if (value < 0) { + if (as_int == 0) + return -0; + return as_int; } return as_int + 1; } @@ -394,8 +398,12 @@ double ceil(double value) { // FIXME: Please fix me. I am naive. int as_int = (int)value; - if (value == (double)as_int) { - return (double)as_int; + if (value == (double)as_int) + return as_int; + if (value < 0) { + if (as_int == 0) + return -0; + return as_int; } return as_int + 1; }