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 2020/08/14 12:28:07 UTC

[incubator-nuttx] 01/02: math: Implement cbrt

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

commit 57e1211ed6ec4bfcc7171149c7e9629272496f26
Author: Huang Qi <hu...@xiaomi.com>
AuthorDate: Wed Aug 12 18:03:00 2020 +0800

    math: Implement cbrt
    
    Signed-off-by: Huang Qi <hu...@xiaomi.com>
---
 include/nuttx/lib/math.h  |  4 ++++
 libs/libc/math/Make.defs  |  2 +-
 libs/libc/math/lib_cbrt.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/include/nuttx/lib/math.h b/include/nuttx/lib/math.h
index 2fa3a4d..74f223a 100644
--- a/include/nuttx/lib/math.h
+++ b/include/nuttx/lib/math.h
@@ -372,6 +372,10 @@ double      cosh  (double x);
 long double coshl (long double x);
 #endif
 
+#ifdef CONFIG_HAVE_DOUBLE
+double      cbrt  (double x);
+#endif
+
 float       tanhf (float x);
 #ifdef CONFIG_HAVE_DOUBLE
 double      tanh  (double x);
diff --git a/libs/libc/math/Make.defs b/libs/libc/math/Make.defs
index 8a41bd5..087ea14 100644
--- a/libs/libc/math/Make.defs
+++ b/libs/libc/math/Make.defs
@@ -49,7 +49,7 @@ CSRCS += lib_cosh.c lib_exp.c lib_fabs.c lib_fmod.c lib_frexp.c
 CSRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c
 CSRCS += lib_pow.c lib_sin.c lib_sinh.c lib_sqrt.c lib_tan.c
 CSRCS += lib_tanh.c lib_asinh.c lib_acosh.c lib_atanh.c lib_erf.c
-CSRCS += lib_copysign.c
+CSRCS += lib_copysign.c lib_cbrt.c
 
 CSRCS += lib_acosl.c lib_asinl.c lib_atan2l.c lib_atanl.c lib_ceill.c
 CSRCS += lib_cosl.c lib_coshl.c lib_expl.c lib_fabsl.c lib_floorl.c
diff --git a/libs/libc/math/lib_cbrt.c b/libs/libc/math/lib_cbrt.c
new file mode 100644
index 0000000..cf28b94
--- /dev/null
+++ b/libs/libc/math/lib_cbrt.c
@@ -0,0 +1,55 @@
+/****************************************************************************
+ * libs/libc/math/lib_cbrt.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <sys/types.h>
+#include <math.h>
+#include <float.h>
+
+#ifdef CONFIG_HAVE_DOUBLE
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+double cbrt(double x)
+{
+  if (fabs(x) < DBL_EPSILON)
+    {
+      return 0.0;
+    }
+
+  if (x > 0.0)
+    {
+      return pow(x, 1.0 / 3.0);
+    }
+  else
+    {
+      return -pow(-x, 1.0 / 3.0);
+    }
+}
+
+#endif
\ No newline at end of file