Skip to content

Commit

Permalink
Merge pull request #1119 from cosmos/zero-one
Browse files Browse the repository at this point in the history
Add Decimal.zero and Decimal.one
  • Loading branch information
webmaster128 authored Apr 12, 2022
2 parents f31045a + e379ccd commit 1aa797a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to

## [Unreleased]

### Added

- @cosmjs/math: Add `Decimal.zero` and `Decimal.one` ([#1110]).

[#1110]: https://github.com/cosmos/cosmjs/issues/1110

## [0.28.3] - 2022-04-11

### Added
Expand Down
44 changes: 44 additions & 0 deletions packages/math/src/decimal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,50 @@ describe("Decimal", () => {
});
});

describe("zero", () => {
it("works", () => {
expect(Decimal.zero(0).toString()).toEqual("0");
expect(Decimal.zero(1).toString()).toEqual("0");
expect(Decimal.zero(2).toString()).toEqual("0");
expect(Decimal.zero(30).toString()).toEqual("0");

expect(Decimal.zero(0).fractionalDigits).toEqual(0);
expect(Decimal.zero(1).fractionalDigits).toEqual(1);
expect(Decimal.zero(2).fractionalDigits).toEqual(2);
expect(Decimal.zero(30).fractionalDigits).toEqual(30);

expect(Decimal.zero(0).atomics).toEqual("0");
expect(Decimal.zero(1).atomics).toEqual("0");
expect(Decimal.zero(2).atomics).toEqual("0");
expect(Decimal.zero(30).atomics).toEqual("0");

expect(() => Decimal.zero(NaN)).toThrowError(/Fractional digits is not an integer/i);
expect(() => Decimal.zero(1.2)).toThrowError(/Fractional digits is not an integer/i);
});
});

describe("one", () => {
it("works", () => {
expect(Decimal.one(0).toString()).toEqual("1");
expect(Decimal.one(1).toString()).toEqual("1");
expect(Decimal.one(2).toString()).toEqual("1");
expect(Decimal.one(30).toString()).toEqual("1");

expect(Decimal.one(0).fractionalDigits).toEqual(0);
expect(Decimal.one(1).fractionalDigits).toEqual(1);
expect(Decimal.one(2).fractionalDigits).toEqual(2);
expect(Decimal.one(30).fractionalDigits).toEqual(30);

expect(Decimal.one(0).atomics).toEqual("1");
expect(Decimal.one(1).atomics).toEqual("10");
expect(Decimal.one(2).atomics).toEqual("100");
expect(Decimal.one(30).atomics).toEqual("1000000000000000000000000000000");

expect(() => Decimal.one(NaN)).toThrowError(/Fractional digits is not an integer/i);
expect(() => Decimal.one(1.2)).toThrowError(/Fractional digits is not an integer/i);
});
});

describe("toString", () => {
it("displays no decimal point for full numbers", () => {
expect(Decimal.fromUserInput("44", 0).toString()).toEqual("44");
Expand Down
22 changes: 22 additions & 0 deletions packages/math/src/decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ export class Decimal {
return new Decimal(atomics, fractionalDigits);
}

/**
* Creates a Decimal with value 0.0 and the given number of fractial digits.
*
* Fractional digits are not relevant for the value but needed to be able
* to perform arithmetic operations with other decimals.
*/
public static zero(fractionalDigits: number): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);
return new Decimal("0", fractionalDigits);
}

/**
* Creates a Decimal with value 1.0 and the given number of fractial digits.
*
* Fractional digits are not relevant for the value but needed to be able
* to perform arithmetic operations with other decimals.
*/
public static one(fractionalDigits: number): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);
return new Decimal("1" + "0".repeat(fractionalDigits), fractionalDigits);
}

private static verifyFractionalDigits(fractionalDigits: number): void {
if (!Number.isInteger(fractionalDigits)) throw new Error("Fractional digits is not an integer");
if (fractionalDigits < 0) throw new Error("Fractional digits must not be negative");
Expand Down

0 comments on commit 1aa797a

Please sign in to comment.