Kai Tietz | 53b69ff | 2007-08-20 13:49:15 +0000 | [diff] [blame] | 1 | /**
|
| 2 | * This file has no copyright assigned and is placed in the Public Domain.
|
| 3 | * This file is part of the w64 mingw-runtime package.
|
| 4 | * No warranty is given; refer to the file DISCLAIMER within this package.
|
| 5 | */
|
Kai Tietz | 518dd33 | 2007-08-10 09:54:15 +0000 | [diff] [blame] | 6 | #include "cephes_mconf.h"
|
| 7 |
|
| 8 | static const long double CBRT2 = 1.2599210498948731647672L;
|
| 9 | static const long double CBRT4 = 1.5874010519681994747517L;
|
| 10 | static const long double CBRT2I = 0.79370052598409973737585L;
|
| 11 | static const long double CBRT4I = 0.62996052494743658238361L;
|
| 12 |
|
Kai Tietz | 3c6bbdb | 2007-09-10 12:53:13 +0000 | [diff] [blame] | 13 | extern long double ldexpl(long double,int);
|
| 14 |
|
Kai Tietz | 518dd33 | 2007-08-10 09:54:15 +0000 | [diff] [blame] | 15 | long double cbrtl(x)
|
| 16 | long double x;
|
| 17 | {
|
| 18 | int e, rem, sign;
|
| 19 | long double z;
|
| 20 |
|
| 21 | if (!isfinite (x) || x == 0.0L)
|
| 22 | return(x);
|
| 23 |
|
| 24 | if( x > 0 )
|
| 25 | sign = 1;
|
| 26 | else
|
| 27 | {
|
| 28 | sign = -1;
|
| 29 | x = -x;
|
| 30 | }
|
| 31 |
|
| 32 | z = x;
|
| 33 | /* extract power of 2, leaving
|
| 34 | * mantissa between 0.5 and 1
|
| 35 | */
|
| 36 | x = frexpl( x, &e );
|
| 37 |
|
| 38 | /* Approximate cube root of number between .5 and 1,
|
| 39 | * peak relative error = 1.2e-6
|
| 40 | */
|
| 41 | x = (((( 1.3584464340920900529734e-1L * x
|
| 42 | - 6.3986917220457538402318e-1L) * x
|
| 43 | + 1.2875551670318751538055e0L) * x
|
| 44 | - 1.4897083391357284957891e0L) * x
|
| 45 | + 1.3304961236013647092521e0L) * x
|
| 46 | + 3.7568280825958912391243e-1L;
|
| 47 |
|
| 48 | /* exponent divided by 3 */
|
| 49 | if( e >= 0 )
|
| 50 | {
|
| 51 | rem = e;
|
| 52 | e /= 3;
|
| 53 | rem -= 3*e;
|
| 54 | if( rem == 1 )
|
| 55 | x *= CBRT2;
|
| 56 | else if( rem == 2 )
|
| 57 | x *= CBRT4;
|
| 58 | }
|
| 59 | else
|
| 60 | { /* argument less than 1 */
|
| 61 | e = -e;
|
| 62 | rem = e;
|
| 63 | e /= 3;
|
| 64 | rem -= 3*e;
|
| 65 | if( rem == 1 )
|
| 66 | x *= CBRT2I;
|
| 67 | else if( rem == 2 )
|
| 68 | x *= CBRT4I;
|
| 69 | e = -e;
|
| 70 | }
|
| 71 |
|
| 72 | /* multiply by power of 2 */
|
| 73 | x = ldexpl( x, e );
|
| 74 |
|
| 75 | /* Newton iteration */
|
| 76 |
|
| 77 | x -= ( x - (z/(x*x)) )*0.3333333333333333333333L;
|
| 78 | x -= ( x - (z/(x*x)) )*0.3333333333333333333333L;
|
| 79 |
|
| 80 | if( sign < 0 )
|
| 81 | x = -x;
|
| 82 | return(x);
|
| 83 | }
|