Scripts: concatenateMatrix takes the exact sum near the overflow point

The quick path in products_sum returned a finite coefficient whenever the
rounded sum was finite. Rounding a product first can drop the part that
puts the exact sum past the overflow midpoint, so a coefficient that
should throw a RangeError came back as Number.MAX_VALUE. The quick path
now runs only when the rounded sum is below half the largest number;
every larger sum goes through the exact expansion and rounds once.
This commit is contained in:
dotCooCoo
2026-11-28 16:00:37 -08:00
parent 6b7e9f01a2
commit b32b14dc57
2 changed files with 8 additions and 3 deletions
+2 -2
View File
@@ -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.
+6 -1
View File
@@ -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]