Issue 163470
Summary [clang] No-zero result from `a*a - b*b` when a == b
Labels clang
Assignees
Reporter zhqrbitee
    Hi,
For below codes,
```cpp 
#include <iostream>
#include <cmath>

double test1(double a, double b) {
    return (a*a - b*b) ;
}


int main() {
 std::cout << "---------------------------------" << std::endl;
 std::cout << test1(1.1, 1.1) << std::endl;
    std::cout << "---------------------------------" << std::endl;
    return 0;
}
``` 
If `a == b`, then I would expect the `test1` function return me exactly `0.0`, as `a * a` should be exactly equal to `b * b`. However, it always return me a small value in my MacBook (with M2 chips). 
I compared the assembly codes from latest clang and GCC with `-O3` in compiler explorer (in arm64): both of them use fused multiply-add instructions, but the GCC one can produce 0.0 as expected, while the clang one produce `8.88178e-18`. 

<img width="2559" height="882" alt="Image" src="" />

The compiler explorer link is:  https://godbolt.org/z/93Tej6d66

I know floating-point issues can be subtle, but it seems returning 0.0 in this case makes more sense to me. 





_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to