Add new methods for floating numbers: lessOrEqual and greaterOrEqual

#134 Merged at f77e5cb
Repository
ocrave
Branch
inequality_tests
Repository
ignitionrobotics
Branch
ign-math2
Author
  1. samuel lekieffre
Reviewers
Description

This pull request is a dependency of a another pull request in gazebo: "Make sure camera's FPS is strictly applied, should the real time factor be impacted"

Comments (8)

  1. samuel lekieffre author

    In my opinion, we cannot say that the operators <= and >= works fine with floating point numbers. Let's take this example:

    #include <iostream>
    
    int main()
    {
        float f = 0.1f;
        float sum;
        sum = 0;
    
        for (int i = 0; i < 10; ++i)
            sum += f;
        float product = f * 10;
    
        if (sum <= product)
            std::cout << "sum <= product" << std::endl;
        else
            std::cout << "sum > product" << std::endl;
    }
    

    We could expect to have "sum <= product", which is not what is produced when executing this code. A tolerance is actually mandatory in order to have results in line with arithmetic, and this is exactly for the same reason we use a tolerance in the ignition::math::equal() method. So I believe that the presence of lessOrEqual() and greaterOrEqual() is as much justified as for equal().

    That being said, I agree that the implementation could be much simpler, like Steven suggested.

  2. Nathan Koenig

    I'll be pedantic for two seconds. The <= and >= operators do work as expected. The problem is associated with floating point representation, not the comparison operators. You could (correctly) make the same argument for ==. Equality tends to elicit a more pronounced error, and I do get your point about the <= and >= operators.

    Also, a tolerance can be dangerous and should not be considered mandatory. A tolerance value should be in line with range of values that are to be compared.

    This pull request does remind me that a better name should be used for equal. Can you change the function names to lessOrNearEqual and greaterOrNearEqual?