(x: Complex, y: Complex, z: Complex)
| 335 | /** |
| 336 | * The principal square root of `x + iy`, with the sign of a zero imaginary |
| 337 | * part kept (`√(−4 − 0i)` is `−2i`). The operands are scaled when they are |
| 338 | * near the ends of the double range, so `x + |z|` does not overflow and a |
| 339 | * subnormal operand keeps its digits. |
| 340 | */ |
| 341 | function sqrtParts(x: number, y: number): [number, number] { |
| 342 | if (x === 0 && y === 0) return [0, y]; |
| 343 | let scale = 1; |
| 344 | const m = Math.max(Math.abs(x), Math.abs(y)); |
| 345 | if (m > 1e300) { |
| 346 | x *= 0.25; |
| 347 | y *= 0.25; |
| 348 | scale = 2; |
| 349 | } else if (m < 1e-300) { |
| 350 | // 2^1000 and 2^-500: powers of two, so the scaling is exact. |
| 351 | x *= 2 ** 1000; |
| 352 | y *= 2 ** 1000; |
| 353 | scale = 2 ** -500; |
| 354 | } |
| 355 | const h = Math.hypot(x, y); |
| 356 | if (!signBit(x) || x === 0) { |
| 357 | const t = Math.sqrt((x + h) / 2); |
| 358 | return [t * scale, (y / (2 * t)) * scale]; |
| 359 | } |
| 360 | const t = Math.sqrt((h - x) / 2); |
| 361 | return [(Math.abs(y) / (2 * t)) * scale, (signBit(y) ? -t : t) * scale]; |
| 362 | } |
| 363 | |
| 364 | function isFiniteComplex(x: number, y: number): boolean { |
| 365 | return Number.isFinite(x) && Number.isFinite(y); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * True when `x + iy` is so large that the product of two square roots in the |
| 370 | * formulas below can overflow (`|z|` above the largest double, about |
| 371 | * 1.8·10³⁰⁸). The functions then use their value at `z/16`: for a large |
| 372 | * `|z|`, `f(z)` and `f(z/16)` have the same angle part, and their |
| 373 | * logarithmic part differs by `ln 16 = 4·ln 2`, to a relative error of about |
| 374 | * `1/|z|²`. The threshold, 6·10³⁰⁷ for the larger part, keeps `|z|` below |
| 375 | * 8.5·10³⁰⁷, and `z/16` below it. |
| 376 | */ |
| 377 | function isHuge(x: number, y: number): boolean { |
| 378 | return Math.max(Math.abs(x), Math.abs(y)) > 6e307; |
| 379 | } |
| 380 | |
| 381 | /** `ln 16`, exact from `Math.LN2` (a product by a power of two). */ |
| 382 | const LN16 = 4 * Math.LN2; |
no test coverage detected