noApproximativeNumericConstant
Summary
Section titled “Summary”- Rule available since:
v1.3.0
- Diagnostic Category:
lint/suspicious/noApproximativeNumericConstant
- This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
approx_constant
- Same as
Description
Section titled “Description”Use standard constants instead of approximated literals.
Usually, the definition in the standard library is more precise than what people come up with or the used constant exceeds the maximum precision of the number type.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let x = 3.141;
code-block.js:1:9 lint/suspicious/noApproximativeNumericConstant FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer constants from the standard library.
> 1 │ let x = 3.141;
│ ^^^^^
2 │
ℹ Unsafe fix: Use Math.PI instead.
1 │ - let·x·=·3.141;
1 │ + let·x·=·Math.PI;
2 2 │
let x = 2.302;
code-block.js:1:9 lint/suspicious/noApproximativeNumericConstant FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer constants from the standard library.
> 1 │ let x = 2.302;
│ ^^^^^
2 │
ℹ Unsafe fix: Use Math.LN10 instead.
1 │ - let·x·=·2.302;
1 │ + let·x·=·Math.LN10;
2 2 │
let x = Math.PI;let y = 3.14;
let x = Math.LN10;
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "noApproximativeNumericConstant": "error" } } }}