Skip to content

Commit

Permalink
Avoid IEEE simplification as a side effect of defloating
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Feb 8, 2024
1 parent a566798 commit 1146d5a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/__tests__/fraction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,19 @@ describe('Fraction', () => {
expect(lensSum.lensSub(b).equals(a)).toBe(true);
expect(lensSum.lensSub(a).equals(b)).toBe(true);
});

it.fails('blows up on repeated division', () => {
let foo = new Fraction('3/2');
const bar = new Fraction('103/101');
for (let i = 0; i < 10; ++i) {
foo = foo.div(bar);
}
});

it.fails('blows up on repeated multiplication', () => {
let foo = new Fraction('103/101');
for (let i = 0; i < 4; ++i) {
foo = foo.mul(foo);
}
});
});
15 changes: 6 additions & 9 deletions src/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export class Fraction {
this.s = Math.sign(numerator * denominator);
this.n = Math.abs(numerator);
this.d = Math.abs(denominator);
if (this.n > Number.MAX_SAFE_INTEGER) {
throw new Error('Numerator above safe limit');
}
if (this.d > Number.MAX_SAFE_INTEGER) {
throw new Error('Denominator above safe limit');
}
if (this.d === 0) {
throw new Error('Division by Zero');
}
Expand Down Expand Up @@ -206,15 +212,6 @@ export class Fraction {
if (isNaN(this.s) || isNaN(this.n) || isNaN(this.d)) {
throw new Error('Cannot represent NaN as a fraction');
}
/*
if (!isFinite(this.d)) {
if (!isFinite(this.n)) {
throw new Error('Both numerator and denominator cannot be infinite');
}
this.n = 0;
this.d = 1;
}
*/
if (this.n > Number.MAX_SAFE_INTEGER) {
throw new Error('Numerator above safe limit');
}
Expand Down

0 comments on commit 1146d5a

Please sign in to comment.