(x: Interval | IntervalResult)
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * The double nearest to `π − Math.PI`. `Math.PI` is below π, and |
| 155 | * `Math.PI + PI_LO` represents π with an error below `1.3·10⁻³²`. |
| 156 | */ |
| 157 | const PI_LO = 1.2246467991473532e-16; |
| 158 | |
| 159 | /** |
| 160 | * The sign of `x − m·π` for the REAL number π, where `m` is an integer or |
| 161 | * an integer plus one half (the poles of `tan` and `sec` are at |
| 162 | * `(n + ½)·π`, those of `cot` and `csc` at `n·π`), or `undefined` when the |
| 163 | * computation below cannot decide it. |
| 164 | * |
| 165 | * No pole other than 0 is a double, so an endpoint is never exactly at one, |
| 166 | * but it can be within an ulp of one on either side: `Math.PI / 2` is BELOW |
| 167 | * `π/2`, so `tan` is finite (about `1.6·10¹⁶`) at that endpoint, while the |
| 168 | * next double up is above `π/2`. Which side the endpoint is on decides the |
| 169 | * answer of the kernel, so it is decided with `m·π` in double-double |
| 170 | * arithmetic: `m·Math.PI = h + l` exactly (TwoProduct), and |
| 171 | * `m·(π − Math.PI) ≈ m·PI_LO`. Near the pole `x − h` is exact (the two are |
| 172 | * within a factor of 2), and the two remaining subtractions and the error of |
| 173 | * `PI_LO` are bounded; the sign is answered only when the difference is |
| 174 | * larger than that bound. |
| 175 | */ |
| 176 | function compareWithPiMultiple(x: number, m: number): -1 | 0 | 1 | undefined { |
| 177 | if (!Number.isFinite(x)) return x > 0 ? 1 : x < 0 ? -1 : undefined; |
| 178 | if (m === 0) return x > 0 ? 1 : x < 0 ? -1 : 0; |
| 179 | const h = m * Math.PI; |
| 180 | const l = productError(m, Math.PI, h); |
| 181 | const t = m * PI_LO; |
| 182 | const d1 = x - h; |
| 183 | // Far from the pole the rounded difference decides. |
| 184 | if (Math.abs(d1) > 4 * (Math.abs(l) + Math.abs(t))) return d1 > 0 ? 1 : -1; |
| 185 | const d = d1 - l - t; |
| 186 | const bound = |
| 187 | Math.abs(m) * 4e-32 + 4.5e-16 * (Math.abs(d1) + Math.abs(l) + Math.abs(t)); |
| 188 | if (Math.abs(d) <= bound) return undefined; |
no test coverage detected