barej
barej

Reputation: 1406

c++ convert infinity to max and -inf to min

How to implement the following action?

if( boost::math:: +is_inf (x) )
    x= max double;
else if( boost::math:: -is_inf (x) )
    x= min double;

I want to preserve the sign of x.

Upvotes: 0

Views: 545

Answers (1)

galinette
galinette

Reputation: 9340

The key here is, you can compare +inf or -inf to 0 with < and >, so testing the sign is very easy.

if(std::isinf(x))
{
    if(x>0)
        x = std::numeric_limits<double>::max();
    else
        x = -std::numeric_limits<double>::max();
}

No needs for boost, if you are using c++11

Upvotes: 5

Related Questions