Back to index

glibc  2.9
s_cbrtf.c
Go to the documentation of this file.
00001 /* Compute cubic root of float 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 float
00040 __cbrtf (float x)
00041 {
00042   float xm, ym, u, t2;
00043   int xe;
00044 
00045   /* Reduce X.  XM now is an range 1.0 to 0.5.  */
00046   xm = __frexpf (fabsf (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.492659620528969547 + (0.697570460207922770
00056                             - 0.191502161678719066 * xm) * xm);
00057 
00058   t2 = u * u * u;
00059 
00060   ym = u * (t2 + 2.0 * xm) / (2.0 * t2 + xm) * factor[2 + xe % 3];
00061 
00062   return __ldexpf (x > 0.0 ? ym : -ym, xe / 3);
00063 }
00064 weak_alias (__cbrtf, cbrtf)