Skip to content

Commit

Permalink
feat: support negative integer exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Apr 15, 2024
1 parent fbe0def commit 635b851
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,19 @@ export default class Quantity {

// integer power
if (typeof y === "bigint") {
let newRaw = x.#qty ** (y > 0 ? y : -y);

// TODO: precision
if (y < 0) newRaw = 1n / newRaw;

return new Quantity(
newRaw,
const positivePower = new Quantity(
x.#qty ** (y > 0 ? y : -y),
x.#D ** y
)._convert(x.#D);

// negative exponent
if (y > 0) return positivePower;

// calculate negative exponent
const result = Quantity.__one(x.#D);
result._div(positivePower);

return result;
}

// ensure same denomination
Expand Down

0 comments on commit 635b851

Please sign in to comment.