1

Possible Duplicate:
Is JavaScript’s Math broken?

So I was working on a js library to work with 3 dimensional vectors and I stumbled upon a very small precision problem. Ok firstly, see the code below:

var a = new vec3(1, 0, 0);
var b = new vec3(1, 1, 0).normalize();

There I'm creating 2 vectors at exactly 45° degrees from one another (I have omitted the vec3 constructor as I just do standard math stuff there)

Now you people know that the dot product of 2 3D vectors yields their magnitudes and the cosine of the angle multiplied. But when I do this:

alert(180 / Math.PI * Math.acos(a.dot(b)));

I get 45.00000000000001

I understand that this is a very, very small difference but this would prevent me from being able to check if the angle between 2 vectors is 45° in case I didn't know that beforehand, since Math.acos(a.dot(b)) == Math.cos(Math.PI / 180 * 45) would result in false

But here is a funny thing, if I change my vectors to this:

var y = new vec3(Math.sqrt(2), Math.sqrt(2), 0).normalize();
var x = new vec3(Math.sqrt(2), 0, 0).normalize();

And then do the same calculation as above, it gives me exactly 45.

Now I'm aware that I can round that 45.00000000000001, but since I'm totally noob to subjects regarding how mathematical functions are implemented on lower levels I got really curious about what factors could cause two calculations that should give exactly the same value to result in slight different things.

Community
  • 1
  • 1
Delta
  • 4,308
  • 2
  • 29
  • 37
  • 1
    You should always compare floats by e.g. `if(Math.abs(x - y) < 0.000001)` to avoid this problem, see http://stackoverflow.com/questions/588004/is-javascripts-math-broken – Uooo Oct 18 '12 at 05:25
  • 1
    I'm voting to close this question [as duplicate]. Concerns, surprises, questions and work around tricks related to the accuracy of floating point arithmetic are a perennial crop on this site and other forums. The community will be better served if we have a few, well documented postings about this important topic, rather than many of these _ad hoc_ "WTF" questions and "Duh, it is IEEE 754 at work" responses. – mjv Oct 18 '12 at 05:33
  • Hey, that's a nice tip thank you. And I'm sorry but I'm just a noob you can close it =P looks like I have a very long paper to read from that answer now. Cheers. – Delta Oct 18 '12 at 05:34
  • 1
    Don't feel bad Delta, many a noob and, alas, not so noob get tricked with this issue ;-) – mjv Oct 18 '12 at 05:35
  • See [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – rsp Oct 18 '12 at 05:37

0 Answers0