Create rule S6534: Numbers should not lose precision (#1653)
This commit is contained in:
parent
2d8892defb
commit
f9ddabacec
20
rules/S6534/javascript/metadata.json
Normal file
20
rules/S6534/javascript/metadata.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"title": "Numbers should not lose precision",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"numbers",
|
||||
"precision",
|
||||
"floating-point"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-6534",
|
||||
"sqKey": "S6534",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "infeasible"
|
||||
}
|
57
rules/S6534/javascript/rule.adoc
Normal file
57
rules/S6534/javascript/rule.adoc
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
|
||||
// If you want to factorize the description uncomment the following line and create the file.
|
||||
//include::../description.adoc[]
|
||||
|
||||
== Why is this an issue?
|
||||
|
||||
Numbers in JavaScript are stored as 64-bit floating-point. However, numbers that can't be represented in such a format get rounded before assigning them to a variable.
|
||||
|
||||
=== What is the potential impact?
|
||||
|
||||
When you are saving a number that is too big or contains a fraction that is not expressible in the 64-bit floating-point format, it gets rounded which will lead to unexpected behaviours when it will be used.
|
||||
|
||||
== How to fix it
|
||||
|
||||
When you need to store a large number, use `BigInt()`. Pay attention that operations between `BigInt` and `Number` types raise an error, so you will have to adapt your code accordingly.
|
||||
|
||||
For numbers with digits after the decimal, it is recommended to use a library to ensure that calculation errors are not introduced by rounding.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,javascript]
|
||||
----
|
||||
const foo = 2312123211345545367
|
||||
const bar = 0.123456789123456789
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,javascript]
|
||||
----
|
||||
const foo = BigInt(2312123211345545367)
|
||||
// use a library like decimal.js for storing numbers containing decimal digits
|
||||
----
|
||||
|
||||
=== How does this work?
|
||||
|
||||
JavaScript numbers are stored in a double-precision 64-bit binary format IEEE 754. There are limits to what can be stored in this format both in huge numbers as in numbers with decimal digits.
|
||||
When attempting to store a number that can not be represented in this format into a variable, the number gets rounded before storage.
|
||||
|
||||
//=== Pitfalls
|
||||
|
||||
//=== Going the extra mile
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number[MDN Number]
|
||||
* https://en.wikipedia.org/wiki/Double-precision_floating-point_format[Wikipedia Double-precision floating-point format]
|
||||
* https://en.wikipedia.org/wiki/IEEE_754[Wikipedia IEEE 754 Standard]
|
||||
|
||||
//=== Articles & blog posts
|
||||
//=== Conference presentations
|
||||
//=== Standards
|
2
rules/S6534/metadata.json
Normal file
2
rules/S6534/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user