Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
puzzlet committed Sep 18, 2024
1 parent ac315da commit 227b2c3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
24 changes: 12 additions & 12 deletions egyptianNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class EgyptianNumber {
];
static readonly glyphToNumber: ReadonlyMap<string, number> = new Map<string, number>();

readonly nominator: bigint;
readonly numerator: bigint;
readonly denominator: bigint;
private _normalized: readonly [bigint, bigint][];
private _hieroglyphs: string;
Expand All @@ -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 {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand All @@ -169,19 +169,19 @@ 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 {
return EgyptianNumber.subtract(a, b);
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion seshatToJavascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SeshatToJavascript extends AbstractParseTreeVisitor<string> 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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/js/negativeFraction.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);

0 comments on commit 227b2c3

Please sign in to comment.