Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INT_[MIN|MAX] limit macros vs numeric_limits<T>

Tags:

c++

Is there any argument for using the numeric limits macros (e.g. INT64_MAX) over std::numeric_limits<T>? From what I understand numeric_limits is in the standard but the macros are only in C99 so therefore non-standard.

like image 342
Graeme Avatar asked Apr 07 '11 10:04

Graeme


People also ask

What is Numeric_limits int min ()?

numeric_limits::minReturns the minimum finite value representable by the numeric type T . For floating-point types with denormalization, min returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min for integral types.

Which data type is accepted by Numeric_limits?

Data types that supports std::numeric_limits() in C++ std::numeric_limits<int>::max() gives the maximum possible value we can store in type int. std::numeric_limits<unsigned int>::max()) gives the maximum possible value we can store in type unsigned int.


1 Answers

The other answers mostly have correct information, but it seems that this needs updating for C++11.

In C++11, std::numeric_limits<T>::min(), std::numeric_limits<T>::max(), and std::numeric_limits<T>::lowest() are all declared constexpr, so they can be usable in most of the same contexts as INT_MIN and company. The only exception I can think of is compile-time string processing using the # stringification token.

This means that numeric_limits can be used for case labels, template parameters, etc., and you get the benefit of using it in generic code (try using INT_MIN vs. LONG_MIN in template<typename T> get_min(T t);).

C++11 also brings a solution to the issue James Kanze talks about, by adding std::numeric_limits<T>::lowest(), which gives the lowest finite value for all types, rather than the lowest value for integer types and the lowest positive value for floating-point types.

like image 106
David Stone Avatar answered Sep 18 '22 05:09

David Stone