Custom Normalizing function not correct

Started by
20 comments, last by Corroded 19 years, 12 months ago
If you know a bit about how floating point works, you''d know that it is very much possible to predict the errors you will get, and that errors from rounding WILL NOT EVER cause a value to "accidentaly" become zero.
Advertisement
quote:Original post by Karg
Also, as far as moving the error checking outside the function; then you have multiple locations where code is reproduced, more potential for programmer error, duplicated code, more work for you and the compiler. Hardly worth removing one if statement that only takes more time if a zero magnitude is found (which, as you said, probably won''t happen).

The point is that a normalize() function doesn''t have enough information to handle invalid input correctly. What do you propose the function should do if it finds it''s input has magnitude 0? The function given simply fails silently which is a very bad ''solution'' - you just pass the bad data on to cause problems elsewhere. By the time the problem shows up it may be very hard to track back to the source of the problem. I''ve seen normalize() type functions which return an arbitrary unit vector (e.g. (1, 0, 0)) when passed a 0 length input which is even worse - a low level function like normalize() is in no position to decide what is a suitable way to ''fix up'' bad input. The fact is that passing a 0 vector into a normalize() function indicates a bug in the calling code and that''s where a fix should be applied. A reasonable solution is to put an assert in the function so that you''ll catch bad data during testing and have the information you need to fix the problem rather than fix the symptoms. Performance is really a side issue here - the central issue is that calling a normalize() function with a 0 length vector is a bug at the call site and so should be caught and fixed at the call site.

Game Programming Blog: www.mattnewport.com/blog

This topic is closed to new replies.

Advertisement