Skip to content

Commit 9c3e566

Browse files
committed
Corrected error when dividing 0 by any number and when specifying a number of significant digits.
Bump to version 0.1.7.
1 parent 3b255a9 commit 9c3e566

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Decimal.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,19 @@ Deno.test('div', () => {
294294
{ a: Decimal.from(1000), b: Decimal.from(0.2), success: true, output: Decimal.from(5000) },
295295
{ a: Decimal.from(1), b: Decimal.from(8), success: true, output: Decimal.from(0.125) },
296296
{ a: Decimal.from(141), b: Decimal.from(376), success: true, output: Decimal.from(0.375) },
297+
{ a: Decimal.from(0), b: Decimal.from(2), success: true, output: Decimal.from(0) },
298+
{ a: Decimal.from(0), b: Decimal.from(3), success: true, output: Decimal.from(0) },
299+
{ a: Decimal.from(0), b: Decimal.from(3), significantDigits: 2, success: true, output: Decimal.from(0) },
300+
{ a: Decimal.from(1), b: Decimal.from(4), significantDigits: 1, success: true, output: Decimal.from(0.3) },
301+
{ a: Decimal.from(1), b: Decimal.from(4), significantDigits: 2, success: true, output: Decimal.from(0.25) },
297302
{ a: Decimal.from(1), b: Decimal.from(3), success: false },
298303
{ a: Decimal.from(1000), b: Decimal.from(30), success: false },
299304
{ a: Decimal.from(1), b: Decimal.from(3), significantDigits: 1, success: true, output: Decimal.from(0.3) },
300305
{ a: Decimal.from(1), b: Decimal.from(3), significantDigits: 2, success: true, output: Decimal.from(0.33) },
306+
{ a: Decimal.from(100), b: Decimal.from(3), significantDigits: 2, success: true, output: Decimal.from(33) },
301307
{ a: Decimal.from(1), b: Decimal.from(6), significantDigits: 1, success: true, output: Decimal.from(0.2) },
302308
{ a: Decimal.from(1), b: Decimal.from(6), significantDigits: 2, success: true, output: Decimal.from(0.17) },
309+
{ a: Decimal.from(100), b: Decimal.from(6), significantDigits: 2, success: true, output: Decimal.from(17) },
303310
];
304311
for (const vector of vectors) {
305312
const { a, b, significantDigits } = vector;

Decimal.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ export default class Decimal {
112112
*/
113113
div(value: DecimalType, significantDigits?: number): Decimal {
114114
value = Decimal.from(value);
115-
assert(value.neq0(), `Division by zero`);
115+
assert(value.neq0(), 'Division by zero');
116+
if (this.eq0()) {
117+
return Decimal.zero;
118+
}
116119
if (this.lt0()) {
117120
return this.neg().div(value, significantDigits).neg();
118121
}

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.6",
3+
"version": "0.1.7",
44
"license": "MIT",
55
"exports": "./Decimal.ts",
66
"imports": {

0 commit comments

Comments
 (0)