标准数学函数能否正确处理无穷大?

Can the standard math functions handle infinity correctly?

int main() {
    double inf = INFINITY;
    double pi = acos(-1.0);
    printf("[1]: %f %f\n", atan(inf) / pi, atan(-inf) / pi);
    printf("[2]: %f %f\n", tan(inf) / pi, tan(-inf) / pi);
    return 0;
}

产出

[1]: 0.500000 -0.500000
[2]: -nan -nan

标准定义了这种行为吗? [2] 是未定义的行为吗?未指定?

我想确定至少 [1] 是一个有保证的结果。

两者都是明确定义的行为。 引用自 http://en.cppreference.com

  • 棕褐色

    If the argument is ±0, it is returned unmodified.
    If the argument is ±∞, NaN is returned and FE_INVALID is raised.
    If the argument is NaN, NaN is returned.

  • 阿坦

    If the argument is ±0, it is returned unmodified.
    If the argument is +∞, +π/2 is returned.
    If the argument is -∞, -π/2 is returned.
    If the argument is NaN, NaN is returned.