Sign in to follow this  
Aliii

isNearZero( Vector)

Recommended Posts

Aliii    1456

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
Bacterius    13165

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
Aliii    1456

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
Trienco    2555

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this