Val! = Std :: numeric_limits <double> :: infinity () or! isinf (val) or isfinite (val)

Most likely bike-shedding , but maybe there is something interesting that I'm missing ...

If the class initializes the member valbefore std::numeric_limits<double>::infinity(), and later wants to check if the val value has been changed to something real (+/- inf are invalid here), what are the tradeoffs of these three approaches and I miss other interesting ways to solve this problem. (remote const for readability in the examples.)

bool IsInit() { return MinX != std::numeric_limits<double>::infinity(); }  // a

bool IsInit() { return !std::isinf(MinX); }  // b

bool IsInit() { return std::isfinite(MinX); }  // c

At the moment, the code is C ++ 03, but how to change the parameters using C ++ 11, C ++ 14 and C ++ 17. For example, with C ++ 17 this code can be simple std::optional<double> val. Or would it be safer for NaN to be a safer bet, just incase +/- inf became valid in the future?

This happened when I read this patch for this code:

For convenience:

Relatively related:

+7
source share
2 answers

, . , . . NaN, , , , , .

, "" . , , , . , , : , .

, , OGREnvelope (, , AABB) github, , :

  • IsInit - - , , AABB . , /, IsInit ( ). IsValid .
  • , , , , AABB MinX, , MaxX, MinY, , MaxY. , AABB. MinX MaxX ( min max Y).
  • , , : (a) AABB AABB ( , 14 4); (b) AABB, max() lowest().
  • , - MinX, MaxX, MinY, MaxY , , IsInit , .

, NaN . , MinX != std::numeric_limits<double>::infinity(). IsInit ( ) . IsValid, :

bool IsValid() const
{
    return !std::isnan(MinX) && !std::isnan(MinY) && MinX <= MaxX && MinY <= MaxY;
}

AABB OGREnvelope, ( NaN), X Y .

+4

, , , , .

, Godbolt: https://godbolt.org/z/184Zb6

, std::numeric_limits<double>::infinity() ( ), std::isfinite(); !std::isinf() , , !std::isinf() - .

0

Source: https://habr.com/ru/post/1015912/


All Articles