From 57e1211ed6ec4bfcc7171149c7e9629272496f26 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Wed, 12 Aug 2020 18:03:00 +0800 Subject: [PATCH] math: Implement cbrt Signed-off-by: Huang Qi --- 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(-) create mode 100644 libs/libc/math/lib_cbrt.c diff --git a/include/nuttx/lib/math.h b/include/nuttx/lib/math.h index 2fa3a4d25e..74f223a946 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 8a41bd5fe4..087ea14792 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 0000000000..cf28b94669 --- /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 +#include + +#include +#include +#include + +#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