From 8462b14d4e8dc9b164d9f14e35a8a3f64c516a09 Mon Sep 17 00:00:00 2001 From: Petro Karashchenko Date: Thu, 30 Dec 2021 21:21:45 +0200 Subject: [PATCH] libc/math: fix fmod family operation Signed-off-by: Petro Karashchenko --- libs/libc/math/lib_fmod.c | 6 ++---- libs/libc/math/lib_fmodf.c | 6 ++---- libs/libc/math/lib_fmodl.c | 6 ++---- libs/libc/math/lib_modf.c | 4 ++-- libs/libc/math/lib_modff.c | 2 +- libs/libc/math/lib_modfl.c | 4 ++-- 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/libs/libc/math/lib_fmod.c b/libs/libc/math/lib_fmod.c index c321086aeb..7f6a789f91 100644 --- a/libs/libc/math/lib_fmod.c +++ b/libs/libc/math/lib_fmod.c @@ -43,10 +43,8 @@ double fmod(double x, double div) { double n0; - x /= div; - x = modf(x, &n0); - x *= div; + modf(x / div, &n0); - return x; + return x - n0 * div; } #endif diff --git a/libs/libc/math/lib_fmodf.c b/libs/libc/math/lib_fmodf.c index 9db337688d..b907af9f52 100644 --- a/libs/libc/math/lib_fmodf.c +++ b/libs/libc/math/lib_fmodf.c @@ -39,9 +39,7 @@ float fmodf(float x, float div) { float n0; - x /= div; - x = modff(x, &n0); - x *= div; + modff(x / div, &n0); - return x; + return x - n0 * div; } diff --git a/libs/libc/math/lib_fmodl.c b/libs/libc/math/lib_fmodl.c index eb2b663330..593ba03d9a 100644 --- a/libs/libc/math/lib_fmodl.c +++ b/libs/libc/math/lib_fmodl.c @@ -43,10 +43,8 @@ long double fmodl(long double x, long double div) { long double n0; - x /= div; - x = modfl(x, &n0); - x *= div; + modfl(x / div, &n0); - return x; + return x - n0 * div; } #endif diff --git a/libs/libc/math/lib_modf.c b/libs/libc/math/lib_modf.c index bbd45a1656..e1ac235508 100644 --- a/libs/libc/math/lib_modf.c +++ b/libs/libc/math/lib_modf.c @@ -46,12 +46,12 @@ double modf(double x, double *iptr) } else if (fabs(x) < 1.0) { - *iptr = (x * 0.0); + *iptr = 0.0; return x; } else { - *iptr = (double)(int64_t) x; + *iptr = (double)(int64_t)x; return (x - *iptr); } } diff --git a/libs/libc/math/lib_modff.c b/libs/libc/math/lib_modff.c index 8757c3ae01..7c068509cd 100644 --- a/libs/libc/math/lib_modff.c +++ b/libs/libc/math/lib_modff.c @@ -44,7 +44,7 @@ float modff(float x, float *iptr) } else if (fabsf(x) < 1.0F) { - *iptr = (x * 0.0F); + *iptr = 0.0F; return x; } else diff --git a/libs/libc/math/lib_modfl.c b/libs/libc/math/lib_modfl.c index b28f2aa124..49fd4f1128 100644 --- a/libs/libc/math/lib_modfl.c +++ b/libs/libc/math/lib_modfl.c @@ -49,12 +49,12 @@ long double modfl(long double x, long double *iptr) } else if (fabsl(x) < 1.0) { - *iptr = (x * 0.0); + *iptr = 0.0; return x; } else { - *iptr = (long double)(int64_t) x; + *iptr = (long double)(int64_t)x; return (x - *iptr); } }