question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vector.normalize() needs to avoid division by zero

See original GitHub issue

Current normalization code is as follows:

    public Vector3d normalize(Vector3d dest) {
        double invLength = 1.0 / length();
        dest.x = x * invLength;
        dest.y = y * invLength;
        dest.z = z * invLength;
        return dest;
    }

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 or NaN, since these values propagate virally):

    public Vector3d normalize(Vector3d dest) {
        double lengthSq = lengthSquared();
        if (lengthSq < 1.0e-30) {
            dest.x = 0.0;
            dest.y = 0.0;
            dest.z = 0.0;
        } else {
            double invLength = 1.0 / Math.sqrt(lengthSq);
            dest.x = x * invLength;
            dest.y = y * invLength;
            dest.z = z * invLength;
        }
        return dest;
    }

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 of normalize() can be quickly checked:

    public boolean isZero() {
        return x == 0.0 && y == 0.0 && z == 0.0;
    }

There should probably also be a Vector.isCloseToZero() method that would replace the if (lengthSq < 1.0e-30) test above:

    public boolean isCloseToZero() {
        double lengthSq = lengthSquared();
        return lengthSq < 1.0e-30;
    }

The constant 1.0e-30 could be smaller for float vectors (maybe 1.0e-12f) – it should be something above the precision floor, but below the probable error/noise floor for the majority of applications.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lukehutchcommented, Feb 27, 2020

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

1reaction
httpdigestcommented, Feb 27, 2020

Alright, just manually retriggered the build and this one went through, so 1.9.23-SNAPSHOT is available now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Normalize vector by zero - math - Stack Overflow
This can only fail is all the values of truth are zero. ... or to avoid the division when the norm is less...
Read more >
neural network - normalizing data and avoiding dividing by zero
I have data that I'm compressing with AutoEncoders (3-layer neural network) and I would like to normalize my data first. I would like...
Read more >
Divide-by-0 bugs in Vector*.Normalize - Community
Vector2 Normalize() has a divide-by-0 bug: If I remember correctly that matches XNA behavior. Normalizing a zero length vector will result in a ......
Read more >
simple question about vector normalization - GameDev.net
Mathematically a vector is normalized by dividing it through its length. Division by zero is undefined, so normalizing a zero-length vector is ...
Read more >
Modelica.Math.Vectors.normalize
The function call " Vectors.normalize(v) " returns the unit vector " v/length(v) " of vector v. ... v/eps is returned in order to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found