When I multiply 3 * 1.1
in JavaScript instead of returning 3.3
, it results in 3.30000000000000003
, Why is this?
Asked
Active
Viewed 423 times
0
-
2[relevante](http://www.smbc-comics.com/index.php?db=comics&id=2999#comic) – nicosantangelo Nov 10 '14 at 12:55
-
4Welcome to Stack Overflow! Currently, we only support English, so would you be so kind to translate your question? – This company is turning evil. Nov 10 '14 at 12:55
-
1This is because js stores numbers as floating point values and floating points arithmetics aren't always 100 % accurate. 0.2 + 0.1 for example does not give 0.3 in javascript but 0.30000000000000004 – Markai Nov 10 '14 at 12:56
-
http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – Dimag Kharab Nov 10 '14 at 12:57
-
http://floating-point-gui.de/ – Dimag Kharab Nov 10 '14 at 12:58
-
Do you have any more constructive question than "Why is this?" – Anders Marzi Tornblad Nov 10 '14 at 12:58
1 Answers
2
That's because numbers in computers are represented as floating point numbers, which have limited precision. Some operations will cause tiny errors and there's nothing you can do about that.
It's also a reason to never compare numbers using ==
.

Filburt
- 17,626
- 12
- 64
- 115

This company is turning evil.
- 5,021
- 5
- 34
- 58
-
-
So if we should not compare it by `==` , then what is the recommended way? – Ayush Kumar Jun 14 '21 at 05:46
-
@AyushKumar compare them to some tolerance: `a == b` becomes `Math.abs(a - b) < TOLERANCE`. What that tolerance is depends on context. Note that JS has the concept of "safe integers", which IIRC are up to [2^53](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER), so if you're only doing additions, subtractions, multiplications, or divisions of even integers, by integers, that result in values in that range, you can be sure they're the same and you can use `==`. – This company is turning evil. Jun 14 '21 at 14:16