C++11 使用nearbyint()四舍五入时无例外保证

C++11 使用nearbyint()四舍五入时无例外保证,c++11,rounding,C++11,Rounding,我知道nearbyint允许我在不抛出异常的情况下对整数进行舍入。可以使用feclearexcept检查错误或查看是否进行了舍入(nearbyint始终如此) 有谁能给我举个例子,说明什么时候会抛出一个通过使用nearbyint避免的异常 以下是该函数的正常使用示例: #include <cfenv> #include <cmath> #include <iostream> void test(const char* title, int round_mod

我知道nearbyint允许我在不抛出异常的情况下对整数进行舍入。可以使用feclearexcept检查错误或查看是否进行了舍入(nearbyint始终如此)

有谁能给我举个例子,说明什么时候会抛出一个通过使用nearbyint避免的异常

以下是该函数的正常使用示例:

#include <cfenv>
#include <cmath>
#include <iostream>

void test(const char* title, int round_mode)
{
    std::feclearexcept(FE_ALL_EXCEPT);
    std::fesetround(round_mode);
    std::cout << title << std::endl;
    std::cout << "nearbyint(2.5) = " << std::nearbyint(2.5) << std::endl;
    std::cout << "nearbyint(-2.5) = " << std::nearbyint(-2.5) << std::endl;
    std::cout << "FE_INEXACT = " << std::boolalpha << (std::fetestexcept(FE_INEXACT) != 0) << std::endl << std::endl;   // This will always be true.
}

#define test(mode) test(#mode, mode)

int main()
{
#ifdef FE_DOWNWARD
    test(FE_DOWNWARD);
#endif
#ifdef FE_TONEAREST
    test(FE_TONEAREST);
#endif
#ifdef FE_TOWARDZERO
    test(FE_TOWARDZERO);
#endif
#ifdef FE_UPWARD
    test(FE_UPWARD);
#endif
}
#包括
#包括
#包括
无效测试(常量字符*标题,整数圆形模式)
{
std::FeClearException(FE_ALL_除外);
标准::fesetround(round_模式);
标准::cout