Back to index

glibc  2.9
s_cbrt.c
Go to the documentation of this file.
00001 /* Compute cubic root of double value.
00002    Copyright (C) 1997 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Dirk Alboth <dirka@uni-paderborn.de> and
00005    Ulrich Drepper <drepper@cygnus.com>, 1997.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include "math.h"
00023 #include "math_private.h"
00024 
00025 
00026 #define CBRT2 1.2599210498948731648              /* 2^(1/3) */
00027 #define SQR_CBRT2 1.5874010519681994748          /* 2^(2/3) */
00028 
00029 static const double factor[5] =
00030 {
00031   1.0 / SQR_CBRT2,
00032   1.0 / CBRT2,
00033   1.0,
00034   CBRT2,
00035   SQR_CBRT2
00036 };
00037 
00038 
00039 double
00040 __cbrt (double x)
00041 {
00042   double xm, ym, u, t2;
00043   int xe;
00044 
00045   /* Reduce X.  XM now is an range 1.0 to 0.5.  */
00046   xm = __frexp (fabs (x), &xe);
00047 
00048   /* If X is not finite or is null return it (with raising exceptions
00049      if necessary.
00050      Note: *Our* version of `frexp' sets XE to zero if the argument is
00051      Inf or NaN.  This is not portable but faster.  */
00052   if (xe == 0 && fpclassify (x) <= FP_ZERO)
00053     return x + x;
00054 
00055  u = (0.354895765043919860
00056       + ((1.50819193781584896
00057         + ((-2.11499494167371287
00058            + ((2.44693122563534430
00059               + ((-1.83469277483613086
00060                 + (0.784932344976639262 - 0.145263899385486377 * xm) * xm)
00061                 * xm))
00062               * xm))
00063            * xm))
00064         * xm));
00065 
00066   t2 = u * u * u;
00067 
00068   ym = u * (t2 + 2.0 * xm) / (2.0 * t2 + xm) * factor[2 + xe % 3];
00069 
00070   return __ldexp (x > 0.0 ? ym : -ym, xe / 3);
00071 }
00072 weak_alias (__cbrt, cbrt)
00073 #ifdef NO_LONG_DOUBLE
00074 strong_alias (__cbrt, __cbrtl)
00075 weak_alias (__cbrt, cbrtl)
00076 #endif