Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit fbcf9e6

Browse files
authored
Strengthen README to make it clear that this is a prototype for proposal-decimal (#77)
* Make clear that this library is intended to match the spec * Use TypeScript for examples
1 parent b7969d8 commit fbcf9e6

10 files changed

+36
-38
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ coverage
33
dist/
44
src/*.d.ts
55
src/*.mjs
6-
src/*.d.mts
6+
src/*.d.mts
7+
/examples/*.mjs
8+
/examples/*.d.ts
9+
/examples/*.d.mts
10+
/examples/*.js

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# decimal128.js—A userland approximation to IEEE 754 Decimal128 in JavaScript
22

3+
This library is a prototype for the [decimal proposal](https://github.com/tc39/proposal-decimal). Apart from the intention of the decimal proposal for `valueOf` to unconditionally throw, there should be no observable difference between what this library does and what the proposal is [supposed to do](http://tc39.es/proposal-decimal/). If you find a mismatch, please file [an issue](https://github.com/jessealama/decimal128/issues) in this repo.
4+
35
## Operations
46

57
- addition (`add`)
@@ -22,7 +24,7 @@ This package also supports minus zero, positive and negative infinity, and NaN.
2224

2325
### Differences with the official Decimal128
2426

25-
This package is not literally an implementation of Decimal128. This package is working with a subset of Decimal128 that makes sense for the use cases we have in mind (mainly, though not exclusively, finance). Only a handful of arithmetic operations are implemented. We do not offer, for instance, the various trigonometric functions.
27+
This package is not literally an implementation of IEEE 754 Decimal128. This package defines a subset of Decimal128 that makes sense for the use cases we have in mind (mainly, though not exclusively, finance). Only a handful of arithmetic operations are implemented. We do not offer, for instance, the various trigonometric functions. Moreover, this package supports the concep of quiet NaNs only. Signalling NaNs are not supported here.
2628

2729
#### Lack of support for specifying context
2830

examples/bill.js examples/bill.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import { Decimal128 } from "../src/decimal128.mjs";
33
const zero = new Decimal128("0");
44
const one = new Decimal128("1");
55

6-
function calculateBill(items, tax) {
7-
let taxRate = new Decimal128(tax).add(one);
6+
interface Item {
7+
price: string;
8+
count: string;
9+
}
10+
11+
function calculateBill(items: Item[], tax: string): Decimal128 {
812
let total = items.reduce((total, { price, count }) => {
913
return total.add(new Decimal128(price).multiply(new Decimal128(count)));
1014
}, zero);
11-
return total.multiply(taxRate);
15+
return total.multiply(new Decimal128(tax).add(one));
1216
}
1317

1418
const items = [
1519
{ price: "1.25", count: "5" },
1620
{ price: "5.00", count: "1" },
1721
];
1822
const tax = "0.0735";
19-
console.log(calculateBill(items, tax).toString());
23+
console.log(calculateBill(items, tax).toString({ numDecimalDigits: 2 }));

examples/floor.js

-3
This file was deleted.

examples/floor.mts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Decimal128 } from "../src/decimal128.mjs";
2+
3+
function floor(d: Decimal128): Decimal128 {
4+
return d.round(0, "floor");
5+
}
6+
7+
export { floor };

examples/mortgage.js examples/mortgage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { Decimal128 } from "../src/decimal128.mjs";
2-
import { pow } from "./pow.js";
2+
import { pow } from "./pow.mjs";
33

44
const one = new Decimal128("1");
55
const paymentsPerYear = new Decimal128("12");
66

7-
function calculateMonthlyPayment(p, r, y) {
7+
function calculateMonthlyPayment(p: string, r: string, y: string): Decimal128 {
88
const principal = new Decimal128(p);
99
const annualInterestRate = new Decimal128(r);
1010
const years = new Decimal128(y);
1111
const monthlyInterestRate = annualInterestRate.divide(paymentsPerYear);
1212
const paymentCount = paymentsPerYear.multiply(years);
1313
const onePlusInterestRate = monthlyInterestRate.add(one);
14-
const ratePower = pow(onePlusInterestRate, paymentCount);
14+
const ratePower = pow(onePlusInterestRate, Number(paymentCount));
1515

1616
const x = principal.multiply(monthlyInterestRate);
1717

examples/normalize.js

-6
This file was deleted.

examples/normalize.mts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Decimal128 } from "../src/decimal128.mjs";
2+
3+
function normalize(d: Decimal128): string {
4+
// Decimal128 object
5+
return d.toString({ normalize: true });
6+
}
7+
8+
export { normalize };

examples/pow.js examples/pow.mts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { Decimal128 } from "../src/decimal128.mjs";
22

3-
const one = new Decimal128("1");
4-
5-
function pow(a, b) {
6-
let i = one;
3+
function pow(a: Decimal128, b: number): Decimal128 {
74
let result = a;
8-
while (-1 === i.cmp(b)) {
5+
for (let i = 0; i < b; i++) {
96
result = result.multiply(a);
10-
i = i.add(one);
117
}
128
return result;
139
}

examples/step.js

-14
This file was deleted.

0 commit comments

Comments
 (0)