Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I prefer HUGE_VAL or INFINITY?

Tags:

c++

infinity

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.

like image 811
Svalorzen Avatar asked Sep 16 '14 11:09

Svalorzen


1 Answers

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.

like image 69
rems4e Avatar answered Oct 20 '22 01:10

rems4e