(a, g)
| 8 | */ |
| 9 | |
| 10 | export const agm = (a, g) => { |
| 11 | if (a === Infinity && g === 0) return NaN |
| 12 | if (Object.is(a, -0) && !Object.is(g, -0)) return 0 |
| 13 | if (a === g) return a // avoid rounding errors, and increase efficiency |
| 14 | let x // temp var |
| 15 | do { |
| 16 | ;[a, g, x] = [(a + g) / 2, Math.sqrt(a * g), a] |
| 17 | } while (a !== x && !isNaN(a)) |
| 18 | /* |
| 19 | `x !== a` ensures the return value has full precision, |
| 20 | and prevents infinite loops caused by rounding differences between `div` and `sqrt` (no need for "epsilon"). |
| 21 | If we were to compare `a` with `g`, some input combinations (not all) can cause an infinite loop, |
| 22 | because the rounding mode never changes at runtime. |
| 23 | Precision is not the same as accuracy, but they're related. |
| 24 | This function isn't always 100% accurate (round-errors), but at least is more than 95% accurate. |
| 25 | `!isNaN(x)` prevents infinite loops caused by invalid inputs like: negatives, NaNs and Infinities. |
| 26 | */ |
| 27 | return a |
| 28 | } |
no outgoing calls
no test coverage detected