I've been wondering about the exact difference between the HUGE_VAL
macro constant and the INFINITY
one, which is also discussed in this question. It does seem that the two are functionally equivalent.
Is there any reason to prefer one over the other, aside from maybe distinguishing logically from a finite but huge value and a truly infinite value?
In particular, the use case I'm having now is about the kind of algorithms that need to find a max, and at the start they initialize the currently found max to -INFINITY
. -HUGE_VAL
could ever be preferable? If you have suggestions about other cases of usage please still tell them.
With C++, you should use the std::numeric_limits<T>
classes defined in the header instead of these macros.
Here's an example :
double val = std::numeric_limits<double>::max();
The classes are specialized for each numeric types supported by your implementation, i.e. char, int, long, float, double etc.
The max value given by those is particularly useful for finding a maximum value, as they are literally the greater value that can be represented by the queried type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With