• Advertisement
Sign in to follow this  

isNearZero( Vector)

This topic is 1730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you check that a vector is so short that it should be considered as null? Like when calculating cross-product or scalar product of 2 vectors ...when do you consider the result null, so the 2 vectors are parallel or perpendicular? etc. Should it depend on the length of the input vectors?

 

Now I just check all 3 coordinates if all of them are between a -eps and +eps value. And what should this epsilon value be in general, ...that I use for telling if a number is "zero" or not? Should it be different for 4 and 8 byte float/double? Thanks!

Share this post


Link to post
Share on other sites
Advertisement

Well, first, why do you need such a function? These kinds of manipulations usually hide a deeper design problem which means your code is fragile. Ignoring that, as for using epsilons, it seems to be a reasonable approach, and provided you don't lose any precision (dot and cross products are only multiplications and additions so it should be fine) then you could use 1e-6 for floats and 1e-13 for doubles (I'm sure you can push it a bit farther and I have no idea what the ideal value is but it's worked pretty well for me so far in most cases). Due to the nature of floating-point arithmetic I don't think there is an ideal epsilon value, actually, it depends on the input values..

Share this post


Link to post
Share on other sites

These kinds of manipulations usually hide a deeper design problem which means your code is fragile

 

Thanks! Yes, ...thats my philosophy too but I prefer throwing assert messages and write logs, instead of seeing that the geometry blew up or disappeared:)

Share this post


Link to post
Share on other sites

If it's just for assertions, why pepper the code with a ton of if's, instead of just doing "return dot(v,v) < eps"? I'd imagine a dot product and one comparison to be not just shorter and easier to read, but also a good bit faster. Only thing to keep in mind is that this returns the squared length, but that shouldn't really matter much.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement