Can the code below be modified such that it works correctly even when compiled by GCC with fast-math enabled?
#include <iostream>
#include <float.h>
using namespace std;
int main()
{
char divider = 2;
float power = 1;
float number = 1;
float current = number + power;
cout.precision(20);
// Divide until rounded off
while(current != number)
{
power /= divider;
current = number + power;
//cout << current << endl;
}
cout << power * divider << endl;
cout << FLT_EPSILON << endl;
}
Note: I have it in a header file and I haven't managed to turn off fast math for the header. See Strange while loop behavior and How to disable fast math for a header file function
Adding this piece of code into the loop solves the problem for me.
std::ostringstream buff;
buff << current
Edit:
Even adding this into the condition works:
&& ! isinf(current)