Skip to content

Commit 3dd62bb

Browse files
committed
If math.Fraction is given a non-integer, numerator and denominator are scaled up to integers
Someone reported that matrixmath.inverse was returning something like the identity matrix for any 3×3 rotation matrix. `matrixmath.inverse` works by converting the matrix to a matrix of Fractions, then performing Gauss-Jordan elimination on them. The Fraction constructor rounds off its inputs, because it's expecting integers on top and bottom. This commit changes the Fraction constructor so both numerator and denominator are multiplied by powers of 2 until they're both integers. This necessarily results in some inaccuracy due to floating point error, but at least it's not completely wrong. I tried explicitly writing out the routine for inverting 3×3 matrices, but it turned out to be less accurate than this method. The floating point error must accumulate because there are more float operations.
1 parent 4330e20 commit 3dd62bb

4 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎runtime/scripts/math.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,10 @@ var Fraction = math.Fraction = function(numerator,denominator) {
22612261
numerator = -numerator;
22622262
denominator = -denominator;
22632263
}
2264+
while(numerator % 1 != 0 || denominator % 1 != 0) {
2265+
numerator *= 2;
2266+
denominator *= 2;
2267+
}
22642268
this.numerator = Math.round(numerator);
22652269
this.denominator = Math.round(denominator);
22662270
}

‎tests/jme-runtime.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4498,6 +4498,10 @@ var Fraction = math.Fraction = function(numerator,denominator) {
44984498
numerator = -numerator;
44994499
denominator = -denominator;
45004500
}
4501+
while(numerator % 1 != 0 || denominator % 1 != 0) {
4502+
numerator *= 2;
4503+
denominator *= 2;
4504+
}
45014505
this.numerator = Math.round(numerator);
45024506
this.denominator = Math.round(denominator);
45034507
}

‎tests/jme/jme-tests.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,8 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
12551255

12561256
deepCloseEqual(assert, evaluate('id(4)[0..2]').value, [[1,0,0,0], [0,1,0,0]], 'id(4)[0..2]');
12571257

1258+
deepCloseEqual(assert, evaluate(`let(m,matrix([0.1, 0.12, 0.123],[0.1234, 0.12345, 0.123456],[0.1234567, 0.12345678, 0.123456789]), precround(m*inverse(m),10))`).value, [[1,0,0],[0,1,0],[0,0,1]], 'inverse of a matrix with non-integer components is correct');
1259+
12581260
var m1 = [[1]];
12591261
m1.rows = 1;
12601262
m1.columns = 1;

‎tests/numbas-runtime.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4499,6 +4499,10 @@ var Fraction = math.Fraction = function(numerator,denominator) {
44994499
numerator = -numerator;
45004500
denominator = -denominator;
45014501
}
4502+
while(numerator % 1 != 0 || denominator % 1 != 0) {
4503+
numerator *= 2;
4504+
denominator *= 2;
4505+
}
45024506
this.numerator = Math.round(numerator);
45034507
this.denominator = Math.round(denominator);
45044508
}

0 commit comments

Comments
 (0)