Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ab7a98

Browse files
authored
Better doc, and corrected some formatting
1 parent 6ab3f4b commit 6ab7a98

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

‎Maths/ArithmeticGeometricMean.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99

1010
export const agm = (a, g) => {
1111
if (a === g) return a; //avoid rounding errors, and increase efficiency
12-
let x; //temp var, for detecting rounding differences between `sqrt` and division
12+
let x; //temp var
1313
do {
1414
[a, g, x] = [(a + g) / 2, Math.sqrt(a * g), a]
1515
} while (a !== x && !isNaN(a));
1616
/*
1717
`x !== a` ensures the return value has full precision,
18-
and prevents infinite loops caused by rounding errors (no need for "epsilon").
18+
and prevents infinite loops caused by rounding differences between `div` and `sqrt` (no need for "epsilon").
19+
If we were to compare `a` with `g`, some input combinations (not all) can cause an infinite loop,
20+
because the rounding mode never changes at runtime.
1921
Precision is not the same as accuracy, but they're related.
2022
This function isn't always 100% accurate (round-errors), but at least is more than 95% accurate.
21-
`!isNaN(x)` prevents infinite loops caused by invalid inputs like: negatives, NaNs and both Infinities.
23+
`!isNaN(x)` prevents infinite loops caused by invalid inputs like: negatives, NaNs and Infinities.
2224
*/
23-
return a
25+
return a;
2426
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /