( x: Complex, y: Complex, z: Complex, p: Complex )
| 388 | return [a, b < 0 ? b - LN16 : b + LN16]; |
| 389 | } |
| 390 | const [ar, ai] = sqrtParts(1 - x, -y); // √(1 − z) |
| 391 | const [br, bi] = sqrtParts(1 + x, y); // √(1 + z) |
| 392 | // Re: atan2(x, Re(√(1 − z)·√(1 + z))) |
| 393 | // Im: arsinh(Im(conj(√(1 − z))·√(1 + z))) |
| 394 | return [Math.atan2(x, ar * br - ai * bi), Math.asinh(ar * bi - ai * br)]; |
| 395 | } |
| 396 | |
| 397 | /** `acos(x + iy)`, the side of a cut picked by the sign of a zero part. */ |
| 398 | function acosCore(x: number, y: number): [number, number] { |
| 399 | if (isHuge(x, y)) { |
| 400 | const [a, b] = acosCore(x / 16, y / 16); |
| 401 | return [a, b < 0 ? b - LN16 : b + LN16]; |
| 402 | } |
| 403 | const [ar, ai] = sqrtParts(1 - x, -y); // √(1 − z) |
| 404 | const [br, bi] = sqrtParts(1 + x, y); // √(1 + z) |
| 405 | // Re: 2·atan2(Re √(1 − z), Re √(1 + z)) |
| 406 | // Im: arsinh(Im(conj(√(1 + z))·√(1 − z))) |
| 407 | return [2 * Math.atan2(ar, br), Math.asinh(br * ai - bi * ar)]; |
| 408 | } |
| 409 | |
| 410 | /** `acosh(x + iy)`, the side of a cut picked by the sign of a zero part. */ |
| 411 | function acoshCore(x: number, y: number): [number, number] { |
| 412 | if (isHuge(x, y)) { |
| 413 | const [a, b] = acoshCore(x / 16, y / 16); |
| 414 | return [a + LN16, b]; |
| 415 | } |
| 416 | const [ar, ai] = sqrtParts(x - 1, y); // √(z − 1) |
| 417 | const [br, bi] = sqrtParts(x + 1, y); // √(z + 1) |
| 418 | // Re: arsinh(Re(conj(√(z − 1))·√(z + 1))) |
| 419 | // Im: 2·atan2(Im √(z − 1), Re √(z + 1)) |
| 420 | return [Math.asinh(ar * br + ai * bi), 2 * Math.atan2(ai, br)]; |
| 421 | } |
| 422 | |
| 423 | /** `atanh(x + iy)`, the side of a cut picked by the sign of a zero part. */ |
| 424 | function atanhCore(x: number, y: number): [number, number] { |
| 425 | const h = Math.hypot(x, y); |
| 426 | if (h > 1e150) { |
| 427 | // atanh z = ½·ln((1 + z)/(1 − z)). For a large |z|, the real part is |
| 428 | // Re(1/z) = x/|z|² to a relative error of about 1/|z|², and the |
| 429 | // imaginary part is ½·atan2(2y, 1 − |z|²) = ½·atan2(2y/|z|², −1) to |
| 430 | // the same error. Dividing by |z| twice avoids the overflow of |z|², |
| 431 | // and |z|/4 (`h4`) is used because |z| itself can overflow. |
| 432 | const h4 = Math.hypot(x / 4, y / 4); |
| 433 | return [x / 16 / h4 / h4, 0.5 * Math.atan2(y / 8 / h4 / h4, -1)]; |
| 434 | } |
| 435 | // The real part is odd in x, so it is computed for |x| and given the sign |
| 436 | // of x: for x < 0, 1 + 4x/|1 − z|² would cancel near z = −1. |
| 437 | const ax = Math.abs(x); |
| 438 | const u = 1 - ax; |
| 439 | const d = u * u + y * y; // |1 − |x| − iy|² |
| 440 | // Re: ¼·ln(|1 + z|²/|1 − z|²) = ¼·ln(1 + 4x/|1 − z|²). When |1 − z|² is |
| 441 | // below the normal range (z very close to ±1), the ratio of the two |
| 442 | // moduli is used instead: there is no cancellation in that case. |
| 443 | const re = |
| 444 | d < 1e-290 |
| 445 | ? 0.5 * (Math.log(Math.hypot(1 + ax, y)) - Math.log(Math.hypot(u, y))) |
| 446 | : 0.25 * Math.log1p((4 * ax) / d); |
| 447 | // Im: ½·arg((1 + z)(1 − conj z)) = ½·atan2(2y, (1 − x)(1 + x) − y²) |
no test coverage detected