Skip to content

Commit

Permalink
Corrected pow. Bump version to 0.1.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinadam committed Aug 5, 2024
1 parent 3a41694 commit 42f66c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
31 changes: 29 additions & 2 deletions Decimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,29 +385,40 @@ Deno.test('mod', () => {

Deno.test('pow', () => {
const vectors: ({ a: Decimal; b: number } & ({ success: true; output: Decimal } | { success: false }))[] = [
{ a: Decimal.from(2), b: 3, success: true, output: Decimal.from(8) },
{ a: Decimal.from(2), b: 2, success: true, output: Decimal.from(4) },
{ a: Decimal.from(2), b: 1, success: true, output: Decimal.from(2) },
{ a: Decimal.from(2), b: 0, success: true, output: Decimal.from(1) },
{ a: Decimal.from(2), b: 2, success: true, output: Decimal.from(4) },
{ a: Decimal.from(2), b: -1, success: true, output: Decimal.from(0.5) },
{ a: Decimal.from(2), b: -2, success: true, output: Decimal.from(0.25) },
{ a: Decimal.from(2), b: -3, success: true, output: Decimal.from(0.125) },
{ a: Decimal.from(0.2), b: 3, success: true, output: Decimal.from(0.008) },
{ a: Decimal.from(0.2), b: 2, success: true, output: Decimal.from(0.04) },
{ a: Decimal.from(0.2), b: 1, success: true, output: Decimal.from(0.2) },
{ a: Decimal.from(0.2), b: 0, success: true, output: Decimal.from(1) },
{ a: Decimal.from(0.2), b: 2, success: true, output: Decimal.from(0.04) },
{ a: Decimal.from(0.2), b: -1, success: true, output: Decimal.from(5) },
{ a: Decimal.from(0.2), b: -2, success: true, output: Decimal.from(25) },
{ a: Decimal.from(0.2), b: -3, success: true, output: Decimal.from(125) },
{ a: Decimal.from(3), b: 3, success: true, output: Decimal.from(27) },
{ a: Decimal.from(3), b: 2, success: true, output: Decimal.from(9) },
{ a: Decimal.from(3), b: 1, success: true, output: Decimal.from(3) },
{ a: Decimal.from(3), b: 0, success: true, output: Decimal.from(1) },
{ a: Decimal.from(3), b: 2, success: true, output: Decimal.from(9) },
{ a: Decimal.from(3), b: -1, success: false },
{ a: Decimal.from(3), b: -2, success: false },
{ a: Decimal.from(3), b: -3, success: false },
];
for (const vector of vectors) {
const { a, b } = vector;
if (vector.success) {
const output = vector.output;
const vectors = [{ a, b, output }, { a: a.neg(), b, output: b % 2 == 0 ? output : output.neg() }];
for (const { a, b, output } of vectors) {
assert(a.pow(b).eq(output));
assert(
a.pow(b).eq(output),
`a: ${a.toString()}, b: ${b}, output: ${output.toString()}, result: ${a.pow(b).toString()}`,
);
}
} else {
assert(wrap(() => a.pow(b)) instanceof Error);
Expand Down Expand Up @@ -576,3 +587,19 @@ Deno.test('lte0', () => {
assert(input.lte0() === output);
}
});

Deno.test('e', () => {
const vectors = [
{ input: Decimal.from(12.3), exponent: 3, output: Decimal.from(12300) },
{ input: Decimal.from(12.3), exponent: 2, output: Decimal.from(1230) },
{ input: Decimal.from(12.3), exponent: 1, output: Decimal.from(123) },
{ input: Decimal.from(12.3), exponent: 0, output: Decimal.from(12.3) },
{ input: Decimal.from(12.3), exponent: -1, output: Decimal.from(1.23) },
{ input: Decimal.from(12.3), exponent: -2, output: Decimal.from(0.123) },
{ input: Decimal.from(12.3), exponent: -3, output: Decimal.from(0.0123) },
];
for (const { input, exponent, output } of vectors) {
assert(input.e(exponent).eq(output));
assert(input.neg().e(exponent).eq(output.neg()));
}
});
17 changes: 7 additions & 10 deletions Decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class Decimal {
* @param exponent The integer exponent to raise 10 to.
* @returns A new Decimal instance representing the result.
*/
e(exponent: bigint | number): Decimal {
e(exponent: number): Decimal {
return this.mul(new Decimal(10n).pow(exponent));
}

Expand Down Expand Up @@ -386,19 +386,16 @@ export default class Decimal {
* @param value The integer exponent to raise to.
* @returns A new Decimal instance representing the result of the exponentiation.
*/
pow(value: bigint | number): Decimal {
if (typeof value === 'number') {
assert(Number.isInteger(value), `Exponent must be an integer, got ${value}`);
value = BigInt(value);
}
if (value === 0n) {
pow(value: number): Decimal {
assert(Number.isInteger(value), `Exponent must be an integer, got ${value}`);
if (value === 0) {
return Decimal.one;
} else if (value === 1n) {
} else if (value === 1) {
return this;
} else if (value < 0n) {
} else if (value < 0) {
return this.inv().pow(-value);
} else {
return new Decimal(this.mantissa ** value, this.exponent * 2);
return new Decimal(this.mantissa ** BigInt(value), this.exponent * value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quentinadam/decimal",
"version": "0.1.4",
"version": "0.1.5",
"exports": "./Decimal.ts",
"imports": {
"@quentinadam/assert": "jsr:@quentinadam/assert@^0.1.6"
Expand Down

0 comments on commit 42f66c1

Please sign in to comment.