Skip to content

Truly independent web browser

LibJS: Compute Math.hypot() with ak::math::hypot

Shannon Booth85afacea93a8
diff --git a/Libraries/LibJS/Rust/src/runtime/math_object.rs b/Libraries/LibJS/Rust/src/runtime/math_object.rs
index ba9971f9c11..f52d6a36f8d 100644
--- a/Libraries/LibJS/Rust/src/runtime/math_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/math_object.rs
@@ -653,41 +653,30 @@ impl MathObject {
let coerced = CoercedArguments::of(vm)?;
// 3. For each element number of coerced, do
- for number in coerced.iter() {
- // a. If number is either +βˆžπ”½ or -βˆžπ”½, return +βˆžπ”½.
- if number.is_infinite() {
- return Ok(js_infinity());
- }
+ // a. If number is either +βˆžπ”½ or -βˆžπ”½, return +βˆžπ”½.
+ if coerced.iter().any(|number| number.is_infinite()) {
+ return Ok(js_infinity());
}
// 4. Let onlyZero be true.
- let mut only_zero = true;
-
- let mut sum_of_squares = 0.0;
-
// 5. For each element number of coerced, do
- for &number in coerced.iter() {
- // a. If number is NaN, return NaN.
- // OPTIMIZATION: For infinities, the result will be infinity with the same sign, so we can return early.
- if number.is_nan() || number.is_infinite() {
- return Ok(Value::from_f64(number));
- }
-
- // b. If number is neither +0𝔽 nor -0𝔽, set onlyZero to false.
- if number != 0.0 {
- only_zero = false;
- }
-
- sum_of_squares += number * number;
+ // a. If number is NaN, return NaN.
+ if coerced.iter().any(|number| number.is_nan()) {
+ return Ok(js_nan());
}
+ // b. If number is neither +0𝔽 nor -0𝔽, set onlyZero to false.
// 6. If onlyZero is true, return +0𝔽.
- if only_zero {
- return Ok(Value::from_i32(0));
- }
-
// 7. Return an implementation-approximated Number value representing the square root of the sum of squares of the mathematical values of the elements of coerced.
- Ok(Value::from_f64(f64::sqrt(sum_of_squares)))
+ // NB: hypot() scales its inputs and maps zeros to +0, which covers steps 5.b to 7. NaN has to be checked first,
+ // since a partial result that overflows to infinity would win over it.
+ let result = match &*coerced {
+ [] => 0.0,
+ [first, rest @ ..] => rest
+ .iter()
+ .fold(first.abs(), |result, &number| math::hypot(result, number)),
+ };
+ Ok(Value::from_f64(result))
}
// 21.3.2.20 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
diff --git a/Tests/LibJS/Runtime/builtins/Math/Math.hypot.js b/Tests/LibJS/Runtime/builtins/Math/Math.hypot.js
index ee4b1ea1b70..dc6cfc636b5 100644
--- a/Tests/LibJS/Runtime/builtins/Math/Math.hypot.js
+++ b/Tests/LibJS/Runtime/builtins/Math/Math.hypot.js
@@ -7,3 +7,26 @@ test("basic functionality", () => {
expect(Math.hypot(3, 4, "5")).toBeCloseTo(7.0710678118654755);
expect(Math.hypot(-3)).toBe(3);
});
+
+test("does not overflow or underflow", () => {
+ expect(Math.hypot(1e200, 1e200)).toBe(1.414213562373095e200);
+ expect(Math.hypot(1e-200, 1e-200)).toBe(1.414213562373095e-200);
+ expect(Math.hypot(Number.MAX_VALUE, Number.MAX_VALUE)).toBe(Infinity);
+ expect(Math.hypot(Number.MIN_VALUE, -Number.MIN_VALUE)).toBe(Number.MIN_VALUE);
+ expect(Math.hypot(1e200, 0, 0, 1e200)).toBe(1.414213562373095e200);
+ expect(Math.hypot(1e200, 1e200, 1e200)).toBe(1.7320508075688773e200);
+});
+
+test("is correctly rounded", () => {
+ expect(Math.hypot(0.8776669492836708, 0.2058533185844357)).toBe(0.9014848099869076);
+ expect(Math.hypot(1, 1e-9)).toBe(1);
+});
+
+test("infinity wins over NaN, and NaN over everything else", () => {
+ expect(Math.hypot(NaN, -Infinity)).toBe(Infinity);
+ expect(Math.hypot(-Infinity, NaN)).toBe(Infinity);
+ expect(Math.hypot(3, NaN, 4)).toBe(NaN);
+ expect(Math.hypot(Number.MAX_VALUE, Number.MAX_VALUE, NaN)).toBe(NaN);
+ expect(Math.hypot(-0, -0)).toBe(0);
+ expect(Object.is(Math.hypot(-0), 0)).toBeTrue();
+});