From 7cd38dca6a3e636131797266b43b980280999789 Mon Sep 17 00:00:00 2001 From: yanghuatao Date: Fri, 15 Dec 2023 14:35:20 +0800 Subject: [PATCH] nuttx/libm: Fix powf wrong if first parameter is negative Signed-off-by: yanghuatao --- libs/libm/libm/lib_powf.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/libs/libm/libm/lib_powf.c b/libs/libm/libm/lib_powf.c index 27d5f30d49..60e7b33fb6 100644 --- a/libs/libm/libm/lib_powf.c +++ b/libs/libm/libm/lib_powf.c @@ -37,5 +37,21 @@ float powf(float b, float e) { - return expf(e * logf(b)); + if (b > 0.0) + { + return expf(e * logf(b)); + } + else if (b < 0.0 && e == (int)e) + { + if ((int)e % 2 == 0) + { + return expf(e * logf(fabsf(b))); + } + else + { + return -expf(e * logf(fabsf(b))); + } + } + + return 0.0; }