diff --git a/include/nuttx/math.h b/include/nuttx/math.h index 11b289ed5c..281b87ed50 100644 --- a/include/nuttx/math.h +++ b/include/nuttx/math.h @@ -358,6 +358,17 @@ double atanh (double x); long double atanhl (long double x); #endif +float erff (float x); +#define erfcf(x) (1 - erff(x)) +#if CONFIG_HAVE_DOUBLE +double erf (double x); +#define erfc(x) (1 - erf(x)) +#endif +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double erfl (long double x); +#define erfcl(x) (1 - erfl(x)) +#endif + #if defined(__cplusplus) } #endif diff --git a/libc/math/Make.defs b/libc/math/Make.defs index b490acb02a..0e40744a0f 100644 --- a/libc/math/Make.defs +++ b/libc/math/Make.defs @@ -41,19 +41,19 @@ CSRCS += lib_acosf.c lib_asinf.c lib_atan2f.c lib_atanf.c lib_ceilf.c lib_cosf.c CSRCS += lib_coshf.c lib_expf.c lib_fabsf.c lib_floorf.c lib_fmodf.c lib_frexpf.c CSRCS += lib_ldexpf.c lib_logf.c lib_log10f.c lib_log2f.c lib_modff.c lib_powf.c CSRCS += lib_rintf.c lib_roundf.c lib_sinf.c lib_sinhf.c lib_sqrtf.c lib_tanf.c -CSRCS += lib_tanhf.c lib_asinhf.c lib_acoshf.c lib_atanhf.c +CSRCS += lib_tanhf.c lib_asinhf.c lib_acoshf.c lib_atanhf.c lib_erff.c CSRCS += lib_acos.c lib_asin.c lib_atan.c lib_atan2.c lib_ceil.c lib_cos.c CSRCS += lib_cosh.c lib_exp.c lib_fabs.c lib_floor.c lib_fmod.c lib_frexp.c CSRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c lib_pow.c CSRCS += lib_rint.c lib_round.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 +CSRCS += lib_tanh.c lib_asinh.c lib_acosh.c lib_atanh.c lib_erf.c CSRCS += lib_acosl.c lib_asinl.c lib_atan2l.c lib_atanl.c lib_ceill.c lib_cosl.c CSRCS += lib_coshl.c lib_expl.c lib_fabsl.c lib_floorl.c lib_fmodl.c lib_frexpl.c CSRCS += lib_ldexpl.c lib_logl.c lib_log10l.c lib_log2l.c lib_modfl.c lib_powl.c CSRCS += lib_rintl.c lib_roundl.c lib_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c -CSRCS += lib_tanhl.c lib_asinhl.c lib_acoshl.c lib_atanhl.c +CSRCS += lib_tanhl.c lib_asinhl.c lib_acoshl.c lib_atanhl.c lib_erfl.c CSRCS += lib_libexpi.c lib_libsqrtapprox.c diff --git a/libc/math/lib_erf.c b/libc/math/lib_erf.c new file mode 100644 index 0000000000..a410170161 --- /dev/null +++ b/libc/math/lib_erf.c @@ -0,0 +1,72 @@ +/************************************************************************ + * libc/math/lib_erf.c + * + * Copyright (C) 2015 Brennan Ashton. All rights reserved. + * Author: Brennan Ashton + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_DOUBLE +double erf(double x) +{ + /* This implementation comes from the Handbook of Mathmatical Functions + * The implementations in this book are not protected by copyright. + * erf comes from formula 7.1.26 + */ + + char sign; + double t; + double a1, a2, a3, a4, a5, p; + + a1 = 0.254829592; + a2 = -0.284496736; + a3 = 1.421413741; + a4 = -1.453152027; + a5 = 1.061405429; + p = 0.3275911; + + sign = (x >= 0 ? 1 : -1); + t = 1.0/(1.0 + p*x); + return sign * (1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * expf(-x * x)); +} +#endif diff --git a/libc/math/lib_erff.c b/libc/math/lib_erff.c new file mode 100644 index 0000000000..4e773320a1 --- /dev/null +++ b/libc/math/lib_erff.c @@ -0,0 +1,70 @@ +/************************************************************************ + * libc/math/lib_erff.c + * + * Copyright (C) 2015 Brennan Ashton. All rights reserved. + * Author: Brennan Ashton + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float erff(float x) +{ + /* This implementation comes from the Handbook of Mathmatical Functions + * The implementations in this book are not protected by copyright. + * erf comes from formula 7.1.26 + */ + + char sign; + float t; + float a1, a2, a3, a4, a5, p; + + a1 = 0.254829592; + a2 = -0.284496736; + a3 = 1.421413741; + a4 = -1.453152027; + a5 = 1.061405429; + p = 0.3275911; + + sign = (x >= 0 ? 1 : -1); + t = 1.0/(1.0 + p*x); + return sign * (1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * expf(-x * x)); +} diff --git a/libc/math/lib_erfl.c b/libc/math/lib_erfl.c new file mode 100644 index 0000000000..594e9183bc --- /dev/null +++ b/libc/math/lib_erfl.c @@ -0,0 +1,72 @@ +/************************************************************************ + * libc/math/lib_erfl.c + * + * Copyright (C) 2015 Brennan Ashton. All rights reserved. + * Author: Brennan Ashton + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include +#include + +#include + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double erfl(long double x) +{ + /* This implementation comes from the Handbook of Mathmatical Functions + * The implementations in this book are not protected by copyright. + * erf comes from formula 7.1.26 + */ + + char sign; + long double t; + long double a1, a2, a3, a4, a5, p; + + a1 = 0.254829592; + a2 = -0.284496736; + a3 = 1.421413741; + a4 = -1.453152027; + a5 = 1.061405429; + p = 0.3275911; + + sign = (x >= 0 ? 1 : -1); + t = 1.0/(1.0 + p*x); + return sign * (1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * expf(-x * x)); +} +#endif