Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow Number Literals That Lose Precision (no-loss-of-precision) #1552

Closed
feross opened this issue Oct 21, 2020 · 1 comment
Closed

Disallow Number Literals That Lose Precision (no-loss-of-precision) #1552

feross opened this issue Oct 21, 2020 · 1 comment

Comments

@feross
Copy link
Member

feross commented Oct 21, 2020

https://eslint.org/docs/rules/no-loss-of-precision

This rule would disallow the use of number literals that immediately lose precision at runtime when converted to a JS Number due to 64-bit floating-point rounding.

Rule Details

In JS, Numbers are stored as double-precision floating-point numbers according to the IEEE 754 standard. 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.

Examples of incorrect code for this rule:

/*eslint no-loss-of-precision: "error"*/

const x = 9007199254740993
const x = 5123000000000000000000000000001
const x = 1230000000000000000000000.0
const x = .1230000000000000000000000
const x = 0X20000000000001
const x = 0X2_000000000_0001;

Examples of correct code for this rule:

/*eslint no-loss-of-precision: "error"*/

const x = 12345
const x = 123.456
const x = 123e34
const x = 12300000000000000000000000
const x = 0x1FFFFFFFFFFFFF
const x = 9007199254740991
const x = 9007_1992547409_91
@feross feross added this to the standard 15 milestone Oct 21, 2020
@feross
Copy link
Member Author

feross commented Oct 21, 2020

No ecosystem impact. Shipping in standard v15.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

1 participant