From 570c6c8458e6bc14b08bf99be289b9bb2efd8f58 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 20 Oct 2019 13:55:57 +0200 Subject: [PATCH] LibM: Make roundf() and ceilf() slightly less terrible These implementations still don't handle all of the corner cases that are possible, but at least they are somewhat usable now. --- Libraries/LibM/math.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index 012d3e0ff8..51af8796ff 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -276,14 +276,20 @@ long double frexpl(long double, int*) float roundf(float value) { - // FIXME: Please fix me. I am sad. - return (int)value; + // FIXME: Please fix me. I am naive. + if (value >= 0.0f) + return (float)(int)(value + 0.5f); + return (float)(int)(value - 0.5f); } float ceilf(float value) { - // FIXME: Please fix me. I am sad. - return (int)value; + // FIXME: Please fix me. I am naive. + int as_int = (int)value; + if (value == (float)as_int) { + return (float)as_int; + } + return as_int + 1; } }