標準とBoostのround - デフォルトの挙動

浮動小数点数の丸め演算の挙動の違いに関して。
C++11のstd::round()は、演算結果が値域エラーになる値を渡した場合、NaNを返します。

#include <iostream>
#include <cmath>
#include <limits>

int main()
{
    double x = std::round(std::numeric_limits<double>::quiet_NaN());
    std::cout << x << std::endl;
}
nan

Boost.Mathのboost::math::round()は、値域エラーになる場合、デフォルトでboost::math::rounding_error例外を送出します。

#include <iostream>
#include <boost/math/special_functions/round.hpp>
#include <limits>

int main()
{
    try {
        double x = boost::math::round(std::numeric_limits<double>::quiet_NaN());
    }
    catch (boost::math::rounding_error& e) {
        std::cout << e.what() << std::endl;
    }
}
Error in function boost::math::round<d>(d): Value nan can not be represented in the target integer type.

Boost.Mathの方は、エラー時の挙動をカスタマイズできるのですが、それはまた後日。