Skip to content

Instantly share code, notes, and snippets.

@vinipsmaker
Created May 10, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinipsmaker/2a1da67d1c038efd0ebe to your computer and use it in GitHub Desktop.
Save vinipsmaker/2a1da67d1c038efd0ebe to your computer and use it in GitHub Desktop.
IEEE 754 fp is not associative
// 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;
}
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