skip to Main Content

If I multiply the numbers 3.14, 3 and 3 in Javascript and print it like this

console.log(3.14*3*3);

I get: 28.259999999999998

If I say, do this:

console.log(3.14*(3*3));

I get: 28.26

Why is that?

2

Answers


  1. The reason for the different outcomes is how computers handle decimal numbers- particularly when executing a series of many computations. These numbers are stored in computer memory as what is known as floating-point arithmetic that therefore means sometimes they have tiny approximations because of rounding off errors.

    Why does the first calculation (3.14 * 3 * 3) yield a slightly inaccurate answer? The little inaccuracies which happen from times three multiplied by 3.14 can really pile up as shown here; when the initial imprecise value is multiplied thrice again, we get a final sum of 28.259999999999998.

    Rounding error occurs in less quantity due to the decrease in number of consecutive operations hence why there are brackets in the second equation i.e. 3.14 * (3 * 3). This in turn gives you

    Login or Signup to reply.
  2. This phenomenon same like 0.1+0.2 === 0.3 //false

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search