Skip to content

Commit 42f66c1

Browse files
committed
Corrected pow. Bump version to 0.1.5.
1 parent 3a41694 commit 42f66c1

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

Decimal.test.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -385,29 +385,40 @@ Deno.test('mod', () => {
385385

386386
Deno.test('pow', () => {
387387
const vectors: ({ a: Decimal; b: number } & ({ success: true; output: Decimal } | { success: false }))[] = [
388+
{ a: Decimal.from(2), b: 3, success: true, output: Decimal.from(8) },
389+
{ a: Decimal.from(2), b: 2, success: true, output: Decimal.from(4) },
388390
{ a: Decimal.from(2), b: 1, success: true, output: Decimal.from(2) },
389391
{ a: Decimal.from(2), b: 0, success: true, output: Decimal.from(1) },
390392
{ a: Decimal.from(2), b: 2, success: true, output: Decimal.from(4) },
391393
{ a: Decimal.from(2), b: -1, success: true, output: Decimal.from(0.5) },
392394
{ a: Decimal.from(2), b: -2, success: true, output: Decimal.from(0.25) },
395+
{ a: Decimal.from(2), b: -3, success: true, output: Decimal.from(0.125) },
396+
{ a: Decimal.from(0.2), b: 3, success: true, output: Decimal.from(0.008) },
397+
{ a: Decimal.from(0.2), b: 2, success: true, output: Decimal.from(0.04) },
393398
{ a: Decimal.from(0.2), b: 1, success: true, output: Decimal.from(0.2) },
394399
{ a: Decimal.from(0.2), b: 0, success: true, output: Decimal.from(1) },
395400
{ a: Decimal.from(0.2), b: 2, success: true, output: Decimal.from(0.04) },
396401
{ a: Decimal.from(0.2), b: -1, success: true, output: Decimal.from(5) },
397402
{ a: Decimal.from(0.2), b: -2, success: true, output: Decimal.from(25) },
403+
{ a: Decimal.from(0.2), b: -3, success: true, output: Decimal.from(125) },
404+
{ a: Decimal.from(3), b: 3, success: true, output: Decimal.from(27) },
405+
{ a: Decimal.from(3), b: 2, success: true, output: Decimal.from(9) },
398406
{ a: Decimal.from(3), b: 1, success: true, output: Decimal.from(3) },
399407
{ a: Decimal.from(3), b: 0, success: true, output: Decimal.from(1) },
400-
{ a: Decimal.from(3), b: 2, success: true, output: Decimal.from(9) },
401408
{ a: Decimal.from(3), b: -1, success: false },
402409
{ a: Decimal.from(3), b: -2, success: false },
410+
{ a: Decimal.from(3), b: -3, success: false },
403411
];
404412
for (const vector of vectors) {
405413
const { a, b } = vector;
406414
if (vector.success) {
407415
const output = vector.output;
408416
const vectors = [{ a, b, output }, { a: a.neg(), b, output: b % 2 == 0 ? output : output.neg() }];
409417
for (const { a, b, output } of vectors) {
410-
assert(a.pow(b).eq(output));
418+
assert(
419+
a.pow(b).eq(output),
420+
`a: ${a.toString()}, b: ${b}, output: ${output.toString()}, result: ${a.pow(b).toString()}`,
421+
);
411422
}
412423
} else {
413424
assert(wrap(() => a.pow(b)) instanceof Error);
@@ -576,3 +587,19 @@ Deno.test('lte0', () => {
576587
assert(input.lte0() === output);
577588
}
578589
});
590+
591+
Deno.test('e', () => {
592+
const vectors = [
593+
{ input: Decimal.from(12.3), exponent: 3, output: Decimal.from(12300) },
594+
{ input: Decimal.from(12.3), exponent: 2, output: Decimal.from(1230) },
595+
{ input: Decimal.from(12.3), exponent: 1, output: Decimal.from(123) },
596+
{ input: Decimal.from(12.3), exponent: 0, output: Decimal.from(12.3) },
597+
{ input: Decimal.from(12.3), exponent: -1, output: Decimal.from(1.23) },
598+
{ input: Decimal.from(12.3), exponent: -2, output: Decimal.from(0.123) },
599+
{ input: Decimal.from(12.3), exponent: -3, output: Decimal.from(0.0123) },
600+
];
601+
for (const { input, exponent, output } of vectors) {
602+
assert(input.e(exponent).eq(output));
603+
assert(input.neg().e(exponent).eq(output.neg()));
604+
}
605+
});

Decimal.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export default class Decimal {
155155
* @param exponent The integer exponent to raise 10 to.
156156
* @returns A new Decimal instance representing the result.
157157
*/
158-
e(exponent: bigint | number): Decimal {
158+
e(exponent: number): Decimal {
159159
return this.mul(new Decimal(10n).pow(exponent));
160160
}
161161

@@ -386,19 +386,16 @@ export default class Decimal {
386386
* @param value The integer exponent to raise to.
387387
* @returns A new Decimal instance representing the result of the exponentiation.
388388
*/
389-
pow(value: bigint | number): Decimal {
390-
if (typeof value === 'number') {
391-
assert(Number.isInteger(value), `Exponent must be an integer, got ${value}`);
392-
value = BigInt(value);
393-
}
394-
if (value === 0n) {
389+
pow(value: number): Decimal {
390+
assert(Number.isInteger(value), `Exponent must be an integer, got ${value}`);
391+
if (value === 0) {
395392
return Decimal.one;
396-
} else if (value === 1n) {
393+
} else if (value === 1) {
397394
return this;
398-
} else if (value < 0n) {
395+
} else if (value < 0) {
399396
return this.inv().pow(-value);
400397
} else {
401-
return new Decimal(this.mantissa ** value, this.exponent * 2);
398+
return new Decimal(this.mantissa ** BigInt(value), this.exponent * value);
402399
}
403400
}
404401

deno.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@quentinadam/decimal",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"exports": "./Decimal.ts",
55
"imports": {
66
"@quentinadam/assert": "jsr:@quentinadam/assert@^0.1.6"

0 commit comments

Comments
 (0)