4

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?

pmor
  • 5,392
  • 4
  • 17
  • 36

1 Answers1

4

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";
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 2
    Conversely, in sitations where just one specific check is needed, it is very likely that this is more efficient than using `fpclassify()`. – njuffa Feb 10 '21 at 17:57