summaryrefslogtreecommitdiff
path: root/lib/cbrt.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2012-02-29 23:42:53 +0100
committerBruno Haible <bruno@clisp.org>2012-02-29 23:42:53 +0100
commita0dccde75e6a3b9c930e3577fae7a5bf9351ec09 (patch)
treecf07ebc2e9ae200885f4d1bea63f01d712b37d3b /lib/cbrt.c
parent14ad442f598655b0dad323d32c22e8b27ad6fb5c (diff)
downloadgnulib-a0dccde75e6a3b9c930e3577fae7a5bf9351ec09.tar.gz
cbrt: Provide replacement on MSVC and Minix.
* lib/math.in.h (cbrt): New declaration. * lib/cbrt.c: New file. * m4/cbrt.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whether cbrt is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_CBRT, HAVE_CBRT. * modules/math (Makefile.am): Substitute GNULIB_CBRT, HAVE_CBRT. * modules/cbrt (Files): Add lib/cbrt.c, m4/cbrt.m4. (Depends-on): Add dependencies. (configure.ac): Arrange to compile replacement if HAVE_CBRT is 0. * tests/test-math-c++.cc: Check the declaration of cbrt. * doc/posix-functions/cbrt.texi: Mention that the module provides a replacement.
Diffstat (limited to 'lib/cbrt.c')
-rw-r--r--lib/cbrt.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/cbrt.c b/lib/cbrt.c
new file mode 100644
index 0000000000..d3659f0a6f
--- /dev/null
+++ b/lib/cbrt.c
@@ -0,0 +1,71 @@
+/* Compute cubic root of double value.
+ Copyright (C) 1997, 2012 Free Software Foundation, Inc.
+
+ Contributed by Dirk Alboth <dirka@uni-paderborn.de> and
+ Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <math.h>
+
+/* Code based on glibc/sysdeps/ieee754/dbl-64/s_cbrt.c. */
+
+#define CBRT2 1.2599210498948731648 /* 2^(1/3) */
+#define SQR_CBRT2 1.5874010519681994748 /* 2^(2/3) */
+
+static const double factor[5] =
+{
+ 1.0 / SQR_CBRT2,
+ 1.0 / CBRT2,
+ 1.0,
+ CBRT2,
+ SQR_CBRT2
+};
+
+
+double
+cbrt (double x)
+{
+ if (isfinite (x) && x != 0.0)
+ {
+ double xm, ym, u, t2;
+ int xe;
+
+ /* Reduce X. XM now is an range 1.0 to 0.5. */
+ xm = frexp (fabs (x), &xe);
+
+ u = (0.354895765043919860
+ + ((1.50819193781584896
+ + ((-2.11499494167371287
+ + ((2.44693122563534430
+ + ((-1.83469277483613086
+ + (0.784932344976639262 - 0.145263899385486377 * xm)
+ * xm)
+ * xm))
+ * xm))
+ * xm))
+ * xm));
+
+ t2 = u * u * u;
+
+ ym = u * (t2 + 2.0 * xm) / (2.0 * t2 + xm) * factor[2 + xe % 3];
+
+ return ldexp (x > 0.0 ? ym : -ym, xe / 3);
+ }
+ else
+ return x + x;
+}