Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::numeric_limits<T>::max() a function?

In the C++ Standard Library the value std::numeric_limits<T>::max() is specified as a function. Further properties of a specific type are given as constants (likestd::numeric_limits<T>::is_signed). All constants that are of type T are given as functions, whereas all other constants are given as, well, constant values.

What's the rationale behind that?

like image 879
ablaeul Avatar asked May 04 '10 14:05

ablaeul


People also ask

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T.

What does Numeric_limits mean in C++?

std::numeric_limits ::digits in C++ with Example Return Value: The function std::numeric_limits<T>::digits returns the number of radix digits that the type can represent without loss of precision.

How do you use limits in C++?

The C++ Standard Library header <limits> includes <climits> , which includes <limits. h> . Microsoft C also permits the declaration of sized integer variables, which are integral types of size 8-, 16-, 32- or 64-bits. For more information on sized integers in C, see Sized Integer Types.

What is epsilon C++?

Machine Epsilon is a machine-dependent floating point value that provides an upper bound on relative error due to rounding in floating point arithmetic. Mathematically, for each floating point type, it is equivalent to the difference between 1.0 and the smallest representable value that is greater than 1.0.


1 Answers

To expand on Neil's remark, std::numeric_limit<T> is available for any number type including floating point numbers, and if you dig through the comp.lang.c++ thread, you'll see the mention that it might not be possible to define the static variables for floating point values.

So, for consistency they decided to put both integral and floating points behind methods.

It will change with C++0x, so there's hope.

like image 171
Matthieu M. Avatar answered Oct 14 '22 10:10

Matthieu M.