-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vector.normalize() needs to avoid division by zero #211
Comments
I want to keep methods with (let's say) arbitrary epsilons and error recovery very low in JOML. If you try to normalize a zero vector, then you will be getting undefined (NaN) as result, because dividing zero by zero is undefined. In my opinion, this is more desirable than recovering from it in a way that may not be suitable for the client/user-application. |
Fair argument. You should at least document all methods that can return infinity/NaN however, and suggest in the Javadoc that the user call Also some methods can set just some components of a result to infinity/NaN. For example, |
Yes, I'll document it and will add a Quaternion.isFinite() too. |
|
I did meant that with my comment #211 (comment) (did you open the link?) |
Oh, sorry, I didn't -- I assumed that was a link to |
Changes are pushed, but it seems the oss.sonatype.org endpoint to publish snapshots is currently down (504 gateway timeout in the Travis build when trying to upload the artifacts): https://travis-ci.org/JOML-CI/JOML/jobs/655543569 (also it seems the logs cannot be pulled fully, so Travis also seems to have an issue... |
Oh man, I run into this all the time when attempting to publish to Sonatype. I keep filing bugs each time it happens, but they never seem to fix the underlying causes in a robust way. It's a reliably unreliable service :-) But there's no great alternative right now (I tried BinTray, and it has its own problems...) Thanks for your work on this! |
Alright, just manually retriggered the build and this one went through, so 1.9.23-SNAPSHOT is available now. |
Just one thing missing... Any method that can result in NaN/infinity in any component needs to recommend in the Javadoc that the user call isFinite on the result, if there's any chance of this happening (eg. when normalizing a zero vector). |
Current normalization code is as follows:
Instead division by zero should always be avoided, and the code should be something like the following to prevent division-by-zero (arguably dealing with a zero vector in the result is better from a robustness point of view than dealing with
Inf
orNaN
, since these values propagate virally):Then there should be a
Vector.isZero()
method for quickly testing if a vector is (exactly) equal to zero, so that return conditions from methods like the result ofnormalize()
can be quickly checked:There should probably also be a
Vector.isCloseToZero()
method that would replace theif (lengthSq < 1.0e-30)
test above:The constant
1.0e-30
could be smaller forfloat
vectors (maybe1.0e-12f
) -- it should be something above the precision floor, but below the probable error/noise floor for the majority of applications.The text was updated successfully, but these errors were encountered: