0

I just started learning Javascript. I was confused with the script and its HTML output below.

Script:

<h1>JavaScript Variables</h1>

<p id="demo"></p>

<script>
var x = 3.14;
document.getElementById("demo").innerHTML = x + 1;
</script>

</body>
</html>

Output: JavaScript Variables

4.140000000000001

I wonder why would I get 4.140000000000001 instead of 4.14? The output is also incorrect if I try x + 2, x + 3, x + 4. It would be correct if x is added to other numbers like 10. Thanks for your help.

Wilson Lau
  • 21
  • 2
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Sebastian Simon Jan 18 '17 at 03:55

2 Answers2

0

Computer hardware does not calculate real numbers the way we do. Neither way is "correct" because some numbers require infinite expansions and we don't have time to deal with infinitely many digits. For example when we calculate 1/3 we get 0.3333.... We are used to the inaccuracy of truncating this expansion and think of it as "correct". Of course it is not exactly correct.

Computer hardware does not use base 10 calculations the way we do. It uses a form of base 2 calculation. The infinite expansions are different in base 2. For example 1/10 (base 10) is 0.000110001100011.... (base 2).

Both systems have inaccuracies because we must truncate infinite decimal expansions. The inaccuracies are different. Computer hardware compensates for the more frequent infinite expansions in base 2 by keeping lots of fractional digits.

The takeaway is that you will always get situations in which your answers are not what you expect.

If your application cannot accept these inaccuracies (and some accounting applications cannot) then you need to emulate base 10 arithmetic in software. There are packages for this. I have no experience with them. To get you started here is one:

https://github.com/MikeMcl/bignumber.js/

  • _“You can imagine that base 2 numbers have more such infinite expansions because they have fewer digits to play with.”_ — isn’t this dependent on the number of factors of the base rather than the number of possible digits? – Sebastian Simon Jan 18 '17 at 18:30
  • I shouldn't have thrown that comment in as to do it right takes the discussion to a level that isn't necessary here. – J Adrian Zimmer Jan 18 '17 at 19:13
-1

It happens with number 3.14 only which is the value of PI (Predefined in the javascript MATH with 15 digits after decimal). Check this http://www.w3schools.com/jsref/jsref_pi.asp

You can set the limits of the digits after decimal by toFixed() function.

Shubhranshu
  • 511
  • 3
  • 12
  • This is neither complete nor accurate. `3.14` isn’t PI; it doesn’t only happen with PI or with `3.14`; you didn’t explain why it happens; there’s a dupe target (first comment under question) that links to the actual explanation, because this question is a duplicate which shouldn’t be answered, but closed. – Sebastian Simon Jan 18 '17 at 05:05