Is fpclassify(x) == FP_NAN functionally equivalent to isnan(x)?

189 Views Asked by At

Is fpclassify(x) == FP_NAN functionally equivalent to isnan(x)?

The same question goes for:

  • fpclassify(x) == FP_INFINITE vs. isinf(x)
  • fpclassify(x) == FP_NORMAL vs. isnormal(x)
  • fpclassify(x) == FP_SUBNORMAL vs. issubnormal(x)
  • fpclassify(x) == FP_ZERO vs. iszero(x)

If they are functionally equivalent, then why need of duplicates?

1

There are 1 best solutions below

1
On BEST ANSWER

They're functionally equivalent. But fpclassify allows you to perform a single test and use a switch statement, which may be slightly faster and/or produce simpler code than the chained if/else if/else blocks would use to perform type by type checks (assuming fpclassify itself has efficient ways to differentiate itself; won't swear to that), e.g. per the cppreference example:

const char *show_classification(double x) {
    switch(fpclassify(x)) {
        case FP_INFINITE:  return "Inf";
        case FP_NAN:       return "NaN";
        case FP_NORMAL:    return "normal";
        case FP_SUBNORMAL: return "subnormal";
        case FP_ZERO:      return "zero";
        default:           return "unknown";
    }
}