Commit 3b4f4a25 authored by Moody Salem's avatar Moody Salem

Add #remainder to the fraction entity

parent 22453c0c
......@@ -37,6 +37,11 @@ export class Fraction {
return JSBI.divide(this.numerator, this.denominator)
}
// remainder after floor division
get remainder(): Fraction {
return this.subtract(this.quotient)
}
invert(): Fraction {
return new Fraction(this.denominator, this.numerator)
}
......
......@@ -9,6 +9,19 @@ describe.only('Fraction', () => {
expect(new Fraction(JSBI.BigInt(16), JSBI.BigInt(5)).quotient).toEqual(JSBI.BigInt(3)) // one above
})
})
describe('#remainder', () => {
it('returns fraction after divison', () => {
expect(new Fraction(JSBI.BigInt(8), JSBI.BigInt(3)).remainder).toEqual(
new Fraction(JSBI.BigInt(2), JSBI.BigInt(3))
)
expect(new Fraction(JSBI.BigInt(12), JSBI.BigInt(4)).remainder).toEqual(
new Fraction(JSBI.BigInt(0), JSBI.BigInt(4))
)
expect(new Fraction(JSBI.BigInt(16), JSBI.BigInt(5)).remainder).toEqual(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(5))
)
})
})
describe('#invert', () => {
it('flips num and denom', () => {
expect(new Fraction(JSBI.BigInt(5), JSBI.BigInt(10)).invert().numerator).toEqual(JSBI.BigInt(10))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment