I’m looking at some 3rd party code and am unsure exactly what one line is doing. I can’t post the exact code but it’s along the lines of:
bool function(float x)
{
float f = doCalculation(x);
return x > 0 ? f : std::numeric_limits<float>::infinity();
}
This obviously throws a warning from the compiler about converting float->bool, but what will the actual behaviour be? How does Visual C++ convert floats to bools? At the very least I should be able to replace that nasty infinity…
I think it is a mistake. That function should return a float. This seem logical to me.
The conversion float to bool is the same as float != 0. However, strict comparing two floating points is not always as you’d expect, due to precision.