In the following code, the functions foo1,foo2 and foo3 are intended to be equivalent. However when run foo3 does not terminate from the loop, is there a reason why this is the case?
template <typename T>
T foo1()
{
T x = T(1);
T y = T(0);
for (;;)
{
if (x == y) break;
y = x;
++x;
}
return x;
}
template <typename T>
T foo2()
{
T x = T(0);
for (;;)
{
T y = x + T(1);
if (!(x != y)) break;
++x;
}
return x;
}
template <typename T>
T foo3()
{
T x = T(0);
while (x != (x + T(1))) ++x;
return x;
}
int main()
{
printf("1 float: %20.5f\n", foo1<float>());
printf("2 float: %20.5f\n", foo2<float>());
printf("3 float: %20.5f\n", foo3<float>());
return 0;
}
Note: This was compiled using VS2010 with /fp precise in release mode. Not sure how GCC etc would treat this code, any information would be great. Could this be an issue where in foo3, the x and x+1 values become NaN somehow?