Hacker News new | past | comments | ask | show | jobs | submit login

Why Vector.isZero() is checking whether a vector length is smaller than 0.0001? Why this arbitrary number and not e.g. 0.00000001?



Dunno about this case, but probably because floating point arithmetic is a bitch. Try the following in Chrome's developer console:

    (0.3 - 0.1 - 0.1 - 0.1) === 0.0
    > false
    0.3 - 0.1 - 0.1 - 0.1
    > -2.7755575615628914e-17
In order to get helpful results, we're gonna have to pick some semi-arbitrary epsilon. Still, my problem domain might require a different epsilon than they expect; even if they have a default, the API should allow me to specify my own choice of epsilon.

They might also want to consider being more nuanced for the equals method than just taking the difference and comparing to zero. See http://floating-point-gui.de/errors/comparison/


You're not the only one! The dev Branch has this fixed so you can pass your own epsilon.


Hooray! Thanks for pointing it out :)


While we're here, the article you linked recommends equality comparison w.r.t. "the maximum number of possible floating-point values between the two values".

Any idea on how to go about this in JavaScript? The binary representation of the float isn't as easy to come by (compared to C, for example), but I wonder if you couldn't get a decent approximation with Math.log2.


You can interact with binary representations of numbers in JavaScript using typed arrays – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Type....

For example, you can get an array of the bytes in a number `n` with this:

    new Uint8Array((new Float64Array([n])).buffer)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: