home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / ldouble / cbrtl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-17  |  2.3 KB  |  122 lines

  1. /*                            cbrtl.c
  2.  *
  3.  *    Cube root, long double precision
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * long double x, y, cbrtl();
  10.  *
  11.  * y = cbrtl( x );
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * Returns the cube root of the argument, which may be negative.
  18.  *
  19.  * Range reduction involves determining the power of 2 of
  20.  * the argument.  A polynomial of degree 2 applied to the
  21.  * mantissa, and multiplication by the cube root of 1, 2, or 4
  22.  * approximates the root to within about 0.1%.  Then Newton's
  23.  * iteration is used three times to converge to an accurate
  24.  * result.
  25.  *
  26.  *
  27.  *
  28.  * ACCURACY:
  29.  *
  30.  *                      Relative error:
  31.  * arithmetic   domain     # trials      peak         rms
  32.  *    IEEE     .125,8        80000      7.0e-20     2.2e-20
  33.  *    IEEE    exp(+-707)    100000      7.0e-20     2.4e-20
  34.  *
  35.  */
  36.  
  37.  
  38. /*
  39. Cephes Math Library Release 2.2: January, 1991
  40. Copyright 1984, 1991 by Stephen L. Moshier
  41. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  42. */
  43.  
  44.  
  45. #include "mconf.h"
  46.  
  47. static long double CBRT2  = 1.2599210498948731647672L;
  48. static long double CBRT4  = 1.5874010519681994747517L;
  49. static long double CBRT2I = 0.79370052598409973737585L;
  50. static long double CBRT4I = 0.62996052494743658238361L;
  51.  
  52.  
  53. long double cbrtl(x)
  54. long double x;
  55. {
  56. int e, rem, sign;
  57. long double z;
  58. long double frexpl(), ldexpl();
  59.  
  60. if( x == 0 )
  61.     return( 0.0L );
  62. if( x > 0 )
  63.     sign = 1;
  64. else
  65.     {
  66.     sign = -1;
  67.     x = -x;
  68.     }
  69.  
  70. z = x;
  71. /* extract power of 2, leaving
  72.  * mantissa between 0.5 and 1
  73.  */
  74. x = frexpl( x, &e );
  75.  
  76. /* Approximate cube root of number between .5 and 1,
  77.  * peak relative error = 1.2e-6
  78.  */
  79. x = (((( 1.3584464340920900529734e-1L * x
  80.        - 6.3986917220457538402318e-1L) * x
  81.        + 1.2875551670318751538055e0L) * x
  82.        - 1.4897083391357284957891e0L) * x
  83.        + 1.3304961236013647092521e0L) * x
  84.        + 3.7568280825958912391243e-1L;
  85.  
  86. /* exponent divided by 3 */
  87. if( e >= 0 )
  88.     {
  89.     rem = e;
  90.     e /= 3;
  91.     rem -= 3*e;
  92.     if( rem == 1 )
  93.         x *= CBRT2;
  94.     else if( rem == 2 )
  95.         x *= CBRT4;
  96.     }
  97. else
  98.     { /* argument less than 1 */
  99.     e = -e;
  100.     rem = e;
  101.     e /= 3;
  102.     rem -= 3*e;
  103.     if( rem == 1 )
  104.         x *= CBRT2I;
  105.     else if( rem == 2 )
  106.         x *= CBRT4I;
  107.     e = -e;
  108.     }
  109.  
  110. /* multiply by power of 2 */
  111. x = ldexpl( x, e );
  112.  
  113. /* Newton iteration */
  114.  
  115. x -= ( x - (z/(x*x)) )*0.3333333333333333333333L;
  116. x -= ( x - (z/(x*x)) )*0.3333333333333333333333L;
  117.  
  118. if( sign < 0 )
  119.     x = -x;
  120. return(x);
  121. }
  122.