diff --git a/crates/engine/src/script/dom/matrix.rs b/crates/engine/src/script/dom/matrix.rs index bae77602cc..211ff9e8b0 100644 --- a/crates/engine/src/script/dom/matrix.rs +++ b/crates/engine/src/script/dom/matrix.rs @@ -103,7 +103,7 @@ fn then(m: Matrix, n: Matrix) -> Matrix { ] } -/// `x·y + z·w + t`. When both products and the whole sum are finite, and a product at or below the +/// `x·y + z·w + t`. When the whole sum is below half the largest number, and a product at or below the /// smallest normal number is the only term that is not 0, the sum is taken as numbers are. /// Otherwise the products are summed past the range of a number and rounded at the end, so a /// finite value is not lost to a product too large or too small for a number, or to a sum that @@ -115,7 +115,7 @@ fn products_sum(x: f64, y: f64, z: f64, w: f64, t: f64) -> f64 { let alone = |a: f64, b: f64| (a == 0.0 || b == 0.0) && t == 0.0; let plain = p + q; let total = plain + t; - if total.is_finite() && (!lost(p, x, y) || alone(z, w)) && (!lost(q, z, w) || alone(x, y)) { + if total.abs() < f64::MAX / 2.0 && (!lost(p, x, y) || alone(z, w)) && (!lost(q, z, w) || alone(x, y)) { return total; } // Each product is split into its rounded value and the part that rounding drops, both exact. diff --git a/crates/engine/src/tests/script_dom_prefs.rs b/crates/engine/src/tests/script_dom_prefs.rs index fae628e3c5..e41bb319a5 100644 --- a/crates/engine/src/tests/script_dom_prefs.rs +++ b/crates/engine/src/tests/script_dom_prefs.rs @@ -376,6 +376,8 @@ fn concatenation_rounds_once_at_the_smallest_normal_number_and_the_largest_numbe let mut s = Session::new(); // N·(1 - 2^-53) is N - S/2, which rounds up to N; the exact A is -1.5·S, which rounds to -2·S. // MAX - 2^969 + 2^970 rounds to MAX, while adding the rounded MAX - 2^969 and 2^970 overflows. + // MAX·(1 - 2^-53) + 2^971 + 2^970 - 2^917 is past the overflow midpoint by 2^917, which rounding + // the first product drops. let out = log( &mut s, "function mat(v) { return {mValueA: v[0], mValueB: v[1], mValueC: v[2], mValueD: v[3], mValueTX: v[4], mValueTY: v[5]}; }\ @@ -385,9 +387,12 @@ fn concatenation_rounds_once_at_the_smallest_normal_number_and_the_largest_numbe var shift = mat([1, 0, -1, 1, Math.pow(2, 970), 0]);\ vc.log(app.concatenateMatrix(mat([1, 0, 0, 1, Number.MAX_VALUE, Math.pow(2, 969)]), shift).mValueTX === Number.MAX_VALUE);\ try { app.concatenateMatrix(mat([1, 0, 0, 1, Number.MAX_VALUE, 0]), shift); vc.log('finite'); }\ + catch (e) { vc.log(e.name); }\ + var near = mat([1 - Math.pow(2, -53), 0, 1, 1, Math.pow(2, 970) - Math.pow(2, 917), 0]);\ + try { app.concatenateMatrix(mat([1, 0, 0, 1, Number.MAX_VALUE, Math.pow(2, 971)]), near); vc.log('finite'); }\ catch (e) { vc.log(e.name); }", ); - assert_eq!(out, vec![json!(true), json!(true), json!(true), json!("RangeError")]); + assert_eq!(out, vec![json!(true), json!(true), json!(true), json!("RangeError"), json!("RangeError")]); } #[test]