【问题标题】:Is fpclassify(x) == FP_NAN functionally equivalent to isnan(x)?fpclassify(x) == FP_NAN 在功能上是否等同于 isnan(x)?
【发布时间】:2021-02-10 17:34:40
【问题描述】:

fpclassify(x) == FP_NAN 在功能上是否等同于 isnan(x)

同样的问题:

  • fpclassify(x) == FP_INFINITEisinf(x)
  • fpclassify(x) == FP_NORMALisnormal(x)
  • fpclassify(x) == FP_SUBNORMALissubnormal(x)
  • fpclassify(x) == FP_ZEROiszero(x)

如果它们在功能上是等效的,那为什么需要重复呢?

【问题讨论】:

    标签: c floating-point c17


    【解决方案1】:

    它们在功能上是等效的。但是fpclassify 允许您执行单个测试并使用switch 语句,这可能比链接的if/else if/else 块用于执行类型稍快和/或生成更简单的代码通过类型检查(假设fpclassify 本身有有效的方法来区分自己;不会发誓),例如每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";
        }
    }
    

    【讨论】:

    • 相反,在只需要一项特定检查的情况下,这很可能比使用fpclassify() 更有效。
    猜你喜欢
    • 2012-04-02
    • 2014-02-14
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2014-11-21
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多