Skip to content

Commit 46f6667

Browse files
authored
Define Decimal.Amount (#188)
* Define `Decimal128.Amount` * Say "Decimal" rather than "Decimal128" and name section IDs consistently with `sec-decimal-*` * Use prettier * Add discussion of `Decimal.Amount` to the README
1 parent da8a57b commit 46f6667

File tree

9 files changed

+933
-698
lines changed

9 files changed

+933
-698
lines changed

.github/workflows/build.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/checkout@v2
11-
- uses: actions/setup-node@v1
12-
with:
13-
node-version: '20.x'
14-
- run: npm install
15-
- run: npm run build
16-
- name: commit changes
17-
uses: elstudio/actions-js-build/commit@v4
18-
with:
19-
commitMessage: "fixup: [spec] `npm run build`"
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v1
12+
with:
13+
node-version: "20.x"
14+
- run: npm install
15+
- run: npm run build
16+
- name: commit changes
17+
uses: elstudio/actions-js-build/commit@v4
18+
with:
19+
commitMessage: "fixup: [spec] `npm run build`"

README.md

Lines changed: 127 additions & 100 deletions
Large diffs are not rendered by default.

RELATED.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ The need for accurately representing decimal quantities is not new or unique to
55
### Related JS ecosystem libraries
66

77
JavaScript programmers are using decimal data types today with various ecosystem libraries. The most popular three on npm are each by [MikeMcl](https://github.com/mikemcl):
8+
89
- [decimal.js](http://mikemcl.github.io/decimal.js/)
910
- [big.js](https://mikemcl.github.io/big.js/)
1011
- [bignumber.js](https://mikemcl.github.io/bignumber.js/)
1112

1213
These packages have some [interesting differences](https://github.com/MikeMcl/big.js/wiki), but there are also many similarities:
14+
1315
- APIs are based on JavaScript objects and method calls
1416
- Rounding modes and precision limits are settings in the constructor
1517
- Inherently rounding operations like sqrt, exponentiation, division are supported
@@ -23,46 +25,46 @@ We plan to investigate the experiences and feedback developers have had with the
2325
Due to the overwhelming evidence listed above that decimal is an important data type in programming, many different programming languages, databases and standards include decimal, rationals, or similar features. Below is a partial listing, with a summary of the semantics in each.
2426

2527
- Standards
26-
- **IEEE 754**-2008 and later: 32-bit, 64-bit and 128-bit decimal; see [explanation](https://en.wikipedia.org/wiki/Decimal_floating_point#IEEE_754-2008_encoding) (recommends against using 32-bit decimal)
27-
- (plus many of the below are standardized)
28+
- **IEEE 754**-2008 and later: 32-bit, 64-bit and 128-bit decimal; see [explanation](https://en.wikipedia.org/wiki/Decimal_floating_point#IEEE_754-2008_encoding) (recommends against using 32-bit decimal)
29+
- (plus many of the below are standardized)
2830
- Programming languages
29-
- Fixed-size decimals:
30-
- **[C](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1312.pdf)**: 32, 64 and 128-bit IEE 754 decimal types, with a global settings object. Still a proposal, but has a GCC implementation.
31-
- **[C++](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3871.html)**: Early proposal work in progress, to be based on IEEE 64 and 128-bit decimal. Still a proposal, but has a GCC implementation.
32-
- **[C#](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/decimal)**/**[.NET](https://msdn.microsoft.com/en-us/library/system.decimal(v=vs.110).aspx)**: Custom 128-bit decimal semantics with slightly different sizes for the mantissa vs exponent compared to IEEE.
33-
- **[Swift](https://developer.apple.com/documentation/foundation/decimal)**/**[Obj-C](https://developer.apple.com/documentation/foundation/nsdecimal?language=objc)**: Yet another custom semantics for fixed-bit-size floating point decimal.
34-
- Global settings for setting decimal precision
35-
- **[Python](https://docs.python.org/2/library/decimal.html)**: Decimal with global settings to set precision.
36-
- Rationals
37-
- **[Perl6](https://docs.perl6.org/type/Rat)**: Literals like `1.5` are Rat instances!
38-
- **[Common Lisp](http://www.lispworks.com/documentation/lw50/CLHS/Body/t_ratio.htm#ratio)**: Ratios live alongside floats; no decimal data type
39-
- **[Scheme](http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2)**: Analogous to Common Lisp, with different names for types (Racket is similar)
40-
- **[Ruby](https://ruby-doc.org/core-2.6.5/Rational.html)**: Rational class alongside BigDecimal.
41-
- Arbitrary-precision decimals (this proposal)
42-
- **[Ruby](https://ruby-doc.org/stdlib-2.4.0/libdoc/bigdecimal/rdoc/BigDecimal.html)**: Arbitrary-precision Decimal, alongside Rational.
43-
- **[PHP](http://php.net/manual/en/book.bc.php)**: A set of functions to bind to bc for mathematical calculations. An alternative community-driven [Decimal library](https://php-decimal.io/) is also available.
44-
- **[Java](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/math/BigDecimal.html)**: Arbitrary-precision decimal based on objects and methods. Requires rounding modes and precision parameters for operations like division
31+
- Fixed-size decimals:
32+
- **[C](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1312.pdf)**: 32, 64 and 128-bit IEE 754 decimal types, with a global settings object. Still a proposal, but has a GCC implementation.
33+
- **[C++](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3871.html)**: Early proposal work in progress, to be based on IEEE 64 and 128-bit decimal. Still a proposal, but has a GCC implementation.
34+
- **[C#](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/decimal)**/**[.NET](<https://msdn.microsoft.com/en-us/library/system.decimal(v=vs.110).aspx>)**: Custom 128-bit decimal semantics with slightly different sizes for the mantissa vs exponent compared to IEEE.
35+
- **[Swift](https://developer.apple.com/documentation/foundation/decimal)**/**[Obj-C](https://developer.apple.com/documentation/foundation/nsdecimal?language=objc)**: Yet another custom semantics for fixed-bit-size floating point decimal.
36+
- Global settings for setting decimal precision
37+
- **[Python](https://docs.python.org/2/library/decimal.html)**: Decimal with global settings to set precision.
38+
- Rationals
39+
- **[Perl6](https://docs.perl6.org/type/Rat)**: Literals like `1.5` are Rat instances!
40+
- **[Common Lisp](http://www.lispworks.com/documentation/lw50/CLHS/Body/t_ratio.htm#ratio)**: Ratios live alongside floats; no decimal data type
41+
- **[Scheme](http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2)**: Analogous to Common Lisp, with different names for types (Racket is similar)
42+
- **[Ruby](https://ruby-doc.org/core-2.6.5/Rational.html)**: Rational class alongside BigDecimal.
43+
- Arbitrary-precision decimals (this proposal)
44+
- **[Ruby](https://ruby-doc.org/stdlib-2.4.0/libdoc/bigdecimal/rdoc/BigDecimal.html)**: Arbitrary-precision Decimal, alongside Rational.
45+
- **[PHP](http://php.net/manual/en/book.bc.php)**: A set of functions to bind to bc for mathematical calculations. An alternative community-driven [Decimal library](https://php-decimal.io/) is also available.
46+
- **[Java](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/math/BigDecimal.html)**: Arbitrary-precision decimal based on objects and methods. Requires rounding modes and precision parameters for operations like division
4547
- Databases
46-
- Decimal with precision configurable in the schema
47-
- [Microsoft SQL Server](https://docs.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql)
48-
- [PostgreSQL](https://www.postgresql.org/docs/9.1/static/datatype-numeric.html)
49-
- [MySQL](https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html)
50-
- IEEE 754-2008 decimal
51-
- Bloomberg's [comdb2](https://bloomberg.github.io/comdb2/decimals.html)
52-
- [MongoDB](https://docs.mongodb.com/manual/core/shell-types/#shell-type-decimal)
48+
- Decimal with precision configurable in the schema
49+
- [Microsoft SQL Server](https://docs.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql)
50+
- [PostgreSQL](https://www.postgresql.org/docs/9.1/static/datatype-numeric.html)
51+
- [MySQL](https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html)
52+
- IEEE 754-2008 decimal
53+
- Bloomberg's [comdb2](https://bloomberg.github.io/comdb2/decimals.html)
54+
- [MongoDB](https://docs.mongodb.com/manual/core/shell-types/#shell-type-decimal)
5355
- Libraries
54-
- Intel C [inteldfp](https://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library): IEEE decimal
55-
- Bloomberg C++ [bdldfp](https://github.com/bloomberg/bde/blob/master/groups/bdl/bdldfp/bdldfp_decimal.h): IEEE decimal
56-
- IBM C [decnumber](http://speleotrove.com/decimal/decnumber.html): Configurable context with precision, rounding mode
57-
- Rust crates [[1]](https://crates.io/crates/decimal) [[2]](https://crates.io/crates/bigdecimal)
56+
- Intel C [inteldfp](https://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library): IEEE decimal
57+
- Bloomberg C++ [bdldfp](https://github.com/bloomberg/bde/blob/master/groups/bdl/bdldfp/bdldfp_decimal.h): IEEE decimal
58+
- IBM C [decnumber](http://speleotrove.com/decimal/decnumber.html): Configurable context with precision, rounding mode
59+
- Rust crates [[1]](https://crates.io/crates/decimal) [[2]](https://crates.io/crates/bigdecimal)
5860
- Hardware (all implementing IEEE decimal)
59-
- [POWER6](https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+Systems/page/POWER6+Decimal+Floating+Point+(DFP))
60-
- [RISC-V](https://en.wikichip.org/wiki/risc-v/standard_extensions) (planned)
61+
- [POWER6](<https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+Systems/page/POWER6+Decimal+Floating+Point+(DFP)>)
62+
- [RISC-V](https://en.wikichip.org/wiki/risc-v/standard_extensions) (planned)
6163

6264
### History of discussion of decimal in TC39
6365

6466
Decimal has been under discussion in TC39 for a very long time, with proposals and feedback from many people including Sam Ruby, Mike Cowlishaw, Brendan Eich, Waldemar Horwat, Maciej Stachowiak, Dave Herman and Mark Miller.
67+
6568
- A new `decimal` type was long planned for ES4, see [Proposed ECMAScript 4th Edition – Language Overview](https://www.ecma-international.org/activities/Languages/Language%20overview.pdf)
6669
- In the following ES3.1/ES5 effort, discussions about a decimal type continued on es-discuss, e.g., [[1]](https://mail.mozilla.org/pipermail/es-discuss/2008-August/007244.html) [[2]](https://mail.mozilla.org/pipermail/es-discuss/2008-September/007466.html)
6770
- Decimal was discussed at length in the development of ES6. It was eventually rolled into the broader typed objects/value types effort, which didn't make it into ES6, but is being incrementally developed now (see the below section about relationship to other TC39 proposals).
68-

decimal128-reference.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ result is 0.8, and they also should be equivalent. However, since the result for
2626
can't be exactly represented by binary floating-point numbers, the results diverge. For this example, the
2727
reason for such difference on results comes from the fact that multiple additions using binary floating-point
2828
numbers will carry more errors from rounding than a single multiplication. It's possible to see more examples
29-
of issues like that on this [Decimal FAQ section](http://speleotrove.com/decimal/decifaq1.html#inexact). Such
29+
of issues like that on this [Decimal FAQ section](http://speleotrove.com/decimal/decifaq1.html#inexact). Such
3030
issue isn't a problem with Decimal128, because we are able to represent all those decimal fractions exactly,
3131
including the intermediate results for arithmetic operations.
3232

@@ -57,6 +57,7 @@ console.log(calculateBill(items, tax));
5757
```
5858

5959
Here, you can see several elements of `Decimal128` at work:
60+
6061
- Create a `Decimal128` as a literal, e.g., `1.25m`, or convert one from another type, as `Decimal128(value)`.
6162
- Add and multiply `Decimal128`s with the `+` and `*` operators.
6263
- Round decimals with `Decimal128.round`, based on an options bag describing how to round.
@@ -305,7 +306,7 @@ console.log(isNotEqual); // prints false
305306

306307
#### Comparing Decimal128 with other primitive types
307308

308-
Since comparison operators uses mathematical value of operands, it is possible to compare Decimal128 other
309+
Since comparison operators uses mathematical value of operands, it is possible to compare Decimal128 other
309310
types like Numbers, BigInt or Strings.
310311

311312
```js
@@ -359,7 +360,7 @@ If a Decimal128 is an operand of a bitwise operator, it results in a `TypeError`
359360
360361
### `Decimal128.round(value [, options])`
361362
362-
This is the function to be used when there's need to round Decimal128 in some specific way. It rounds the
363+
This is the function to be used when there's need to round Decimal128 in some specific way. It rounds the
363364
Decimal128 passed as parameter, tanking in consideration `options`.
364365
365366
- `value`: A Decimal128 value. If the value is from another type, it throws `TypeError`.
@@ -371,12 +372,12 @@ Decimal128 passed as parameter, tanking in consideration `options`.
371372
option is described below.
372373
- `down`: round towards zero.
373374
- `half down`: round towards "nearest neighbor". If both neighbors are equidistant, it rounds down.
374-
- `half up`: round towards "nearest neighbor". If both neighbors are equidistant, it rounds up.
375+
- `half up`: round towards "nearest neighbor". If both neighbors are equidistant, it rounds up.
375376
- `half even`: round towards the "nearest neighbor". If both neighbors are equidistant, it rounds towards
376377
the even neighbor.
377378
- `up`: round away from zero.
378379
379-
```js
380+
```js
380381
let a = Decimal128.round(0.53m, {roundingMode: 'half up', maximumFractionDigits: 1});
381382
assert(a, 0.5m);
382383

@@ -391,7 +392,7 @@ assert(a, 0.3m);
391392

392393
a = Decimal128.round(0.31m, {roundingMode: 'up', maximumFractionDigits: 1});
393394
assert(a, 0.4m);
394-
```
395+
```
395396
396397
### Decimal128.add(lhs, rhs [, options])
397398

index.html

Lines changed: 273 additions & 214 deletions
Large diffs are not rendered by default.

intl.emu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<h1>Decimal128.prototype.toLocaleString ( [ _locales_ [ , _options_ ] ] )</h1>
1919

2020
<p>
21-
This definition supersedes the definition provided in es2024, <emu-xref href="#sec-decimal128.prototype.tolocalestring"></emu-xref>.
21+
This definition supersedes the definition provided in es2024, <emu-xref href="#sec-decimal.prototype.tolocalestring"></emu-xref>.
2222
</p>
2323

2424
<p>

package-lock.json

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"start": "npm run build-loose -- --watch",
77
"build": "npm run build-loose -- --strict",
8-
"build-loose": "ecmarkup --verbose --lint-spec --load-biblio @tc39/ecma262-biblio --load-biblio @tc39/ecma402-biblio spec.emu index.html"
8+
"build-loose": "ecmarkup --verbose --lint-spec --load-biblio @tc39/ecma262-biblio --load-biblio @tc39/ecma402-biblio spec.emu index.html",
9+
"format": "prettier . --write",
10+
"lint": "prettier . --check"
911
},
1012
"repository": {
1113
"type": "git",
@@ -26,6 +28,7 @@
2628
"devDependencies": {
2729
"@tc39/ecma262-biblio": "2.1",
2830
"@tc39/ecma402-biblio": "^2.1.1050",
29-
"ecmarkup": "github:jessealama/ecmarkup#permit-decimal-subscript-D"
31+
"ecmarkup": "github:jessealama/ecmarkup#permit-decimal-subscript-D",
32+
"prettier": "^3.5.3"
3033
}
3134
}

0 commit comments

Comments
 (0)