diff --git a/README.md b/README.md index 6386385..1d79fa1 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Among the oldest mathematical texts in human history are Rhind Mathematical Papy > Make 10 _heqats_ of the fat into _ro_; it shall become 3200. > Make a year into days; it shall become 365. > Divide 3200 by 365; it shall become 8 + 2/3 + 1/10 + 1/2190. -> Make this in [_haqet_ and _ro_ notation]; 1/64 [_haqet_ and] 1/3 + 1/10 + 1/2190 _ro_. +> Make this in [_haqet_ and _ro_ notation]; 1/64 [_haqet_ and] 2/3 + 1/10 + 1/2190 _ro_. > > -- from Rhind Mathematical Papyrus Problem 66 @@ -35,9 +35,10 @@ x = x / days_in_a_year assert x == 8 + 2/3 + 1/10 + 1/2190 result = ro_to_haqet_and_ro(x) assert result.haqet == 1/64 -assert result.ro == 1/13 + 1/10 + 1/2190 +assert result.ro == 2/3 + 1/10 + 1/2190 ``` + ### Binary calculations For multiplications and divisions, Egyptians used a method called "doubling." For example, to calculate 2000 * 5, they simply doubled 2000 until the multiplier reachs high enough for 5, and added things up: diff --git a/egyptianNumber.ts b/egyptianNumber.ts index 80c60a3..f44436e 100644 --- a/egyptianNumber.ts +++ b/egyptianNumber.ts @@ -50,7 +50,7 @@ export class EgyptianNumber { ]; static readonly glyphToNumber: ReadonlyMap = new Map(); - readonly nominator: bigint; + readonly numerator: bigint; readonly denominator: bigint; private _normalized: readonly [bigint, bigint][]; private _hieroglyphs: string; @@ -74,18 +74,18 @@ export class EgyptianNumber { }); } - constructor(nominator: number | bigint, denominator: number | bigint = 1) { - const gcd = EgyptianNumber.gcd(nominator, denominator); - this.nominator = BigInt(nominator) / gcd; + constructor(numerator: number | bigint, denominator: number | bigint = 1) { + const gcd = EgyptianNumber.gcd(numerator, denominator); + this.numerator = BigInt(numerator) / gcd; this.denominator = BigInt(denominator) / gcd; if (this.denominator < 0) { - this.nominator = -this.nominator; + this.numerator = -this.numerator; this.denominator = -this.denominator; } } toString(): string { - return `EgyptianNumber(${this.nominator}, ${this.denominator}) ${this.toHieroglyphs()}`; + return `EgyptianNumber(${this.numerator}, ${this.denominator}) ${this.toHieroglyphs()}`; } toHieroglyphs(): string { @@ -109,7 +109,7 @@ export class EgyptianNumber { get normalized(): readonly [bigint, bigint][] { if (this._normalized) return this._normalized; - let nom: bigint = this.nominator; + let nom: bigint = this.numerator; let denom: bigint = this.denominator; let normalized: [bigint, bigint][] = []; if (nom == BigInt(0)) { @@ -153,11 +153,11 @@ export class EgyptianNumber { } isEqualTo(a: EgyptianNumber): boolean { - return this.denominator * a.nominator == a.denominator * this.nominator; + return this.denominator * a.numerator == a.denominator * this.numerator; } static add(a: EgyptianNumber, b: EgyptianNumber): EgyptianNumber { - return new EgyptianNumber(a.nominator * b.denominator + b.nominator * a.denominator, a.denominator * b.denominator); + return new EgyptianNumber(a.numerator * b.denominator + b.numerator * a.denominator, a.denominator * b.denominator); } static addAll(...summands: EgyptianNumber[]): EgyptianNumber { @@ -169,7 +169,7 @@ export class EgyptianNumber { } static subtract(a: EgyptianNumber, b: EgyptianNumber): EgyptianNumber { - return new EgyptianNumber(a.nominator * b.denominator - b.nominator * a.denominator, a.denominator * b.denominator); + return new EgyptianNumber(a.numerator * b.denominator - b.numerator * a.denominator, a.denominator * b.denominator); } static reverseSubtract(b: EgyptianNumber, a: EgyptianNumber): EgyptianNumber { @@ -177,11 +177,11 @@ export class EgyptianNumber { } static multiply(a: EgyptianNumber, b: EgyptianNumber): EgyptianNumber { - return new EgyptianNumber(a.nominator * b.nominator, a.denominator * b.denominator); + return new EgyptianNumber(a.numerator * b.numerator, a.denominator * b.denominator); } static divide(a: EgyptianNumber, b: EgyptianNumber): EgyptianNumber { - return new EgyptianNumber(a.nominator * b.denominator, a.denominator * b.nominator); + return new EgyptianNumber(a.numerator * b.denominator, a.denominator * b.numerator); } static parseIntegerFromString(str: string | undefined): number { diff --git a/seshatToJavascript.ts b/seshatToJavascript.ts index 945a0e0..352d6a5 100644 --- a/seshatToJavascript.ts +++ b/seshatToJavascript.ts @@ -74,7 +74,7 @@ export class SeshatToJavascript extends AbstractParseTreeVisitor impleme visitNumber(ctx: NumberContext | undefined): string { if (! ctx) return ''; const parsed = this.parseNumber(ctx); - return `(new seshatEnvironment.EgyptianNumber(${parsed.nominator}, ${parsed.denominator}))`; + return `(new seshatEnvironment.EgyptianNumber(${parsed.numerator}, ${parsed.denominator}))`; } parseNumber(ctx: NumberContext): EgyptianNumber { diff --git a/tests/js/negativeFraction.mjs b/tests/js/negativeFraction.mjs index 062ecc5..8b2ef9e 100644 --- a/tests/js/negativeFraction.mjs +++ b/tests/js/negativeFraction.mjs @@ -5,16 +5,16 @@ import { EgyptianNumber } from "../../build/lib/egyptianNumber.js"; let x; x = new EgyptianNumber(-1, 2); -assert.equal(x.nominator, -1); +assert.equal(x.numerator, -1); assert.equal(x.denominator, 2); assert.deepEqual(x.normalized, [[0, -1], [1, -2]]); x = new EgyptianNumber(-2, 6); -assert.equal(x.nominator, -1); +assert.equal(x.numerator, -1); assert.equal(x.denominator, 3); assert.deepEqual(x.normalized, [[0, -1], [1, -3]]); x = new EgyptianNumber(12, -20); -assert.equal(x.nominator, -3); +assert.equal(x.numerator, -3); assert.equal(x.denominator, 5); assert.deepEqual(x.normalized, [[0, -1], [1, -2], [1, -10]]);