Skip to content

Commit

Permalink
feat: mod wip
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Apr 13, 2024
1 parent 5aef5ef commit 21af47a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ Quantity.isQuantity(new Quantity(15n, 4n));

Get if a provided value is a valid `Quantity` of a `Token` instance. This will compare the denomination used in the provided `Quantity` instance:


```ts
// CRED has a denomination of 3

Expand Down Expand Up @@ -241,7 +240,7 @@ console.log("qty2 is", qty2.toString());

#### Math and utilities

These functions have both static and non-static implementations. Static implementations will always start with two "`_`" and create new `Quantity` instances. Non-static implementations start with one "`_`" and modify themselves (the instances they are associated with).
These functions have both static and non-static implementations. Static implementations will always start with two "`_`" and create new `Quantity` instances. Non-static implementations start with one "`_`" and modify themselves (the instances they are associated with). Calling static functions with tokens of different denominations will cause the result to have the higher denomination by default. Non-static functions (that are modified in-place) keep their denominations, but might lose precision in doing so.

##### Math operators

Expand Down
27 changes: 27 additions & 0 deletions src/Quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,33 @@ export default class Quantity {
this.#qty = res.#qty;
}

/**
* Get the remainder left when x is divided by y
* @param x Quantity to divide
* @param y Quantity to divide with
* @returns The remaineder left after the operation
*/
static __mod(x: Quantity, y: Quantity) {
[x, y] = Quantity.sameDenomination(x, y);

return new Quantity(
x.#qty * 10n ** x.#D % y.#qty,
x.#D
);
}

/**
* Get the remainder left when divided by y (in-place)
* @param y Quantity to divide with
*/
_mod(y: Quantity) {
const res = Quantity.__convert(
Quantity.__mod(this, y),
this.#D
);
this.#qty = res.#qty;
}

/**
* Other Math functions
*/
Expand Down
16 changes: 16 additions & 0 deletions src/quantity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ describe("Quantity testing", () => {
expect(inst1.toString()).toEqual("6.25");
});

test("Static remainder", () => {
const inst1 = new Quantity(400n, 2n);
const inst2 = new Quantity(3000n, 3n);

const res = Quantity.__mod(inst1, inst2);

expect(res.toString()).toEqual("1");
});

test("In-place remainder", () => {
const inst1 = new Quantity(600000n, 5n);
inst1._mod(new Quantity(400n, 2n));

expect(inst1.toString()).toEqual("2");
});

test("Quantity min()", () => {
const min = new Quantity(1n, 10n);
const list = [new Quantity(456n, 2n), min, new Quantity(1n, 5n)];
Expand Down

0 comments on commit 21af47a

Please sign in to comment.