C ++将无穷大转换为最大值并将-inf转换为最小值
c++ convert infinity to max and -inf to min
如何实现下面的动作?
if( boost::math:: +is_inf (x) )
x= max double;
else if( boost::math:: -is_inf (x) )
x= min double;
我想保留 x 的符号。
这里的关键是,你可以用<
和>
比较+inf
或-inf
为0,所以测试符号很容易。
if(std::isinf(x))
{
if(x>0)
x = std::numeric_limits<double>::max();
else
x = -std::numeric_limits<double>::max();
}
如果您使用的是 c++11,则无需提升
如何实现下面的动作?
if( boost::math:: +is_inf (x) )
x= max double;
else if( boost::math:: -is_inf (x) )
x= min double;
我想保留 x 的符号。
这里的关键是,你可以用<
和>
比较+inf
或-inf
为0,所以测试符号很容易。
if(std::isinf(x))
{
if(x>0)
x = std::numeric_limits<double>::max();
else
x = -std::numeric_limits<double>::max();
}
如果您使用的是 c++11,则无需提升