hppa64: Fix fmt_f_default_field_width_3.f90 and fmt_g_default_field_width_3.f90
[official-gcc.git] / libquadmath / math / cbrtq.c
blob5b8c30ea763c2c5fec696a93325a490401eeb917
1 /* cbrtq.c
3 * Cube root, long double precision
7 * SYNOPSIS:
9 * long double x, y, cbrtq();
11 * y = cbrtq( x );
15 * DESCRIPTION:
17 * Returns the cube root of the argument, which may be negative.
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.
28 * ACCURACY:
30 * Relative error:
31 * arithmetic domain # trials peak rms
32 * IEEE -8,8 100000 1.3e-34 3.9e-35
33 * IEEE exp(+-707) 100000 1.3e-34 4.3e-35
38 Cephes Math Library Release 2.2: January, 1991
39 Copyright 1984, 1991 by Stephen L. Moshier
40 Adapted for glibc October, 2001.
42 This library is free software; you can redistribute it and/or
43 modify it under the terms of the GNU Lesser General Public
44 License as published by the Free Software Foundation; either
45 version 2.1 of the License, or (at your option) any later version.
47 This library is distributed in the hope that it will be useful,
48 but WITHOUT ANY WARRANTY; without even the implied warranty of
49 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 Lesser General Public License for more details.
52 You should have received a copy of the GNU Lesser General Public
53 License along with this library; if not, see
54 <http://www.gnu.org/licenses/>. */
56 #include "quadmath-imp.h"
58 static const __float128 CBRT2 = 1.259921049894873164767210607278228350570251Q;
59 static const __float128 CBRT4 = 1.587401051968199474751705639272308260391493Q;
60 static const __float128 CBRT2I = 0.7937005259840997373758528196361541301957467Q;
61 static const __float128 CBRT4I = 0.6299605249474365823836053036391141752851257Q;
64 __float128
65 cbrtq (__float128 x)
67 int e, rem, sign;
68 __float128 z;
70 if (!finiteq (x))
71 return x + x;
73 if (x == 0)
74 return (x);
76 if (x > 0)
77 sign = 1;
78 else
80 sign = -1;
81 x = -x;
84 z = x;
85 /* extract power of 2, leaving mantissa between 0.5 and 1 */
86 x = frexpq (x, &e);
88 /* Approximate cube root of number between .5 and 1,
89 peak relative error = 1.2e-6 */
90 x = ((((1.3584464340920900529734e-1Q * x
91 - 6.3986917220457538402318e-1Q) * x
92 + 1.2875551670318751538055e0Q) * x
93 - 1.4897083391357284957891e0Q) * x
94 + 1.3304961236013647092521e0Q) * x + 3.7568280825958912391243e-1Q;
96 /* exponent divided by 3 */
97 if (e >= 0)
99 rem = e;
100 e /= 3;
101 rem -= 3 * e;
102 if (rem == 1)
103 x *= CBRT2;
104 else if (rem == 2)
105 x *= CBRT4;
107 else
108 { /* argument less than 1 */
109 e = -e;
110 rem = e;
111 e /= 3;
112 rem -= 3 * e;
113 if (rem == 1)
114 x *= CBRT2I;
115 else if (rem == 2)
116 x *= CBRT4I;
117 e = -e;
120 /* multiply by power of 2 */
121 x = ldexpq (x, e);
123 /* Newton iteration */
124 x -= (x - (z / (x * x))) * 0.3333333333333333333333333333333333333333Q;
125 x -= (x - (z / (x * x))) * 0.3333333333333333333333333333333333333333Q;
126 x -= (x - (z / (x * x))) * 0.3333333333333333333333333333333333333333Q;
128 if (sign < 0)
129 x = -x;
130 return (x);