Merge pull request #421 from CosmWasm/math-decimal-minus

Add Decimal.minus() to math package
This commit is contained in:
Simon Warta 2020-09-15 10:14:06 +02:00 committed by GitHub
commit 1809d76de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -211,6 +211,54 @@ describe("Decimal", () => {
});
});
describe("minus", () => {
it("returns correct values", () => {
const zero = Decimal.fromUserInput("0", 5);
expect(Decimal.fromUserInput("0", 5).minus(zero).toString()).toEqual("0");
expect(Decimal.fromUserInput("1", 5).minus(zero).toString()).toEqual("1");
expect(Decimal.fromUserInput("2", 5).minus(zero).toString()).toEqual("2");
expect(Decimal.fromUserInput("2.8", 5).minus(zero).toString()).toEqual("2.8");
expect(Decimal.fromUserInput("0.12345", 5).minus(zero).toString()).toEqual("0.12345");
const one = Decimal.fromUserInput("1", 5);
expect(Decimal.fromUserInput("1", 5).minus(one).toString()).toEqual("0");
expect(Decimal.fromUserInput("2", 5).minus(one).toString()).toEqual("1");
expect(Decimal.fromUserInput("3", 5).minus(one).toString()).toEqual("2");
expect(Decimal.fromUserInput("3.8", 5).minus(one).toString()).toEqual("2.8");
expect(Decimal.fromUserInput("1.12345", 5).minus(one).toString()).toEqual("0.12345");
const oneDotFive = Decimal.fromUserInput("1.5", 5);
expect(Decimal.fromUserInput("1.5", 5).minus(oneDotFive).toString()).toEqual("0");
expect(Decimal.fromUserInput("2.5", 5).minus(oneDotFive).toString()).toEqual("1");
expect(Decimal.fromUserInput("3.5", 5).minus(oneDotFive).toString()).toEqual("2");
expect(Decimal.fromUserInput("4.3", 5).minus(oneDotFive).toString()).toEqual("2.8");
expect(Decimal.fromUserInput("1.62345", 5).minus(oneDotFive).toString()).toEqual("0.12345");
// original value remain unchanged
expect(zero.toString()).toEqual("0");
expect(one.toString()).toEqual("1");
expect(oneDotFive.toString()).toEqual("1.5");
});
it("throws for different fractional digits", () => {
const zero = Decimal.fromUserInput("0", 5);
expect(() => Decimal.fromUserInput("1", 1).minus(zero)).toThrowError(/do not match/i);
expect(() => Decimal.fromUserInput("1", 2).minus(zero)).toThrowError(/do not match/i);
expect(() => Decimal.fromUserInput("1", 3).minus(zero)).toThrowError(/do not match/i);
expect(() => Decimal.fromUserInput("1", 4).minus(zero)).toThrowError(/do not match/i);
expect(() => Decimal.fromUserInput("1", 6).minus(zero)).toThrowError(/do not match/i);
expect(() => Decimal.fromUserInput("1", 7).minus(zero)).toThrowError(/do not match/i);
});
it("throws for negative results", () => {
const one = Decimal.fromUserInput("1", 5);
expect(() => Decimal.fromUserInput("0", 5).minus(one)).toThrowError(/must not be negative/i);
expect(() => Decimal.fromUserInput("0.5", 5).minus(one)).toThrowError(/must not be negative/i);
expect(() => Decimal.fromUserInput("0.98765", 5).minus(one)).toThrowError(/must not be negative/i);
});
});
describe("equals", () => {
it("returns correct values", () => {
const zero = Decimal.fromUserInput("0", 5);

View File

@ -124,6 +124,19 @@ export class Decimal {
return new Decimal(sum.toString(), this.fractionalDigits);
}
/**
* a.minus(b) returns a-b.
*
* Both values need to have the same fractional digits.
* The resulting difference needs to be non-negative.
*/
public minus(b: Decimal): Decimal {
if (this.fractionalDigits !== b.fractionalDigits) throw new Error("Fractional digits do not match");
const difference = this.data.atomics.sub(new BN(b.atomics));
if (difference.ltn(0)) throw new Error("Difference must not be negative");
return new Decimal(difference.toString(), this.fractionalDigits);
}
public equals(b: Decimal): boolean {
return Decimal.compare(this, b) === 0;
}

View File

@ -24,6 +24,13 @@ export declare class Decimal {
* Both values need to have the same fractional digits.
*/
plus(b: Decimal): Decimal;
/**
* a.minus(b) returns a-b.
*
* Both values need to have the same fractional digits.
* The resulting difference needs to be non-negative.
*/
minus(b: Decimal): Decimal;
equals(b: Decimal): boolean;
isLessThan(b: Decimal): boolean;
isLessThanOrEqual(b: Decimal): boolean;