• Create Account

## isNearZero( Vector)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Aliii  Members

1455
Like
0Likes
Like

Posted 23 April 2013 - 10:04 PM

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!

### #2Bacterius  Members

13100
Like
0Likes
Like

Posted 24 April 2013 - 04:33 AM

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..

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3Aliii  Members

1455
Like
0Likes
Like

Posted 24 April 2013 - 03:22 PM

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:)

### #4Trienco  Members

2555
Like
1Likes
Like

Posted 24 April 2013 - 10:21 PM

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.

f@dzhttp://festini.device-zero.de

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.