1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm),
3 // ====================================================
4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved.
6 // Developed at SunSoft, a Sun Microsystems, Inc. business.
7 // Permission to use, copy, modify, and distribute this
8 // software is freely granted, provided that this notice
10 // ====================================================
12 // The original source code covered by the above license above has been
13 // modified significantly by Google Inc.
14 // Copyright 2014 the V8 project authors. All rights reserved.
16 // The following is a straightforward translation of fdlibm routines
17 // by Raymond Toy (rtoy@google.com).
19 // Double constants that do not have empty lower 32 bits are found in fdlibm.cc
20 // and exposed through kMath as typed array. We assume the compiler to convert
21 // from decimal to binary accurately enough to produce the intended values.
22 // kMath is initialized to a Float64Array during genesis and not writable.
25 const INVPIO2 = kMath[0];
26 const PIO2_1 = kMath[1];
27 const PIO2_1T = kMath[2];
28 const PIO2_2 = kMath[3];
29 const PIO2_2T = kMath[4];
30 const PIO2_3 = kMath[5];
31 const PIO2_3T = kMath[6];
32 const PIO4 = kMath[32];
33 const PIO4LO = kMath[33];
35 // Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For
36 // precision, r is returned as two values y0 and y1 such that r = y0 + y1
37 // to more than double precision.
40 var hx = %_DoubleHi(X);
41 var ix = hx & 0x7fffffff;
43 if (ix < 0x4002d97c) {
44 // |X| ~< 3*pi/4, special case with n = +/- 1
47 if (ix != 0x3ff921fb) {
48 // 33+53 bit pi is good enough
50 y1 = (z - y0) - PIO2_1T;
52 // near pi/2, use 33+33+53 bit pi
55 y1 = (z - y0) - PIO2_2T;
61 if (ix != 0x3ff921fb) {
62 // 33+53 bit pi is good enough
64 y1 = (z - y0) + PIO2_1T;
66 // near pi/2, use 33+33+53 bit pi
69 y1 = (z - y0) + PIO2_2T;
73 } else if (ix <= 0x413921fb) {
74 // |X| ~<= 2^19*(pi/2), medium size
76 n = (t * INVPIO2 + 0.5) | 0;
77 var r = t - n * PIO2_1;
79 // First round good to 85 bit
81 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) {
82 // 2nd iteration needed, good to 118
86 w = n * PIO2_2T - ((t - r) - w);
88 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) {
89 // 3rd iteration needed. 151 bits accuracy
93 w = n * PIO2_3T - ((t - r) - w);
104 // Need to do full Payne-Hanek reduction here.
113 // __kernel_sin(X, Y, IY)
114 // kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
115 // Input X is assumed to be bounded by ~pi/4 in magnitude.
116 // Input Y is the tail of X so that x = X + Y.
119 // 1. Since ieee_sin(-x) = -ieee_sin(x), we need only to consider positive x.
120 // 2. ieee_sin(x) is approximated by a polynomial of degree 13 on
123 // sin(x) ~ x + S1*x + ... + S6*x
126 // |ieee_sin(x) 2 4 6 8 10 12 | -58
127 // |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
130 // 3. ieee_sin(X+Y) = ieee_sin(X) + sin'(X')*Y
131 // ~ ieee_sin(X) + (1-X*X/2)*Y
132 // For better accuracy, let
134 // r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6))))
136 // sin(x) = X + (S1*X + (X *(r-Y/2)+Y))
142 macro RETURN_KERNELSIN(X, Y, SIGN)
145 var r = KSIN(1) + z * (KSIN(2) + z * (KSIN(3) +
146 z * (KSIN(4) + z * KSIN(5))));
147 return (X - ((z * (0.5 * Y - v * r) - Y) - v * KSIN(0))) SIGN;
150 // __kernel_cos(X, Y)
151 // kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
152 // Input X is assumed to be bounded by ~pi/4 in magnitude.
153 // Input Y is the tail of X so that x = X + Y.
156 // 1. Since ieee_cos(-x) = ieee_cos(x), we need only to consider positive x.
157 // 2. ieee_cos(x) is approximated by a polynomial of degree 14 on
160 // cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
161 // where the remez error is
163 // | 2 4 6 8 10 12 14 | -58
164 // |ieee_cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
168 // 3. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
169 // ieee_cos(x) = 1 - x*x/2 + r
170 // since ieee_cos(X+Y) ~ ieee_cos(X) - ieee_sin(X)*Y
171 // ~ ieee_cos(X) - X*Y,
172 // a correction term is necessary in ieee_cos(x) and hence
173 // cos(X+Y) = 1 - (X*X/2 - (r - X*Y))
174 // For better accuracy when x > 0.3, let qx = |x|/4 with
175 // the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
177 // cos(X+Y) = (1-qx) - ((X*X/2-qx) - (r-X*Y)).
178 // Note that 1-qx and (X*X/2-qx) is EXACT here, and the
179 // magnitude of the latter is at least a quarter of X*X/2,
180 // thus, reducing the rounding error in the subtraction.
186 macro RETURN_KERNELCOS(X, Y, SIGN)
187 var ix = %_DoubleHi(X) & 0x7fffffff;
189 var r = z * (KCOS(0) + z * (KCOS(1) + z * (KCOS(2)+
190 z * (KCOS(3) + z * (KCOS(4) + z * KCOS(5))))));
191 if (ix < 0x3fd33333) { // |x| ~< 0.3
192 return (1 - (0.5 * z - (z * r - X * Y))) SIGN;
195 if (ix > 0x3fe90000) { // |x| > 0.78125
198 qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0);
200 var hz = 0.5 * z - qx;
201 return (1 - qx - (hz - (z * r - X * Y))) SIGN;
206 // kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
207 // Input x is assumed to be bounded by ~pi/4 in magnitude.
208 // Input y is the tail of x.
209 // Input k indicates whether ieee_tan (if k = 1) or -1/tan (if k = -1)
213 // 1. Since ieee_tan(-x) = -ieee_tan(x), we need only to consider positive x.
214 // 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
215 // 3. ieee_tan(x) is approximated by a odd polynomial of degree 27 on
218 // tan(x) ~ x + T1*x + ... + T13*x
221 // |ieee_tan(x) 2 4 26 | -59.2
222 // |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
225 // Note: ieee_tan(x+y) = ieee_tan(x) + tan'(x)*y
226 // ~ ieee_tan(x) + (1+x*x)*y
227 // Therefore, for better accuracy in computing ieee_tan(x+y), let
229 // r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
232 // tan(x+y) = x + (T1*x + (x *(r+y)+y))
234 // 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
235 // tan(x) = ieee_tan(pi/4-y) = (1-ieee_tan(y))/(1+ieee_tan(y))
236 // = 1 - 2*(ieee_tan(y) - (ieee_tan(y)^2)/(1+ieee_tan(y)))
238 // Set returnTan to 1 for tan; -1 for cot. Anything else is illegal
239 // and will cause incorrect results.
245 function KernelTan(x, y, returnTan) {
248 var hx = %_DoubleHi(x);
249 var ix = hx & 0x7fffffff;
251 if (ix < 0x3e300000) { // |x| < 2^-28
252 if (((ix | %_DoubleLo(x)) | (returnTan + 1)) == 0) {
253 // x == 0 && returnTan = -1
254 return 1 / MathAbs(x);
256 if (returnTan == 1) {
259 // Compute -1/(x + y) carefully
261 var z = %_ConstructDouble(%_DoubleHi(w), 0);
264 var t = %_ConstructDouble(%_DoubleHi(a), 0);
266 return t + a * (s + t * v);
270 if (ix >= 0x3fe59428) { // |x| > .6744
283 // Break x^5 * (T1 + x^2*T2 + ...) into
284 // x^5 * (T1 + x^4*T3 + ... + x^20*T11) +
285 // x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12))
286 var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) +
287 w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11)))));
288 var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) +
289 w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12))))));
291 r = y + z * (s * (r + v) + y);
294 if (ix >= 0x3fe59428) {
295 return (1 - ((hx >> 30) & 2)) *
296 (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r)));
298 if (returnTan == 1) {
301 z = %_ConstructDouble(%_DoubleHi(w), 0);
304 var t = %_ConstructDouble(%_DoubleHi(a), 0);
306 return t + a * (s + t * v);
310 function MathSinSlow(x) {
312 var sign = 1 - (n & 2);
314 RETURN_KERNELCOS(y0, y1, * sign);
316 RETURN_KERNELSIN(y0, y1, * sign);
320 function MathCosSlow(x) {
323 var sign = (n & 2) - 1;
324 RETURN_KERNELSIN(y0, y1, * sign);
326 var sign = 1 - (n & 2);
327 RETURN_KERNELCOS(y0, y1, * sign);
331 // ECMA 262 - 15.8.2.16
332 function MathSin(x) {
333 x = x * 1; // Convert to number.
334 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
335 // |x| < pi/4, approximately. No reduction needed.
336 RETURN_KERNELSIN(x, 0, /* empty */);
338 return MathSinSlow(x);
341 // ECMA 262 - 15.8.2.7
342 function MathCos(x) {
343 x = x * 1; // Convert to number.
344 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
345 // |x| < pi/4, approximately. No reduction needed.
346 RETURN_KERNELCOS(x, 0, /* empty */);
348 return MathCosSlow(x);
351 // ECMA 262 - 15.8.2.18
352 function MathTan(x) {
353 x = x * 1; // Convert to number.
354 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
355 // |x| < pi/4, approximately. No reduction needed.
356 return KernelTan(x, 0, 1);
359 return KernelTan(y0, y1, (n & 1) ? -1 : 1);
362 // ES6 draft 09-27-13, section 20.2.2.20.
366 // 1. Argument Reduction: find k and f such that
367 // 1+x = 2^k * (1+f),
368 // where sqrt(2)/2 < 1+f < sqrt(2) .
370 // Note. If k=0, then f=x is exact. However, if k!=0, then f
371 // may not be representable exactly. In that case, a correction
372 // term is need. Let u=1+x rounded. Let c = (1+x)-u, then
373 // log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
374 // and add back the correction term c/u.
375 // (Note: when x > 2**53, one can simply return log(x))
377 // 2. Approximation of log1p(f).
378 // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
379 // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
381 // We use a special Reme algorithm on [0,0.1716] to generate
382 // a polynomial of degree 14 to approximate R The maximum error
383 // of this polynomial approximation is bounded by 2**-58.45. In
386 // R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
387 // (the values of Lp1 to Lp7 are listed in the program)
390 // | Lp1*s +...+Lp7*s - R(z) | <= 2
392 // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
393 // In order to guarantee error in log below 1ulp, we compute log
395 // log1p(f) = f - (hfsq - s*(hfsq+R)).
397 // 3. Finally, log1p(x) = k*ln2 + log1p(f).
398 // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
399 // Here ln2 is split into two floating point number:
401 // where n*ln2_hi is always exact for |n| < 2000.
404 // log1p(x) is NaN with signal if x < -1 (including -INF) ;
405 // log1p(+INF) is +INF; log1p(-1) is -INF with signal;
406 // log1p(NaN) is that NaN with no signal.
409 // according to an error analysis, the error is always less than
410 // 1 ulp (unit in the last place).
413 // Constants are found in fdlibm.cc. We assume the C++ compiler to convert
414 // from decimal to binary accurately enough to produce the intended values.
416 // Note: Assuming log() return accurate answer, the following
417 // algorithm can be used to compute log1p(x) to within a few ULP:
420 // if (u==1.0) return x ; else
421 // return log(u)*(x/(u-1.0));
423 // See HP-15C Advanced Functions Handbook, p.193.
425 const LN2_HI = kMath[34];
426 const LN2_LO = kMath[35];
427 const TWO54 = kMath[36];
428 const TWO_THIRD = kMath[37];
433 function MathLog1p(x) {
434 x = x * 1; // Convert to number.
435 var hx = %_DoubleHi(x);
436 var ax = hx & 0x7fffffff;
443 if (hx < 0x3fda827a) {
445 if (ax >= 0x3ff00000) { // |x| >= 1
447 return -INFINITY; // log1p(-1) = -inf
449 return NAN; // log1p(x<-1) = NaN
451 } else if (ax < 0x3c900000) {
452 // For |x| < 2^-54 we can return x.
454 } else if (ax < 0x3e200000) {
455 // For |x| < 2^-29 we can use a simple two-term Taylor series.
456 return x - x * x * 0.5;
459 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d
460 // -.2929 < x < 0.41422
465 // Handle Infinity and NAN
466 if (hx >= 0x7ff00000) return x;
469 if (hx < 0x43400000) {
473 k = (hu >> 20) - 1023;
474 c = (k > 0) ? 1 - (u - x) : x - (u - 1);
478 k = (hu >> 20) - 1023;
482 u = %_ConstructDouble(hu | 0x3ff00000, %_DoubleLo(u)); // Normalize u.
485 u = %_ConstructDouble(hu | 0x3fe00000, %_DoubleLo(u)); // Normalize u/2.
486 hu = (0x00100000 - hu) >> 2;
491 var hfsq = 0.5 * f * f;
498 return k * LN2_HI + (c + k * LN2_LO);
501 var R = hfsq * (1 - TWO_THIRD * f);
505 return k * LN2_HI - ((R - (k * LN2_LO + c)) - f);
511 var R = z * (KLOG1P(0) + z * (KLOG1P(1) + z *
512 (KLOG1P(2) + z * (KLOG1P(3) + z *
513 (KLOG1P(4) + z * (KLOG1P(5) + z * KLOG1P(6)))))));
515 return f - (hfsq - s * (hfsq + R));
517 return k * LN2_HI - ((hfsq - (s * (hfsq + R) + (k * LN2_LO + c))) - f);
521 // ES6 draft 09-27-13, section 20.2.2.14.
523 // Returns exp(x)-1, the exponential of x minus 1.
526 // 1. Argument reduction:
527 // Given x, find r and integer k such that
529 // x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
531 // Here a correction term c will be computed to compensate
532 // the error in r when rounded to a floating-point number.
534 // 2. Approximating expm1(r) by a special rational function on
535 // the interval [0,0.34658]:
537 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
538 // we define R1(r*r) by
539 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
541 // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
542 // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
543 // = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
544 // We use a special Remes algorithm on [0,0.347] to generate
545 // a polynomial of degree 5 in r*r to approximate R1. The
546 // maximum error of this polynomial approximation is bounded
547 // by 2**-61. In other words,
548 // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
549 // where Q1 = -1.6666666666666567384E-2,
550 // Q2 = 3.9682539681370365873E-4,
551 // Q3 = -9.9206344733435987357E-6,
552 // Q4 = 2.5051361420808517002E-7,
553 // Q5 = -6.2843505682382617102E-9;
554 // (where z=r*r, and the values of Q1 to Q5 are listed below)
555 // with error bounded by
557 // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
560 // expm1(r) = exp(r)-1 is then computed by the following
561 // specific way which minimize the accumulation rounding error:
563 // r r [ 3 - (R1 + R1*r/2) ]
564 // expm1(r) = r + --- + --- * [--------------------]
565 // 2 2 [ 6 - r*(3 - R1*r/2) ]
567 // To compensate the error in the argument reduction, we use
568 // expm1(r+c) = expm1(r) + c + expm1(r)*c
569 // ~ expm1(r) + c + r*c
570 // Thus c+r*c will be added in as the correction terms for
571 // expm1(r+c). Now rearrange the term to avoid optimization
574 // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
575 // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
576 // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
580 // 3. Scale back to obtain expm1(x):
581 // From step 1, we have
582 // expm1(x) = either 2^k*[expm1(r)+1] - 1
583 // = or 2^k*[expm1(r) + (1-2^-k)]
584 // 4. Implementation notes:
585 // (A). To save one multiplication, we scale the coefficient Qi
586 // to Qi*2^i, and replace z by (x^2)/2.
587 // (B). To achieve maximum accuracy, we compute expm1(x) by
588 // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
589 // (ii) if k=0, return r-E
590 // (iii) if k=-1, return 0.5*(r-E)-0.5
591 // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
592 // else return 1.0+2.0*(r-E);
593 // (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
594 // (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
595 // (vii) return 2^k(1-((E+2^-k)-r))
598 // expm1(INF) is INF, expm1(NaN) is NaN;
599 // expm1(-INF) is -1, and
600 // for finite argument, only expm1(0)=0 is exact.
603 // according to an error analysis, the error is always less than
604 // 1 ulp (unit in the last place).
608 // if x > 7.09782712893383973096e+02 then expm1(x) overflow
610 const KEXPM1_OVERFLOW = kMath[45];
611 const INVLN2 = kMath[46];
616 function MathExpm1(x) {
617 x = x * 1; // Convert to number.
625 var hx = %_DoubleHi(x);
626 var xsb = hx & 0x80000000; // Sign bit of x
627 var y = (xsb === 0) ? x : -x; // y = |x|
628 hx &= 0x7fffffff; // High word of |x|
630 // Filter out huge and non-finite argument
631 if (hx >= 0x4043687a) { // if |x| ~=> 56 * ln2
632 if (hx >= 0x40862e42) { // if |x| >= 709.78
633 if (hx >= 0x7ff00000) {
634 // expm1(inf) = inf; expm1(-inf) = -1; expm1(nan) = nan;
635 return (x === -INFINITY) ? -1 : x;
637 if (x > KEXPM1_OVERFLOW) return INFINITY; // Overflow
639 if (xsb != 0) return -1; // x < -56 * ln2, return -1.
642 // Argument reduction
643 if (hx > 0x3fd62e42) { // if |x| > 0.5 * ln2
644 if (hx < 0x3ff0a2b2) { // and |x| < 1.5 * ln2
655 k = (INVLN2 * x + ((xsb === 0) ? 0.5 : -0.5)) | 0;
657 // t * ln2_hi is exact here.
663 } else if (hx < 0x3c900000) {
664 // When |x| < 2^-54, we can return x.
671 // x is now in primary range
674 var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs *
675 (KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4)))));
677 var e = hxs * ((r1 - t) / (6 - x * t));
678 if (k === 0) { // c is 0
679 return x - (x*e - hxs);
681 e = (x * (e - c) - c);
683 if (k === -1) return 0.5 * (x - e) - 0.5;
685 if (x < -0.25) return -2 * (e - (x + 0.5));
686 return 1 + 2 * (x - e);
689 if (k <= -2 || k > 56) {
690 // suffice to return exp(x) + 1
692 // Add k to y's exponent
693 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
698 t = %_ConstructDouble(0x3ff00000 - (0x200000 >> k), 0);
700 // Add k to y's exponent
701 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
704 t = %_ConstructDouble((0x3ff - k) << 20, 0);
707 // Add k to y's exponent
708 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
715 // ES6 draft 09-27-13, section 20.2.2.30.
718 // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
719 // 1. Replace x by |x| (sinh(-x) = -sinh(x)).
722 // 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
725 // 22 <= x <= lnovft : sinh(x) := exp(x)/2
726 // lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
727 // ln2ovft < x : sinh(x) := x*shuge (overflow)
730 // sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
731 // only sinh(0)=0 is exact for finite x.
733 const KSINH_OVERFLOW = kMath[52];
734 const TWO_M28 = 3.725290298461914e-9; // 2^-28, empty lower half
735 const LOG_MAXD = 709.7822265625; // 0x40862e42 00000000, empty lower half
737 function MathSinh(x) {
738 x = x * 1; // Convert to number.
739 var h = (x < 0) ? -0.5 : 0.5;
740 // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1))
743 // For |x| < 2^-28, sinh(x) = x
744 if (ax < TWO_M28) return x;
745 var t = MathExpm1(ax);
746 if (ax < 1) return h * (2 * t - t * t / (t + 1));
747 return h * (t + t / (t + 1));
749 // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|)
750 if (ax < LOG_MAXD) return h * MathExp(ax);
751 // |x| in [log(maxdouble), overflowthreshold]
752 // overflowthreshold = 710.4758600739426
753 if (ax <= KSINH_OVERFLOW) {
754 var w = MathExp(0.5 * ax);
758 // |x| > overflowthreshold or is NaN.
759 // Return Infinity of the appropriate sign or NaN.
764 // ES6 draft 09-27-13, section 20.2.2.12.
767 // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
768 // 1. Replace x by |x| (cosh(x) = cosh(-x)).
771 // 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
775 // ln2/2 <= x <= 22 : cosh(x) := -------------------
777 // 22 <= x <= lnovft : cosh(x) := exp(x)/2
778 // lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
779 // ln2ovft < x : cosh(x) := huge*huge (overflow)
782 // cosh(x) is |x| if x is +INF, -INF, or NaN.
783 // only cosh(0)=1 is exact for finite x.
785 const KCOSH_OVERFLOW = kMath[52];
787 function MathCosh(x) {
788 x = x * 1; // Convert to number.
789 var ix = %_DoubleHi(x) & 0x7fffffff;
790 // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|))
791 if (ix < 0x3fd62e43) {
792 var t = MathExpm1(MathAbs(x));
794 // For |x| < 2^-55, cosh(x) = 1
795 if (ix < 0x3c800000) return w;
796 return 1 + (t * t) / (w + w);
798 // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2
799 if (ix < 0x40360000) {
800 var t = MathExp(MathAbs(x));
801 return 0.5 * t + 0.5 / t;
803 // |x| in [22, log(maxdouble)], return half*exp(|x|)
804 if (ix < 0x40862e42) return 0.5 * MathExp(MathAbs(x));
805 // |x| in [log(maxdouble), overflowthreshold]
806 if (MathAbs(x) <= KCOSH_OVERFLOW) {
807 var w = MathExp(0.5 * MathAbs(x));
811 if (NUMBER_IS_NAN(x)) return x;
812 // |x| > overflowthreshold.