Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*                                                      cbrtf.c
  2.  *
  3.  *      Cube root
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * float x, y, cbrtf();
  10.  *
  11.  * y = cbrtf( 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 to converge to an accurate result.
  24.  *
  25.  *
  26.  *
  27.  * ACCURACY:
  28.  *
  29.  *                      Relative error:
  30.  * arithmetic   domain     # trials      peak         rms
  31.  *    IEEE      0,1e38      100000      7.6e-8      2.7e-8
  32.  *
  33.  */
  34. /*                                                     cbrt.c  */
  35.  
  36. /*
  37. Cephes Math Library Release 2.2:  June, 1992
  38. Copyright 1984, 1987, 1988, 1992 by Stephen L. Moshier
  39. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  40. */
  41.  
  42. /*
  43.   Modified for mingwex.a
  44.   2002-07-01  Danny Smith  <dannysmith@users.sourceforge.net>
  45.  */
  46. #ifdef __MINGW32__
  47. #include <math.h>
  48. #include "cephes_mconf.h"
  49. #else
  50. #include "mconf.h"
  51. #endif
  52.  
  53. static const float CBRT2 = 1.25992104989487316477;
  54. static const float CBRT4 = 1.58740105196819947475;
  55.  
  56. #ifndef __MINGW32__
  57. #ifdef ANSIC
  58. float frexpf(float, int *), ldexpf(float, int);
  59.  
  60. float cbrtf( float xx )
  61. #else
  62. float frexpf(), ldexpf();
  63.  
  64. float cbrtf(xx)
  65. double xx;
  66. #endif
  67. {
  68. int e, rem, sign;
  69. float x, z;
  70.  
  71. x = xx;
  72.  
  73. #else /* __MINGW32__ */
  74. float cbrtf (float x)
  75. {
  76. int e, rem, sign;
  77. float z;
  78. #endif /* __MINGW32__ */
  79.  
  80. #ifdef __MINGW32__
  81. if (!isfinite (x) || x == 0.0F )
  82.   return x;
  83. #else
  84. if( x == 0 )
  85.         return( 0.0 );
  86. #endif
  87. if( x > 0 )
  88.         sign = 1;
  89. else
  90.         {
  91.         sign = -1;
  92.         x = -x;
  93.         }
  94.  
  95. z = x;
  96. /* extract power of 2, leaving
  97.  * mantissa between 0.5 and 1
  98.  */
  99. x = frexpf( x, &e );
  100.  
  101. /* Approximate cube root of number between .5 and 1,
  102.  * peak relative error = 9.2e-6
  103.  */
  104. x = (((-0.13466110473359520655053  * x
  105.       + 0.54664601366395524503440 ) * x
  106.       - 0.95438224771509446525043 ) * x
  107.       + 1.1399983354717293273738  ) * x
  108.       + 0.40238979564544752126924;
  109.  
  110. /* exponent divided by 3 */
  111. if( e >= 0 )
  112.         {
  113.         rem = e;
  114.         e /= 3;
  115.         rem -= 3*e;
  116.         if( rem == 1 )
  117.                 x *= CBRT2;
  118.         else if( rem == 2 )
  119.                 x *= CBRT4;
  120.         }
  121.  
  122.  
  123. /* argument less than 1 */
  124.  
  125. else
  126.         {
  127.         e = -e;
  128.         rem = e;
  129.         e /= 3;
  130.         rem -= 3*e;
  131.         if( rem == 1 )
  132.                 x /= CBRT2;
  133.         else if( rem == 2 )
  134.                 x /= CBRT4;
  135.         e = -e;
  136.         }
  137.  
  138. /* multiply by power of 2 */
  139. x = ldexpf( x, e );
  140.  
  141. /* Newton iteration */
  142. x -= ( x - (z/(x*x)) ) * 0.333333333333;
  143.  
  144. if( sign < 0 )
  145.         x = -x;
  146. return(x);
  147. }
  148.