0

I'm using x + (distance * Math.cos(orientation * Math.PI)) and y + (distance * Math.sin(orientation * Math.PI)) to calculate xx and yy respectively.

50 + (20 * Math.sin(1 * Math.PI)) and 50 + (20 * Math.cos(0.5 * Math.PI)) equal 50

but

50 + (20 * Math.sin(2 * Math.PI)) and 50 + (20 * Math.cos(1.5 * Math.PI)) equal 49.99999999999999?

I expected 50 here too. Is it a javascript quirk I need to use Math.ceil() with (calculating coords on a canvas)?

isherwood
  • 58,414
  • 16
  • 114
  • 157
ron_g
  • 1,474
  • 2
  • 21
  • 39
  • 2
    This is quirk will all the programming languages in the world. https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – tsamridh86 Jan 25 '22 at 14:32
  • 2
    javascript does not differentiate between ints and doubles, everything is just a `number` – skara9 Jan 25 '22 at 14:32
  • 3
    Canonical duplicate: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – esqew Jan 25 '22 at 14:32

0 Answers0