I think it would be better to use isnan().
isnan() returns true if f is not-a-number. But it will return true for e.g. 0.0 ...
#include <cmath>
bool function(float x)
{
float f = doCalculation(x);
return isnan(f) ? false : true;
}
as mentioned that will not catch the case where f is 0.0 - or very close to it.
If you need this you could check with:
bool near0 = std::abs(f) > std::numeric_limits<float>::epsilon();
EDIT: here an improved example including a test driver:
#include <cmath>
#include <limits>
#include <iostream>
#include <vector>
// using namespace std;
bool fn(float f) {
if (isnan(f)) return false; // it is not-a-number
return std::abs(f) > std::numeric_limits<float>::epsilon();
}
// testdriver
int main(void) {
std::vector<float> t;
t.push_back(0.0);
t.push_back(0.1);
t.push_back(-0.1);
t.push_back( 0.0 + std::numeric_limits<float>::epsilon());
t.push_back( 0.0 - std::numeric_limits<float>::epsilon());
t.push_back( 0.0 - 2*std::numeric_limits<float>::epsilon());
t.push_back( 0.0 + 2*std::numeric_limits<float>::epsilon());
t.push_back( 1.0 * std::numeric_limits<float>::epsilon());
t.push_back(-0.1 * std::numeric_limits<float>::epsilon());
t.push_back( 0.1 * std::numeric_limits<float>::epsilon());
for (unsigned int i=0; i<t.size(); i++) {
std::cout << "fn(" << t[i] << ") returned " << fn(t[i]) << std::endl;
}
}
testresults:
fn(0) returned 0
fn(0.1) returned 1
fn(-0.1) returned 1
fn(1.19209e-07) returned 0
fn(-1.19209e-07) returned 0
fn(-2.38419e-07) returned 1
fn(2.38419e-07) returned 1
fn(1.19209e-07) returned 0
fn(-1.19209e-08) returned 0
fn(1.19209e-08) returned 0