Skip to content

Commit

Permalink
Support pow for integers (#45)
Browse files Browse the repository at this point in the history
* Support pow for integers

* Add a changelog
  • Loading branch information
jessealama authored Oct 25, 2023
1 parent 7e2870b commit 48658e2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 6 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

## [5.2.0] - 2023-10-25

### Added

- `pow` for raising a decimal to a power (#43)
28 changes: 26 additions & 2 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,10 @@ export class Decimal128 {

if (!x.isFinite) {
if (this.isNegative === x.isNegative) {
return new Decimal128("Infinity");
return new Decimal128("0");
}

return new Decimal128("-Infinity");
return new Decimal128("-0");
}

return new Decimal128(
Expand Down Expand Up @@ -879,4 +879,28 @@ export class Decimal128 {
let q = this.divide(d).round();
return this.subtract(d.multiply(q)).abs();
}

reciprocal(): Decimal128 {
return new Decimal128("1").divide(this);
}

pow(n: Decimal128): Decimal128 {
if (!n.isInteger()) {
throw new TypeError("Exponent must be an integer");
}

if (n.isNegative) {
return this.pow(n.negate()).reciprocal();
}

let one = new Decimal128("1");
let i = new Decimal128("0");
let result: Decimal128 = one;
while (i.cmp(n) === -1) {
result = result.multiply(this);
i = i.add(one);
}

return result;
}
}
8 changes: 4 additions & 4 deletions tests/divide.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ describe("division", () => {
test("positive number divided bv positive infinity", () => {
expect(
new Decimal128("123.5").divide(posInf).toString()
).toStrictEqual("Infinity");
).toStrictEqual("0");
});
test("positive number divided bv negative infinity", () => {
expect(
new Decimal128("123.5").divide(negInf).toString()
).toStrictEqual("-Infinity");
).toStrictEqual("-0");
});
test("negative number divided by positive infinity", () => {
expect(
new Decimal128("-2").divide(posInf).toString()
).toStrictEqual("-Infinity");
).toStrictEqual("-0");
});
test("negative number divided by negative infinity", () => {
expect(
new Decimal128("-2").divide(negInf).toString()
).toStrictEqual("Infinity");
).toStrictEqual("0");
});
});
});
44 changes: 44 additions & 0 deletions tests/pow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Decimal128 } from "../src/decimal128.mjs";

describe("pow", () => {
test("throws if not an integer", () => {
expect(() => {
new Decimal128("1.2").pow(new Decimal128("2.3"));
}).toThrow();
});
test("pow zero is one", () => {
expect(
new Decimal128("42.456").pow(new Decimal128("0")).toString()
).toStrictEqual("1");
});
test("negative power", () => {
expect(
new Decimal128("5.6").pow(new Decimal128("-2")).toString()
).toStrictEqual("0.031887755102040816326530612244898");
});
test("positive power", () => {
expect(
new Decimal128("5.6").pow(new Decimal128("8")).toString()
).toStrictEqual("967173.11574016");
});
describe("infinity", () => {
test("positive infinity", () => {
expect(
new Decimal128("Infinity").pow(new Decimal128("2")).toString()
).toStrictEqual("Infinity");
});
test("negative infinity squared", () => {
expect(
new Decimal128("-Infinity").pow(new Decimal128("2")).toString()
).toStrictEqual("Infinity");
});
test("negative infinity cubed", () => {
expect(
new Decimal128("-Infinity").pow(new Decimal128("3")).toString()
).toStrictEqual("-Infinity");
});
});
describe("NaN", () => {
expect(() => new Decimal128("42").pow("NaN")).toThrow();
});
});
47 changes: 47 additions & 0 deletions tests/reciprocal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Decimal128 } from "../src/decimal128.mjs";

describe("reciprocal", () => {
test("divide by zero", () => {
expect(new Decimal128("0").reciprocal().toString()).toStrictEqual(
"NaN"
);
});
test("one divided by one", () => {
expect(new Decimal128("1").reciprocal().toString()).toStrictEqual("1");
});
test("one divided by two", () => {
expect(new Decimal128("2").reciprocal().toString()).toStrictEqual(
"0.5"
);
});
test("reciprocal of point two", () => {
expect(new Decimal128("0.2").reciprocal().toString()).toStrictEqual(
"5"
);
});
test("receiprocal of three", () => {
expect(new Decimal128("3").reciprocal().toString()).toStrictEqual(
"0.3333333333333333333333333333333333"
);
});
test("reciprocal of point three", () => {
expect(new Decimal128("0.3").reciprocal().toString()).toStrictEqual(
"3.333333333333333333333333333333333"
);
});
test("NaN", () => {
expect(new Decimal128("NaN").reciprocal().toString()).toStrictEqual(
"NaN"
);
});
test("positive infinity", () => {
expect(
new Decimal128("Infinity").reciprocal().toString()
).toStrictEqual("0");
});
test("negative infinity", () => {
expect(
new Decimal128("-Infinity").reciprocal().toString()
).toStrictEqual("-0");
});
});

0 comments on commit 48658e2

Please sign in to comment.