I think there is a problem in G++ compiler about double operation.
I was trying to test some program, and found bugs in comparing(==) double. So, I read some articles and found out that double comparison should work with comparing EPSILON(really small number) https://stackoverflow.com/a/17341/7105963
I wrote code as below, but It doesn't work as I expected.
As you see in main code, I sent double parameter A and A + EPSILON to function is_same(). Because of if condition, obviously(mathematically) std::abs(A - (A + EPSILON)) = std::abs(EPSILON), this should return false. but, it returns true(1) in G++ (9.3.0).
And also, A + EPSILON doesn't calculate. It just returns A in std::cout. Is there any function to calculate double variables more precisely?
Code
#include <iostream>
#include <cmath>
bool is_same(double __x, double __y) {
    if(std::abs(__x - __y) < __DBL_EPSILON__) {
        std::cout << std::fixed << "IN Function __x\t\t" << __x << "\n";
        std::cout << std::fixed << "IN Function __y\t\t" << __y << "\n";
        std::cout << std::fixed << "std::abs(__x - __y)\t" << std::abs(__x - __y) << "\n";
        return true;
    } else {
        return false;
    }
}
double distance(double x1, double y1, double x2, double y2) {
    return std::sqrt(std::pow((x2 - x1), 2) + std::pow((y2 - y1), 2));
}
int main() {
    std::cout.precision(17);        // maximum precision : https://stackoverflow.com/a/554134/7105963
    std::cout << std::fixed << "dist (0, 0) ~ (3, 4)\t" << distance(0, 0, 3, 4) << "\n";
    std::cout << std::fixed << "EPSILON(small)\t\t" << __DBL_EPSILON__ << "\n";
    std::cout << std::fixed << "distance + EPSILON\t" << (distance(0, 0, 3, 4) + __DBL_EPSILON__) << "\n";
    std::cout << std::fixed << "distance - EPSILON\t" << (distance(0, 0, 3, 4) - __DBL_EPSILON__) << "\n";
    // std::cout << is_same(distance(0, 0, 3, 4), (distance(0, 0, 3, 4) + __DBL_EPSILON__)) << "\n";
    std::cout << is_same(distance(0, 0, 3, 4), (distance(0, 0, 3, 4) + __DBL_EPSILON__)) << "\n";
}
Output
dist (0, 0) ~ (3, 4) 5.00000000000000000 EPSILON(small) 0.00000000000000022 distance + EPSILON 5.00000000000000000 distance - EPSILON 5.00000000000000000 IN Function __x 5.00000000000000000 IN Function __y 5.00000000000000000 std::abs(__x - __y) 0.00000000000000000 1
Environment
- Docker GCC(9.3.0) (precisely G++ 9.3.0)
