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

标签 c++ c math floating-point

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.

关于c++ - 标准数学函数能否正确处理无穷大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30696923/

相关文章:

c++ - 简单的继承问题

C、复合赋值-算术运算,转换可能会丢失信息

python - 在 OSX 上使用 openMP 支持编译 cython

c++ - C/C++ 中的指针及其内存空间

c - 向客户端发送 100,000 个 UDP 数据包(C Socket 编程)

c++ - 如何解决这个逻辑运算符问题?

math - 3D 向量的哈希值

python - 为什么使用参数会使这个函数慢得多?

c++ - 如何使用 LibSoX 混合两个 wav 文件

c - Pset4 (cs50) 恢复无法正常工作。它可以编译,但不能恢复超过 2 个 jpeg。检查 JPEG 签名是否有问题?