Created
May 10, 2015 20:08
-
-
Save vinipsmaker/2a1da67d1c038efd0ebe to your computer and use it in GitHub Desktop.
IEEE 754 fp is not associative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clang++ doubletest.cpp -std=c++11 -stdlib=libc++ -lc++abi | |
#include <iostream> | |
#include <ios> | |
#include <limits> | |
using namespace std; | |
static_assert(numeric_limits<double>::is_iec559, "Must use IEEE 754 fp"); | |
int main(int argc, char *argv[]) | |
{ | |
volatile double a = 1.; | |
volatile auto b = numeric_limits<double>::epsilon() / double{2.}; | |
volatile auto c = b; | |
volatile auto lhs = a + b + c; | |
volatile auto rhs = c + b + a; | |
cout << hexfloat; | |
cout << "a\n " << a << endl; | |
cout << "b\n " << b << endl; | |
cout << "c\n " << c << endl; | |
cout << "lhs\n " << lhs << endl; | |
cout << "rhs\n " << rhs << endl; | |
// Should be true, right? | |
cout << "lhs == rhs\n " << boolalpha << (lhs == rhs) << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | |
0x1p+0 | |
b | |
0x1p-53 | |
c | |
0x1p-53 | |
lhs | |
0x1p+0 | |
rhs | |
0x1.0000000000001p+0 | |
lhs == rhs | |
false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment