1 The following functions for the `long double' versions of the libm
2 function have to be written:
31 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36 * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
37 * we approximate asin(x) on [0,0.5] by
38 * asin(x) = x + x*x^2*R(x^2)
40 * R(x^2) is a rational approximation of (asin(x)-x)/x^3
41 * and its remez error is bounded by
42 * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
45 * asin(x) = pi/2-2*asin(sqrt((1-x)/2))
46 * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
48 * asin(x) = pi/2 - 2*(s+s*z*R(z))
49 * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
50 * For x<=0.98, let pio4_hi = pio2_hi/2, then
52 * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
54 * asin(x) = pi/2 - 2*(s+s*z*R(z))
55 * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
56 * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
57 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 * acos(x) = pi/2 - asin(x)
62 * acos(-x) = pi/2 + asin(x)
64 * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c)
66 * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
67 * = 2asin(sqrt((1-x)/2))
68 * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z)
69 * = 2f + (2c + 2s*z*R(z))
70 * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
71 * for f so that f+c ~ sqrt(z).
73 * acos(x) = pi - 2asin(sqrt((1-|x|)/2))
74 * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
75 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
80 * 2. Reduce x to positive by (if x and y are unexceptional):
81 * ARG (x+iy) = arctan(y/x) ... if x > 0,
82 * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 * 1. Reduce x to positive by atan(x) = -atan(-x).
88 * 2. According to the integer k=4t+0.25 chopped, t=x, the argument
89 * is further reduced to one of the following intervals and the
90 * arctangent of t is evaluated by the corresponding formula:
92 * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
93 * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
94 * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
95 * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
96 * [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101 * 1. Argument reduction:
102 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
103 * Given x, find r and integer k such that
105 * x = k*ln2 + r, |r| <= 0.5*ln2.
107 * Here r will be represented as r = hi-lo for better
110 * 2. Approximation of exp(r) by a special rational function on
111 * the interval [0,0.34658]:
113 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
114 * We use a special Reme algorithm on [0,0.34658] to generate
115 * a polynomial of degree 5 to approximate R. The maximum error
116 * of this polynomial approximation is bounded by 2**-59. In
118 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
119 * (where z=r*r, and the values of P1 to P5 are listed below)
122 * | 2.0+P1*z+...+P5*z - R(z) | <= 2
124 * The computation of exp(r) thus becomes
126 * exp(r) = 1 + -------
129 * = 1 + r + ----------- (for better accuracy)
133 * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
135 * 3. Scale back to obtain exp(x):
136 * From step 1, we have
137 * exp(x) = 2^k * exp(r)
138 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 * If (assume round-to-nearest) z=x*x+y*y
142 * has error less than sqrt(2)/2 ulp, than
143 * sqrt(z) has error less than 1 ulp (exercise).
145 * So, compute sqrt(x*x+y*y) with some care as
146 * follows to get the error below 1 ulp:
149 * (if possible, set rounding to round-to-nearest)
151 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
152 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
154 * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
155 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
156 * y1= y with lower 32 bits chopped, y2 = y-y1.
158 * NOTE: scaling may be necessary if some argument is too
160 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164 * 1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ...
165 * 2. Reduce x to |x| since j0(x)=j0(-x), and
167 * j0(x) = 1-z/4+ z^2*R0/S0, where z = x*x;
168 * (precision: |j0-1+z/4-z^2R0/S0 |<2**-63.67 )
170 * j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0))
171 * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
173 * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4)
174 * = 1/sqrt(2) * (cos(x) + sin(x))
175 * sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4)
176 * = 1/sqrt(2) * (sin(x) - cos(x))
177 * (To avoid cancellation, use
178 * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
179 * to compute the worse one.)
184 * y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...)
185 * therefore y0(x)-2/pi*j0(x)*ln(x) is an even function.
186 * We use the following function to approximate y0,
187 * y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2
189 * U(z) = u00 + u01*z + ... + u06*z^6
190 * V(z) = 1 + v01*z + ... + v04*z^4
191 * with absolute approximation error bounded by 2**-72.
192 * Note: For tiny x, U/V = u0 and j0(x)~1, hence
193 * y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27)
195 * y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0))
196 * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
197 * by the method mentioned above.
198 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202 * 1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ...
203 * 2. Reduce x to |x| since j1(x)=-j1(-x), and
205 * j1(x) = x/2 + x*z*R0/S0, where z = x*x;
206 * (precision: |j1/x - 1/2 - R0/S0 |<2**-61.51 )
208 * j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1))
209 * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
210 * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
212 * cos(x1) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
213 * = 1/sqrt(2) * (sin(x) - cos(x))
214 * sin(x1) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
215 * = -1/sqrt(2) * (sin(x) + cos(x))
216 * (To avoid cancellation, use
217 * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
218 * to compute the worse one.)
221 * 1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN
224 * y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...)
225 * therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function.
226 * We use the following function to approximate y1,
227 * y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2
228 * where for x in [0,2] (abs err less than 2**-65.89)
229 * U(z) = U0[0] + U0[1]*z + ... + U0[4]*z^4
230 * V(z) = 1 + v0[0]*z + ... + v0[4]*z^5
231 * Note: For tiny x, 1/x dominate y1 and hence
232 * y1(tiny) = -2/pi/tiny, (choose tiny<2**-54)
234 * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
235 * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
236 * by method mentioned above.
237 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240 * Note 2. About jn(n,x), yn(n,x)
241 * For n=0, j0(x) is called,
242 * for n=1, j1(x) is called,
243 * for n<x, forward recursion us used starting
244 * from values of j0(x) and j1(x).
245 * for n>x, a continued fraction approximation to
246 * j(n,x)/j(n-1,x) is evaluated and then backward
247 * recursion is used starting from a supposed value
248 * for j(n,x). The resulting value of j(0,x) is
249 * compared with the actual value to correct the
250 * supposed value of j(n,x).
252 * yn(n,x) is similar in all respects, except
253 * that forward recursion is used for all
258 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
259 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
260 * Let s=sin(x), c=cos(x),
261 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
263 * n sin(xn)*sqt2 cos(xn)*sqt2
264 * ----------------------------------
270 /* x is tiny, return the first Taylor expansion of J(n,x)
271 * J(n,x) = 1/n!*(x/2)^n - ...
273 /* use backward recurrence */
275 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
276 * 2n - 2(n+1) - 2(n+2)
279 * (for large x) = ---- ------ ------ .....
281 * -- - ------ - ------ -
284 * Let w = 2n/x and h=2/x, then the above quotient
285 * is equal to the continued fraction:
287 * = -----------------------
289 * w - -----------------
294 * To determine how many terms needed, let
295 * Q(0) = w, Q(1) = w(w+h) - 1,
296 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
297 * When Q(k) > 1e4 good for single
298 * When Q(k) > 1e9 good for double
299 * When Q(k) > 1e17 good for quadruple
302 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
303 * Hence, if n*(log(2n/x)) > ...
304 * single 8.8722839355e+01
305 * double 7.09782712893383973096e+02
306 * long double 1.1356523406294143949491931077970765006170e+04
307 * then recurrent value may overflow and the result is
308 * likely underflow to zero
312 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
313 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
314 * Let s=sin(x), c=cos(x),
315 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
317 * n sin(xn)*sqt2 cos(xn)*sqt2
318 * ----------------------------------
323 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327 * 1. Argument Reduction for 0 < x <= 8
328 * Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
329 * reduce x to a number in [1.5,2.5] by
330 * lgamma(1+s) = log(s) + lgamma(s)
332 * lgamma(7.3) = log(6.3) + lgamma(6.3)
333 * = log(6.3*5.3) + lgamma(5.3)
334 * = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
335 * 2. Polynomial approximation of lgamma around its
336 * minimun ymin=1.461632144968362245 to maintain monotonicity.
337 * On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
339 * lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
341 * poly(z) is a 14 degree polynomial.
342 * 2. Rational approximation in the primary interval [2,3]
343 * We use the following approximation:
345 * lgamma(x) = 0.5*s + s*P(s)/Q(s)
347 * |P/Q - (lgamma(x)-0.5s)| < 2**-61.71
348 * Our algorithms are based on the following observation
350 * zeta(2)-1 2 zeta(3)-1 3
351 * lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ...
354 * where Euler = 0.5771... is the Euler constant, which is very
357 * 3. For x>=8, we have
358 * lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
360 * lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
361 * Let z = 1/x, then we approximation
362 * f(z) = lgamma(x) - (x-0.5)(log(x)-1)
365 * w = w0 + w1*z + w2*z + w3*z + ... + w6*z
367 * |w - f(z)| < 2**-58.74
369 * 4. For negative x, since (G is gamma function)
370 * -x*G(-x)*G(x) = pi/sin(pi*x),
372 * G(x) = pi/(sin(pi*x)*(-x)*G(-x))
373 * since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
374 * Hence, for x<0, signgam = sign(sin(pi*x)) and
375 * lgamma(x) = log(|Gamma(x)|)
376 * = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
377 * Note: one should avoid compute pi*(-x) directly in the
378 * computation of sin(pi*(-x)).
379 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383 * 1. Argument Reduction: find k and f such that
385 * where sqrt(2)/2 < 1+f < sqrt(2) .
387 * 2. Approximation of log(1+f).
388 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
389 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
391 * We use a special Reme algorithm on [0,0.1716] to generate
392 * a polynomial of degree 14 to approximate R The maximum error
393 * of this polynomial approximation is bounded by 2**-58.45. In
396 * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
397 * (the values of Lg1 to Lg7 are listed in the program)
400 * | Lg1*s +...+Lg7*s - R(z) | <= 2
402 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
403 * In order to guarantee error in log below 1ulp, we compute log
405 * log(1+f) = f - s*(f - R) (if f is not too large)
406 * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
408 * 3. Finally, log(x) = k*ln2 + log(1+f).
409 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
410 * Here ln2 is split into two floating point number:
412 * where n*ln2_hi is always exact for |n| < 2000.
413 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
417 * Let log10_2hi = leading 40 bits of log10(2) and
418 * log10_2lo = log10(2) - log10_2hi,
419 * ivln10 = 1/log(10) rounded.
424 * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
427 * To guarantee log10(10**n)=n, where 10**n is normal, the rounding
428 * mode must set to Round-to-Nearest.
430 * [1/log(10)] rounded to 53 bits has error .198 ulps;
431 * log10 is monotonic at all binary break points.
432 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
435 * Method: Let x = 2 * (1+f)
436 * 1. Compute and return log2(x) in two pieces:
438 * where w1 has 53-24 = 29 bit trailing zeros.
439 * 2. Perform y*log2(x) = n+y' by simulating muti-precision
440 * arithmetic, where |y'|<=0.5.
441 * 3. Return x**y = 2**n*exp(y'*log2)
444 * 1. (anything) ** 0 is 1
445 * 2. (anything) ** 1 is itself
446 * 3. (anything) ** NAN is NAN
447 * 4. NAN ** (anything except 0) is NAN
448 * 5. +-(|x| > 1) ** +INF is +INF
449 * 6. +-(|x| > 1) ** -INF is +0
450 * 7. +-(|x| < 1) ** +INF is +0
451 * 8. +-(|x| < 1) ** -INF is +INF
452 * 9. +-1 ** +-INF is NAN
453 * 10. +0 ** (+anything except 0, NAN) is +0
454 * 11. -0 ** (+anything except 0, NAN, odd integer) is +0
455 * 12. +0 ** (-anything except 0, NAN) is +INF
456 * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
457 * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
458 * 15. +INF ** (+anything except 0,NAN) is +INF
459 * 16. +INF ** (-anything except 0,NAN) is +0
460 * 17. -INF ** (anything) = -0 ** (-anything)
461 * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
462 * 19. (-anything except 0 and inf) ** (non-integer) is NAN
463 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
464 rem_pio2 return the remainder of x rem pi/2 in y[0]+y[1]
466 This is one of the basic functions which is written with highest accuracy
468 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
472 * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
473 * 1. Replace x by |x| (sinh(-x) = -sinh(x)).
476 * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
479 * 22 <= x <= lnovft : sinh(x) := exp(x)/2
480 * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
481 * ln2ovft < x : sinh(x) := x*shuge (overflow)
482 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
486 * Bit by bit method using integer arithmetic. (Slow, but portable)
488 * Scale x to y in [1,4) with even powers of 2:
489 * find an integer k such that 1 <= (y=x*2^(-2k)) < 4, then
490 * sqrt(x) = 2^k * sqrt(y)
491 * 2. Bit by bit computation
492 * Let q = sqrt(y) truncated to i bit after binary point (q = 1),
495 * s = 2*q , and y = 2 * ( y - q ). (1)
498 * To compute q from q , one checks whether
505 * If (2) is false, then q = q ; otherwise q = q + 2 .
508 * With some algebric manipulation, it is not difficult to see
509 * that (2) is equivalent to
514 * The advantage of (3) is that s and y can be computed by
516 * the following recurrence formula:
519 * s = s , y = y ; (4)
524 * s = s + 2 , y = y - s - 2 (5)
527 * One may easily use induction to prove (4) and (5).
528 * Note. Since the left hand side of (3) contain only i+2 bits,
529 * it does not necessary to do a full (53-bit) comparison
532 * After generating the 53 bits result, we compute one more bit.
533 * Together with the remainder, we can decide whether the
534 * result is exact, bigger than 1/2ulp, or less than 1/2ulp
535 * (it will never equal to 1/2ulp).
536 * The rounding mode can be detected by checking whether
537 * huge + tiny is equal to huge, and whether huge - tiny is
538 * equal to huge for some floating point number "huge" and "tiny".
539 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
542 * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
543 * Input x is assumed to be bounded by ~pi/4 in magnitude.
544 * Input y is the tail of x.
547 * 1. Since cos(-x) = cos(x), we need only to consider positive x.
548 * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
549 * 3. cos(x) is approximated by a polynomial of degree 14 on
552 * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
553 * where the remez error is
555 * | 2 4 6 8 10 12 14 | -58
556 * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
560 * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
561 * cos(x) = 1 - x*x/2 + r
562 * since cos(x+y) ~ cos(x) - sin(x)*y
564 * a correction term is necessary in cos(x) and hence
565 * cos(x+y) = 1 - (x*x/2 - (r - x*y))
566 * For better accuracy when x > 0.3, let qx = |x|/4 with
567 * the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
569 * cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
570 * Note that 1-qx and (x*x/2-qx) is EXACT here, and the
571 * magnitude of the latter is at least a quarter of x*x/2,
572 * thus, reducing the rounding error in the subtraction.
573 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
576 * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
577 * Input x is assumed to be bounded by ~pi/4 in magnitude.
578 * Input y is the tail of x.
579 * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
582 * 1. Since sin(-x) = -sin(x), we need only to consider positive x.
583 * 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
584 * 3. sin(x) is approximated by a polynomial of degree 13 on
587 * sin(x) ~ x + S1*x + ... + S6*x
590 * |sin(x) 2 4 6 8 10 12 | -58
591 * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
594 * 4. sin(x+y) = sin(x) + sin'(x')*y
595 * ~ sin(x) + (1-x*x/2)*y
596 * For better accuracy, let
598 * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
600 * sin(x) = x + (S1*x + (x *(r-y/2)+y))
601 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
604 * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
605 * Input x is assumed to be bounded by ~pi/4 in magnitude.
606 * Input y is the tail of x.
607 * Input k indicates whether tan (if k=1) or
608 * -1/tan (if k= -1) is returned.
611 * 1. Since tan(-x) = -tan(x), we need only to consider positive x.
612 * 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
613 * 3. tan(x) is approximated by a odd polynomial of degree 27 on
616 * tan(x) ~ x + T1*x + ... + T13*x
619 * |tan(x) 2 4 26 | -59.2
620 * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
623 * Note: tan(x+y) = tan(x) + tan'(x)*y
624 * ~ tan(x) + (1+x*x)*y
625 * Therefore, for better accuracy in computing tan(x+y), let
627 * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
630 * tan(x+y) = x + (T1*x + (x *(r+y)+y))
632 * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
633 * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
634 * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
635 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
639 * 1. Reduce x to positive by atan(x) = -atan(-x).
640 * 2. According to the integer k=4t+0.25 chopped, t=x, the argument
641 * is further reduced to one of the following intervals and the
642 * arctangent of t is evaluated by the corresponding formula:
644 * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
645 * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
646 * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
647 * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
648 * [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
649 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
654 * erf(x) = --------- | exp(-t*t)dt
661 * erfc(-x) = 2 - erfc(x)
664 * 1. For |x| in [0, 0.84375]
665 * erf(x) = x + x*R(x^2)
666 * erfc(x) = 1 - erf(x) if x in [-.84375,0.25]
667 * = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375]
668 * where R = P/Q where P is an odd poly of degree 8 and
669 * Q is an odd poly of degree 10.
671 * | R - (erf(x)-x)/x | <= 2
674 * Remark. The formula is derived by noting
675 * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
677 * 2/sqrt(pi) = 1.128379167095512573896158903121545171688
678 * is close to one. The interval is chosen because the fix
679 * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
680 * near 0.6174), and by some experiment, 0.84375 is chosen to
681 * guarantee the error is less than one ulp for erf.
683 * 2. For |x| in [0.84375,1.25], let s = |x| - 1, and
684 * c = 0.84506291151 rounded to single (24 bits)
685 * erf(x) = sign(x) * (c + P1(s)/Q1(s))
686 * erfc(x) = (1-c) - P1(s)/Q1(s) if x > 0
687 * 1+(c+P1(s)/Q1(s)) if x < 0
688 * |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
689 * Remark: here we use the taylor series expansion at x=1.
690 * erf(1+s) = erf(1) + s*Poly(s)
691 * = 0.845.. + P1(s)/Q1(s)
692 * That is, we use rational approximation to approximate
693 * erf(1+s) - (c = (single)0.84506291151)
694 * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
696 * P1(s) = degree 6 poly in s
697 * Q1(s) = degree 6 poly in s
699 * 3. For x in [1.25,1/0.35(~2.857143)],
700 * erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1)
701 * erf(x) = 1 - erfc(x)
703 * R1(z) = degree 7 poly in z, (z=1/x^2)
704 * S1(z) = degree 8 poly in z
706 * 4. For x in [1/0.35,28]
707 * erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0
708 * = 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6<x<0
709 * = 2.0 - tiny (if x <= -6)
710 * erf(x) = sign(x)*(1.0 - erfc(x)) if x < 6, else
711 * erf(x) = sign(x)*(1.0 - tiny)
713 * R2(z) = degree 6 poly in z, (z=1/x^2)
714 * S2(z) = degree 7 poly in z
717 * To compute exp(-x*x-0.5625+R/S), let s be a single
718 * precision number and s := x; then
719 * -x*x = -s*s + (s-x)*(s+x)
720 * exp(-x*x-0.5626+R/S) =
721 * exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S);
723 * Here 4 and 5 make use of the asymptotic series
725 * erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) )
727 * We use rational approximation to approximate
728 * g(s)=f(1/x^2) = log(erfc(x)*x) - x*x + 0.5625
729 * Here is the error bound for R1/S1 and R2/S2
730 * |R1/S1 - f(x)| < 2**(-62.57)
731 * |R2/S2 - f(x)| < 2**(-61.52)
733 * 5. For inf > x >= 28
734 * erf(x) = sign(x) *(1 - tiny) (raise inexact)
735 * erfc(x) = tiny*tiny (raise underflow) if x > 0
737 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
738 expm1 Returns exp(x)-1, the exponential of x minus 1
741 * 1. Argument reduction:
742 * Given x, find r and integer k such that
744 * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
746 * Here a correction term c will be computed to compensate
747 * the error in r when rounded to a floating-point number.
749 * 2. Approximating expm1(r) by a special rational function on
750 * the interval [0,0.34658]:
752 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
753 * we define R1(r*r) by
754 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
756 * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
757 * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
758 * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
759 * We use a special Reme algorithm on [0,0.347] to generate
760 * a polynomial of degree 5 in r*r to approximate R1. The
761 * maximum error of this polynomial approximation is bounded
762 * by 2**-61. In other words,
763 * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
764 * where Q1 = -1.6666666666666567384E-2,
765 * Q2 = 3.9682539681370365873E-4,
766 * Q3 = -9.9206344733435987357E-6,
767 * Q4 = 2.5051361420808517002E-7,
768 * Q5 = -6.2843505682382617102E-9;
769 * (where z=r*r, and the values of Q1 to Q5 are listed below)
770 * with error bounded by
772 * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
775 * expm1(r) = exp(r)-1 is then computed by the following
776 * specific way which minimize the accumulation rounding error:
778 * r r [ 3 - (R1 + R1*r/2) ]
779 * expm1(r) = r + --- + --- * [--------------------]
780 * 2 2 [ 6 - r*(3 - R1*r/2) ]
782 * To compensate the error in the argument reduction, we use
783 * expm1(r+c) = expm1(r) + c + expm1(r)*c
784 * ~ expm1(r) + c + r*c
785 * Thus c+r*c will be added in as the correction terms for
786 * expm1(r+c). Now rearrange the term to avoid optimization
789 * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
790 * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
791 * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
795 * 3. Scale back to obtain expm1(x):
796 * From step 1, we have
797 * expm1(x) = either 2^k*[expm1(r)+1] - 1
798 * = or 2^k*[expm1(r) + (1-2^-k)]
799 * 4. Implementation notes:
800 * (A). To save one multiplication, we scale the coefficient Qi
801 * to Qi*2^i, and replace z by (x^2)/2.
802 * (B). To achieve maximum accuracy, we compute expm1(x) by
803 * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
804 * (ii) if k=0, return r-E
805 * (iii) if k=-1, return 0.5*(r-E)-0.5
806 * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
807 * else return 1.0+2.0*(r-E);
808 * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
809 * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
810 * (vii) return 2^k(1-((E+2^-k)-r))
813 * expm1(INF) is INF, expm1(NaN) is NaN;
814 * expm1(-INF) is -1, and
815 * for finite argument, only expm1(0)=0 is exact.
816 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
820 * 1. Argument Reduction: find k and f such that
822 * where sqrt(2)/2 < 1+f < sqrt(2) .
824 * Note. If k=0, then f=x is exact. However, if k!=0, then f
825 * may not be representable exactly. In that case, a correction
826 * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
827 * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
828 * and add back the correction term c/u.
829 * (Note: when x > 2**53, one can simply return log(x))
831 * 2. Approximation of log1p(f).
832 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
833 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
835 * We use a special Reme algorithm on [0,0.1716] to generate
836 * a polynomial of degree 14 to approximate R The maximum error
837 * of this polynomial approximation is bounded by 2**-58.45. In
840 * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
841 * (the values of Lp1 to Lp7 are listed in the program)
844 * | Lp1*s +...+Lp7*s - R(z) | <= 2
846 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
847 * In order to guarantee error in log below 1ulp, we compute log
849 * log1p(f) = f - (hfsq - s*(hfsq+R)).
851 * 3. Finally, log1p(x) = k*ln2 + log1p(f).
852 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
853 * Here ln2 is split into two floating point number:
855 * where n*ln2_hi is always exact for |n| < 2000.
856 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~