]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-loss-of-precision | |
8f9d1d4d DC |
3 | rule_type: problem |
4 | --- | |
ebb53d86 | 5 | |
8f9d1d4d DC |
6 | |
7 | ||
8 | This rule would disallow the use of number literals that lose precision at runtime when converted to a JS `Number` due to 64-bit floating-point rounding. | |
ebb53d86 TL |
9 | |
10 | ## Rule Details | |
11 | ||
12 | In JS, `Number`s are stored as double-precision floating-point numbers according to the [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754). Because of this, numbers can only retain accuracy up to a certain amount of digits. If the programmer enters additional digits, those digits will be lost in the conversion to the `Number` type and will result in unexpected behavior. | |
13 | ||
14 | Examples of **incorrect** code for this rule: | |
15 | ||
8f9d1d4d DC |
16 | ::: incorrect |
17 | ||
ebb53d86 TL |
18 | ```js |
19 | /*eslint no-loss-of-precision: "error"*/ | |
20 | ||
21 | const x = 9007199254740993 | |
22 | const x = 5123000000000000000000000000001 | |
23 | const x = 1230000000000000000000000.0 | |
24 | const x = .1230000000000000000000000 | |
25 | const x = 0X20000000000001 | |
6f036462 | 26 | const x = 0X2_000000000_0001; |
ebb53d86 TL |
27 | ``` |
28 | ||
8f9d1d4d DC |
29 | ::: |
30 | ||
ebb53d86 TL |
31 | Examples of **correct** code for this rule: |
32 | ||
8f9d1d4d DC |
33 | ::: correct |
34 | ||
ebb53d86 TL |
35 | ```js |
36 | /*eslint no-loss-of-precision: "error"*/ | |
37 | ||
38 | const x = 12345 | |
39 | const x = 123.456 | |
40 | const x = 123e34 | |
41 | const x = 12300000000000000000000000 | |
42 | const x = 0x1FFFFFFFFFFFFF | |
43 | const x = 9007199254740991 | |
6f036462 | 44 | const x = 9007_1992547409_91 |
ebb53d86 | 45 | ``` |
8f9d1d4d DC |
46 | |
47 | ::: |