F
Fred Zwarts
In a template function I need the minimum and maximum value of the valid range of values of a type.
For integer types I can use std::numeric_limits<Type>::min () and std::numeric_limits<Type>::max () for this purpose.
But this template function is also used for floating point types.
For floating point types std::numeric_limits<Type>::min () does not return what it suggests, the minimum value,
but it returns the smallest value larger than 0. (I wonder why this confusion was introduced.)
My question is how to find the real minimum value in such cases.
Is -std::numeric_limits<Type>::max () a good choice?
I know that for signed integer types the assumption that
-std::numeric_limits<Type>::max () is the most negative valid value for that type
is often wrong. How is that for floating point types?
Would the following code in a template function result in the best value?
Type RealMinimun = std::numeric_limits<Type>::min () > 0 ? // Test for floating point type
-std::numeric_limits<Type>::max () // Floating point minimum
: std::numeric_limits<Type>::min (); // Integer minimum
This results in a compiler warning when the template is instantiated for unsigned integer types,
which should not be negated.
I can ignore this warning, because the negation is not used in this case.
For integer types I can use std::numeric_limits<Type>::min () and std::numeric_limits<Type>::max () for this purpose.
But this template function is also used for floating point types.
For floating point types std::numeric_limits<Type>::min () does not return what it suggests, the minimum value,
but it returns the smallest value larger than 0. (I wonder why this confusion was introduced.)
My question is how to find the real minimum value in such cases.
Is -std::numeric_limits<Type>::max () a good choice?
I know that for signed integer types the assumption that
-std::numeric_limits<Type>::max () is the most negative valid value for that type
is often wrong. How is that for floating point types?
Would the following code in a template function result in the best value?
Type RealMinimun = std::numeric_limits<Type>::min () > 0 ? // Test for floating point type
-std::numeric_limits<Type>::max () // Floating point minimum
: std::numeric_limits<Type>::min (); // Integer minimum
This results in a compiler warning when the template is instantiated for unsigned integer types,
which should not be negated.
I can ignore this warning, because the negation is not used in this case.