Skip to content
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

Equality for floating point numbers of points #49

Open
flippmoke opened this issue Mar 27, 2017 · 4 comments
Open

Equality for floating point numbers of points #49

flippmoke opened this issue Mar 27, 2017 · 4 comments

Comments

@flippmoke
Copy link
Member

https://github.com/mapbox/geometry.hpp/blob/master/include/mapbox/geometry/point.hpp#L25

template <typename T>
constexpr bool operator==(point<T> const& lhs, point<T> const& rhs)
{
    return lhs.x == rhs.x && lhs.y == rhs.y;
}

Would it be better in the case of double or float that we used an approximate equality here?

@jfirebaugh
Copy link
Contributor

The appropriate epsilon for approximate equality varies by context, so I think if you need approximate equality you're best off creating your own function that compares the way you want. That said, I can see an argument for straight up disabling operator== for floating point coordinate types.

@flippmoke
Copy link
Member Author

The appropriate epsilon for approximate equality varies by context

While I agree, I think a reasonable assumption for a general case could be made?

template <typename T>
bool values_are_equal(T a, T b) 
{
    return a == b;
}

template <>
bool values_are_equal<double>(double a, double b) 
{
    return std::fabs(a - b) < (3.0 * std::numeric_limits<double>::epsilon());
}   

However, if we could not find a reasonable balance I would suggest disabling as well.

@joto
Copy link

joto commented Nov 2, 2017

The Catch unit test framework has an Approx class which is used to compare doubles. Maybe the implementation and docs can provide some insight.

@artemp
Copy link
Contributor

artemp commented Nov 2, 2017

@joto - thanks.
There is also a comprehensive peer-reviewed docs and implementation :
http://www.boost.org/doc/libs/1_65_1/libs/math/doc/html/math_toolkit/float_comparison.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants