0

The following line:

console.log(ship.x + " + " + (4/3) + " * " + ship.r + " * " + Math.cos(ship.a));

returns:

50 + 1.3333333333333333 * 15 * 6.123233995736766e-17

however when I actually do the math with JavaScript via:

console.log(ship.x + 4 / 3 * ship.r * Math.cos(ship.a));

it returns the integer value of '50'? How is this possible? The first value in the line is 50 so you would think it would logically be a bigger number once it is ran. I've tried PEMDAS and many other variations and never end up with the value of 50. What exactly is JavaScript doing with the above code? Here's a link to the entire code page: https://codepen.io/hoyos/pen/vYJqaRw?editors=0010

Ghoyos
  • 604
  • 2
  • 7
  • 18
  • 2
    Floating point math can't represent that much precision. Adding a number that small to any integer past 15 will have no effect. – General Grievance Nov 25 '21 at 03:16
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Peter O. Nov 25 '21 at 08:57

1 Answers1

0

Calculuswhiz's comment is correct. Floating-point math cannot handle the size of your number (6e-17), so you end up with 50 + 1.33333 * 15 * 0, which is 50 + 0.

Check out the approved answer to this question for more details: Is floating point math broken?

user1280483
  • 470
  • 4
  • 11