You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,48 +6,22 @@ The Decimal proposal uses a subset of the IEEE 754-2019 Decimal128 format. This
6
6
7
7
## IEEE 754-2019 Decimal128
8
8
9
-
Decimal128 is part of the IEEE 754 standard for floating-point arithmetic, added in the 2008 revision. It provides:
10
-
11
-
-**128-bit representation**: Each decimal value is stored in exactly 128 bits
12
-
-**Base-10 arithmetic**: Unlike binary floating-point, calculations are performed in base 10
13
-
-**34 significant digits**: Up to 34 decimal digits of precision
14
-
-**Exponent range**: -6143 to +6144 (base 10)
15
-
16
-
### Why Decimal128?
9
+
Decimal128 is part of the IEEE 754 standard for floating-point arithmetic, added in the 2008 revision. (In fact, JS's Number type is also based on IEEE 754: binary64) It provides a 128-bit representation(each decimal value is stored in exactly 128 bits) for base-10 arithmetic with up to 34 significant digits and an exponent range of -6143 to +6144.
17
10
18
11
The choice of Decimal128 balances several considerations:
19
12
20
13
1.**Sufficient precision**: 34 digits covers virtually all practical use cases, including:
21
-
- Financial calculations (even national debts)
14
+
- Financial calculations (even very large values that don't occur in everyday scenarios)
22
15
- Scientific measurements
23
-
- Cryptocurrency (Bitcoin has 8 decimal places)
24
-
25
16
2.**Fixed size**: Unlike arbitrary-precision decimals, Decimal128 has predictable memory usage and performance characteristics
26
-
27
-
3.**Industry standard**: Implemented in hardware on some platforms and widely supported in other languages
28
-
17
+
3.**Industry standard**: Widely supported in other languages
29
18
4.**Reasonable range**: Can represent values from ±10^-6143 to ±10^6144
30
19
31
-
## Representation Details
32
-
33
-
### Encoding Format
34
-
35
-
Decimal128 uses a specialized encoding that efficiently packs decimal digits into 128 bits:
This decision was made based on implementer feedback and concerns about:
189
-
- Performance implications
190
-
- Confusion with implicit conversions
191
-
- Consistency with BigInt precedent
160
+
This decision was made based on implementer feedback and concerns about performance implication, confusion with implicit conversions (given that Decimal is not proposed as a new primitive type).
192
161
193
162
### No Literal Syntax
194
163
@@ -204,60 +173,8 @@ This simplifies the implementation and avoids syntax complexity.
204
173
205
174
### Canonicalization vs Cohort Preservation
206
175
207
-
IEEE 754 supports the concept of "cohorts" - different representations of the same value (e.g., 1.20 vs 1.2). The Decimal proposal canonicalizes values, not preserving cohorts:
176
+
IEEE 754 supports the concept of "cohorts", which are different representations of the same value (e.g., 1.20 vs 1.2). The Decimal proposal canonicalizes values, not preserving cohorts:
208
177
209
178
```javascript
210
179
newDecimal("1.20").toString(); // => "1.2" (trailing zero lost)
211
180
```
212
-
213
-
## Implementation Considerations
214
-
215
-
### Memory Layout
216
-
217
-
Each Decimal value occupies 128 bits (16 bytes). Implementations may choose to:
218
-
- Store inline for small objects
219
-
- Use tagged pointers
220
-
- Allocate on heap for compatibility
221
-
222
-
### Performance
223
-
224
-
Expected performance characteristics:
225
-
-**Addition/Subtraction**: Similar to integer operations with normalization
226
-
-**Multiplication**: More complex than binary, but hardware acceleration possible
227
-
-**Division**: Most expensive operation, may require iterative algorithms
228
-
-**Comparison**: Fast, often just integer comparison of encoded form
229
-
230
-
### Platform Support
231
-
232
-
Some platforms provide hardware support for Decimal128:
233
-
- IBM POWER (Power6 and later)
234
-
- IBM System z
235
-
- Some Intel processors (via libraries)
236
-
237
-
On platforms without hardware support, software implementations are used.
238
-
239
-
## Future Considerations
240
-
241
-
### Decimal256
242
-
243
-
For applications requiring more than 34 digits, a future proposal might add Decimal256 (70 digits). The current API is designed to be extensible.
244
-
245
-
### Arbitrary Precision
246
-
247
-
Some use cases might benefit from arbitrary-precision decimals (BigDecimal). This could be a separate type that interoperates with Decimal128.
248
-
249
-
### Operator Overloading
250
-
251
-
If JavaScript adds general operator overloading, Decimal could be updated to support arithmetic operators while maintaining backward compatibility with the method-based API.
252
-
253
-
## Summary
254
-
255
-
The Decimal data model provides:
256
-
- Exact decimal arithmetic for 34 significant digits
257
-
- IEEE 754-2019 standard compliance
258
-
- Predictable performance and memory usage
259
-
- Special value handling (NaN, Infinity)
260
-
- Multiple rounding modes
261
-
- Clear semantics without implicit conversions
262
-
263
-
This design balances the needs of financial calculations, data exchange, and general decimal arithmetic while maintaining implementation feasibility across JavaScript engines.
0 commit comments