diff liboctave/lo-specfun.cc @ 10414:2a8b1db1e2ca

implement built-in cbrt
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 16 Mar 2010 15:16:32 +0100
parents 59e34bcdff13
children 4d1fc073fbb7
line wrap: on
line diff
--- a/liboctave/lo-specfun.cc	Tue Mar 16 12:29:49 2010 +0100
+++ b/liboctave/lo-specfun.cc	Tue Mar 16 15:16:32 2010 +0100
@@ -560,6 +560,22 @@
   return retval;
 }
 
+#if !defined (HAVE_CBRT)
+double cbrt (double x)
+{
+  static const double one_third = 0.3333333333333333333;
+  if (xfinite (x))
+    {
+      // Use pow.
+      double y = std::pow (std::abs (x), one_third) * signum (x);
+      // Correct for better accuracy.
+      return (x / (y*y) + y + y) / 3;
+    }
+  else
+    return x;
+}
+#endif
+
 #if !defined (HAVE_LOG1PF)
 float
 log1pf (float x)
@@ -603,6 +619,22 @@
   return retval;
 }
 
+#if !defined (HAVE_CBRTF)
+float cbrtf (float x)
+{
+  static const float one_third = 0.3333333333333333333f;
+  if (xfinite (x))
+    {
+      // Use pow.
+      float y = std::pow (std::abs (x), one_third) * signum (x);
+      // Correct for better accuracy.
+      return (x / (y*y) + y + y) / 3;
+    }
+  else
+    return x;
+}
+#endif
+
 static inline Complex
 zbesj (const Complex& z, double alpha, int kode, octave_idx_type& ierr);