Git Product home page Git Product logo

Comments (6)

 avatar commented on July 17, 2024

Ok so i looked up where you guys found the implementation for this function and the original author thinks that it's ok that pure 0 isn't equal to something very very close to zero.

    assertFalse(nearlyEqual(0.00000001f, 0.0f));
    assertFalse(nearlyEqual(0.0f, 0.00000001f));
    assertFalse(nearlyEqual(-0.00000001f, 0.0f));
    assertFalse(nearlyEqual(0.0f, -0.00000001f));

Personally I can't agree with that but I don't want to change the lib if you guys think so.

from mathgl.

 avatar commented on July 17, 2024

UPDATE: ok i digged in deeper and realised that epsilon passed to FloatEqualThreshold isnt the max difference between a and b. This should be mentioned and it should be clear how it scales with numbers.

from mathgl.

 avatar commented on July 17, 2024

Also, the declaration in java is

public static boolean nearlyEqual(float a, float b, float epsilon) {
    final float absA = Math.abs(a);
    final float absB = Math.abs(b);
    final float diff = Math.abs(a - b);

    if (a == b) { // shortcut, handles infinities
        return true;
    } else if (a == 0 || b == 0 || diff < Float.MIN_NORMAL) {
        // a or b is zero or both are extremely close to it
        // relative error is less meaningful here
        return diff < (epsilon * Float.MIN_NORMAL);
    } else { // use relative error
        return diff / Math.min((absA + absB), Float.MAX_VALUE) < epsilon;
    }
}

while ours is

func FloatEqualThreshold(a, b, epsilon float32) bool {
    if a == b { // Handles the case of inf or shortcuts the loop when no significant error has     accumulated
        return true
    }

    diff := Abs(a - b)
    if a*b == 0 || diff < MinNormal { // If a or b are 0 or both are extremely close to it
        return diff < epsilon*epsilon
    }

    // Else compare difference
    return diff/(Abs(a)+Abs(b)) < epsilon
}

Any reason we do epsilon*epsilon instead of epsilon*MinNormal?

from mathgl.

 avatar commented on July 17, 2024

We should add a function that takes a target float, a "biggest difference" and returns a epsilon

from mathgl.

slimsag avatar slimsag commented on July 17, 2024

@hydroflame I haven't read everything here -- but you may be looking for relative equality, e.g. https://github.com/azul3d/lmath/blob/master/math.go#L12-L22 (http://realtimecollisiondetection.net/blog/?p=89)

from mathgl.

 avatar commented on July 17, 2024

Yeah we still need a clearer documentation

from mathgl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.