You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/09/03 13:19:14 UTC

[incubator-nuttx] branch master updated: Fixed pow() for negative bases.

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 985710665a Fixed pow() for negative bases.
985710665a is described below

commit 985710665a62e886694cf87f00be08fdf38e4702
Author: Fotis Panagiotopoulos <f....@amco.gr>
AuthorDate: Fri Sep 2 18:50:54 2022 +0300

    Fixed pow() for negative bases.
---
 libs/libc/math/lib_pow.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/libs/libc/math/lib_pow.c b/libs/libc/math/lib_pow.c
index c918479ea3..e82e181eb2 100644
--- a/libs/libc/math/lib_pow.c
+++ b/libs/libc/math/lib_pow.c
@@ -41,6 +41,22 @@
 #ifdef CONFIG_HAVE_DOUBLE
 double pow(double b, double e)
 {
-  return exp(e * log(b));
+  if (b > 0)
+    {
+      return exp(e * log(b));
+    }
+  else if (b < 0 && e == (int)e)
+    {
+      if ((int)e % 2 == 0)
+        {
+          return exp(e * log(fabs(b)));
+        }
+      else
+        {
+          return -exp(e * log(fabs(b)));
+        }
+    }
+
+  return 0;
 }
 #endif