-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Closed as not planned
Labels
floating-pointFloating-point mathFloating-point mathquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Description
Hi,
For below codes,
#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 produces 8.88178e-18.
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.
Metadata
Metadata
Assignees
Labels
floating-pointFloating-point mathFloating-point mathquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!