numeric_limits Class
The class template describes arithmetic properties of built-in numerical types.
template <class Type>
class numeric_limits
Type
The fundamental element data type whose properties are being tested or queried or set. Type can also be declared const
, volatile
, or const volatile
.
The header defines explicit specializations for the types wchar_t
, bool
, char
, signed char
, unsigned char
, short
, unsigned short
, int
, unsigned int
, long
, unsigned long
, float
, double
, long double
, long long
, unsigned long long
, char16_t
, and char32_t
. For these explicit specializations, the member numeric_limits::is_specialized is true
, and all relevant members have meaningful values. The program can supply additional explicit specializations. Most member functions of the class describe or test possible implementations of float
.
For an arbitrary specialization, no members have meaningful values. A member object that does not have a meaningful value stores zero (or false
) and a member function that does not return a meaningful value returns Type(0)
.
Name | Description |
---|---|
denorm_min | Returns the smallest nonzero denormalized value. |
digits | Returns the number of radix digits that the type can represent without loss of precision. |
digits10 | Returns the number of decimal digits that the type can represent without loss of precision. |
epsilon | Returns the difference between 1 and the smallest value greater than 1 that the data type can represent. |
has_denorm | Tests whether a type allows denormalized values. |
has_denorm_loss | Tests whether loss of accuracy is detected as a denormalization loss rather than as an inexact result. |
has_infinity | Tests whether a type has a representation for positive infinity. |
has_quiet_NaN | Tests whether a type has a representation for a quiet not a number (NAN), which is non-signaling. |
has_signaling_NaN | Tests whether a type has a representation for signaling not a number (NAN). |
infinity | The representation for positive infinity for a type, if available. |
is_bounded | Tests if the set of values that a type may represent is finite. |
is_exact | Tests if the calculations done on a type are free of rounding errors. |
is_iec559 | Tests if a type conforms to IEC 559 standards. |
is_integer | Tests if a type has an integer representation. |
is_modulo | Tests if a type has a modulo representation. |
is_signed | Tests if a type has a signed representation. |
is_specialized | Tests if a type has an explicit specialization defined in the class template numeric_limits . |
lowest | Returns the most negative finite value. |
max | Returns the maximum finite value for a type. |
max_digits10 | Returns the number of decimal digits required to ensure that two distinct values of the type have distinct decimal representations. |
max_exponent | Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
max_exponent10 | Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
min | Returns the minimum normalized value for a type. |
min_exponent | Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
min_exponent10 | Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
quiet_NaN | Returns the representation of a quiet not a number (NAN) for the type. |
radix | Returns the integral base, referred to as radix, used for the representation of a type. |
round_error | Returns the maximum rounding error for the type. |
round_style | Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value. |
signaling_NaN | Returns the representation of a signaling not a number (NAN) for the type. |
tinyness_before | Tests whether a type can determine that a value is too small to represent as a normalized value before rounding it. |
traps | Tests whether trapping that reports on arithmetic exceptions is implemented for a type. |
Returns the smallest nonzero denormalized value.
static constexpr Type denorm_min() throw();
The smallest nonzero denormalized value.
long double
is the same as double
for the C++ compiler.
The function returns the minimum value for the type, which is the same as min if has_denorm is not equal to denorm_present
.
// numeric_limits_denorm_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The smallest nonzero denormalized value" << endl
<< "for float objects is: "
<< numeric_limits<float>::denorm_min( ) << endl;
cout << "The smallest nonzero denormalized value" << endl
<< "for double objects is: "
<< numeric_limits<double>::denorm_min( ) << endl;
cout << "The smallest nonzero denormalized value" << endl
<< "for long double objects is: "
<< numeric_limits<long double>::denorm_min( ) << endl;
// A smaller value will round to zero
cout << numeric_limits<float>::denorm_min( )/2 <<endl;
cout << numeric_limits<double>::denorm_min( )/2 <<endl;
cout << numeric_limits<long double>::denorm_min( )/2 <<endl;
}
The smallest nonzero denormalized value
for float objects is: 1.4013e-045
The smallest nonzero denormalized value
for double objects is: 4.94066e-324
The smallest nonzero denormalized value
for long double objects is: 4.94066e-324
0
0
0
Returns the number of radix digits that the type can represent without loss of precision.
static constexpr int digits = 0;
The number of radix digits that the type can represent without loss of precision.
The member stores the number of radix digits that the type can represent without change, which is the number of bits other than any sign bit for a predefined integer type, or the number of mantissa digits for a predefined floating-point type.
// numeric_limits_digits_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << numeric_limits<float>::digits <<endl;
cout << numeric_limits<double>::digits <<endl;
cout << numeric_limits<long double>::digits <<endl;
cout << numeric_limits<int>::digits <<endl;
cout << numeric_limits<__int64>::digits <<endl;
}
24
53
53
31
63
Returns the number of decimal digits that the type can represent without loss of precision.
static constexpr int digits10 = 0;
The number of decimal digits that the type can represent without loss of precision.
// numeric_limits_digits10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << numeric_limits<float>::digits10 <<endl;
cout << numeric_limits<double>::digits10 <<endl;
cout << numeric_limits<long double>::digits10 <<endl;
cout << numeric_limits<int>::digits10 <<endl;
cout << numeric_limits<__int64>::digits10 <<endl;
float f = (float)99999999;
cout.precision ( 10 );
cout << "The float is; " << f << endl;
}
6
15
15
9
18
The float is; 100000000
The function returns the difference between 1 and the smallest value greater than 1 that is representable for the data type.
static constexpr Type epsilon() throw();
The difference between 1 and the smallest value greater than 1 that is representable for the data type.
The value is FLT_EPSILON for type float
. epsilon
for a type is the smallest positive floating-point number N such that N + epsilon
+ N is representable.
// numeric_limits_epsilon.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The difference between 1 and the smallest "
<< "value greater than 1" << endl
<< "for float objects is: "
<< numeric_limits<float>::epsilon( ) << endl;
cout << "The difference between 1 and the smallest "
<< "value greater than 1" << endl
<< "for double objects is: "
<< numeric_limits<double>::epsilon( ) << endl;
cout << "The difference between 1 and the smallest "
<< "value greater than 1" << endl
<< "for long double objects is: "
<< numeric_limits<long double>::epsilon( ) << endl;
}
The difference between 1 and the smallest value greater than 1
for float objects is: 1.19209e-007
The difference between 1 and the smallest value greater than 1
for double objects is: 2.22045e-016
The difference between 1 and the smallest value greater than 1
for long double objects is: 2.22045e-016
Tests whether a type allows denormalized values.
static constexpr float_denorm_style has_denorm = denorm_absent;
An enumeration value of type const float_denorm_style
, indicating whether the type allows denormalized values.
The member stores denorm_present
for a floating-point type that has denormalized values, effectively a variable number of exponent bits.
// numeric_limits_has_denorm.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects allow denormalized values: "
<< numeric_limits<float>::has_denorm
<< endl;
cout << "Whether double objects allow denormalized values: "
<< numeric_limits<double>::has_denorm
<< endl;
cout << "Whether long int objects allow denormalized values: "
<< numeric_limits<long int>::has_denorm
<< endl;
}
Whether float objects allow denormalized values: 1
Whether double objects allow denormalized values: 1
Whether long int objects allow denormalized values: 0
Tests whether loss of accuracy is detected as a denormalization loss rather than as an inexact result.
static constexpr bool has_denorm_loss = false;
true
if the loss of accuracy is detected as a denormalization loss; false
if not.
The member stores true for a type that determines whether a value has lost accuracy because it is delivered as a denormalized result (too small to represent as a normalized value) or because it is inexact (not the same as a result not subject to limitations of exponent range and precision), an option with IEC 559 floating-point representations that can affect some results.
// numeric_limits_has_denorm_loss.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects can detect denormalized loss: "
<< numeric_limits<float>::has_denorm_loss
<< endl;
cout << "Whether double objects can detect denormalized loss: "
<< numeric_limits<double>::has_denorm_loss
<< endl;
cout << "Whether long int objects can detect denormalized loss: "
<< numeric_limits<long int>::has_denorm_loss
<< endl;
}
Whether float objects can detect denormalized loss: 1
Whether double objects can detect denormalized loss: 1
Whether long int objects can detect denormalized loss: 0
Tests whether a type has a representation for positive infinity.
static constexpr bool has_infinity = false;
true
if the type has a representation for positive infinity; false
if not.
The member returns true
if is_iec559 is true
.
// numeric_limits_has_infinity.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have infinity: "
<< numeric_limits<float>::has_infinity
<< endl;
cout << "Whether double objects have infinity: "
<< numeric_limits<double>::has_infinity
<< endl;
cout << "Whether long int objects have infinity: "
<< numeric_limits<long int>::has_infinity
<< endl;
}
Whether float objects have infinity: 1
Whether double objects have infinity: 1
Whether long int objects have infinity: 0
Tests whether a type has a representation for a quiet not a number (NAN), which is nonsignaling.
static constexpr bool has_quiet_NaN = false;
true
if the type has a representation for a quiet NAN; false
if not.
A quiet NAN is an encoding for not a number, which does not signal its presence in an expression. The return value is true
if is_iec559 is true.
// numeric_limits_has_quiet_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have quiet_NaN: "
<< numeric_limits<float>::has_quiet_NaN
<< endl;
cout << "Whether double objects have quiet_NaN: "
<< numeric_limits<double>::has_quiet_NaN
<< endl;
cout << "Whether long int objects have quiet_NaN: "
<< numeric_limits<long int>::has_quiet_NaN
<< endl;
}
Whether float objects have quiet_NaN: 1
Whether double objects have quiet_NaN: 1
Whether long int objects have quiet_NaN: 0
Tests whether a type has a representation for signaling not a number (NAN).
static constexpr bool has_signaling_NaN = false;
true
if the type has a representation for a signaling NAN; false
if not.
A signaling NAN is an encoding for not a number, which signals its presence in an expression. The return value is true
if is_iec559 is true.
// numeric_limits_has_signaling_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have a signaling_NaN: "
<< numeric_limits<float>::has_signaling_NaN
<< endl;
cout << "Whether double objects have a signaling_NaN: "
<< numeric_limits<double>::has_signaling_NaN
<< endl;
cout << "Whether long int objects have a signaling_NaN: "
<< numeric_limits<long int>::has_signaling_NaN
<< endl;
}
Whether float objects have a signaling_NaN: 1
Whether double objects have a signaling_NaN: 1
Whether long int objects have a signaling_NaN: 0
The representation of positive infinity for a type, if available.
static constexpr Type infinity() throw();
The representation of positive infinity for a type, if available.
The return value is meaningful only if has_infinity is true
.
// numeric_limits_infinity.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << numeric_limits<float>::has_infinity <<endl;
cout << numeric_limits<double>::has_infinity<<endl;
cout << numeric_limits<long double>::has_infinity <<endl;
cout << numeric_limits<int>::has_infinity <<endl;
cout << numeric_limits<__int64>::has_infinity <<endl;
cout << "The representation of infinity for type float is: "
<< numeric_limits<float>::infinity( ) <<endl;
cout << "The representation of infinity for type double is: "
<< numeric_limits<double>::infinity( ) <<endl;
cout << "The representation of infinity for type long double is: "
<< numeric_limits<long double>::infinity( ) <<endl;
}
1
1
1
0
0
The representation of infinity for type float is: inf
The representation of infinity for type double is: inf
The representation of infinity for type long double is: inf
Tests if the set of values that a type may represent is finite.
static constexpr bool is_bounded = false;
true
if the type has a bounded set of representable values; false
if not.
All predefined types have a bounded set of representable values and return true
.
// numeric_limits_is_bounded.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have bounded set "
<< "of representable values: "
<< numeric_limits<float>::is_bounded
<< endl;
cout << "Whether double objects have bounded set "
<< "of representable values: "
<< numeric_limits<double>::is_bounded
<< endl;
cout << "Whether long int objects have bounded set "
<< "of representable values: "
<< numeric_limits<long int>::is_bounded
<< endl;
cout << "Whether unsigned char objects have bounded set "
<< "of representable values: "
<< numeric_limits<unsigned char>::is_bounded
<< endl;
}
Whether float objects have bounded set of representable values: 1
Whether double objects have bounded set of representable values: 1
Whether long int objects have bounded set of representable values: 1
Whether unsigned char objects have bounded set of representable values: 1
Tests if the calculations done on a type are free of rounding errors.
static constexpr bool is_exact = false;
true
if the calculations are free of rounding errors; false
if not.
All predefined integer types have exact representations for their values and return false
. A fixed-point or rational representation is also considered exact, but a floating-point representation is not.
// numeric_limits_is_exact.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have calculations "
<< "free of rounding errors: "
<< numeric_limits<float>::is_exact
<< endl;
cout << "Whether double objects have calculations "
<< "free of rounding errors: "
<< numeric_limits<double>::is_exact
<< endl;
cout << "Whether long int objects have calculations "
<< "free of rounding errors: "
<< numeric_limits<long int>::is_exact
<< endl;
cout << "Whether unsigned char objects have calculations "
<< "free of rounding errors: "
<< numeric_limits<unsigned char>::is_exact
<< endl;
}
Whether float objects have calculations free of rounding errors: 0
Whether double objects have calculations free of rounding errors: 0
Whether long int objects have calculations free of rounding errors: 1
Whether unsigned char objects have calculations free of rounding errors: 1
Tests if a type conforms to IEC 559 standards.
static constexpr bool is_iec559 = false;
true
if the type conforms to the IEC 559 standards; false
if not.
The IEC 559 is an international standard for representing floating-point values and is also known as IEEE 754 in the USA.
// numeric_limits_is_iec559.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects conform to iec559 standards: "
<< numeric_limits<float>::is_iec559
<< endl;
cout << "Whether double objects conform to iec559 standards: "
<< numeric_limits<double>::is_iec559
<< endl;
cout << "Whether int objects conform to iec559 standards: "
<< numeric_limits<int>::is_iec559
<< endl;
cout << "Whether unsigned char objects conform to iec559 standards: "
<< numeric_limits<unsigned char>::is_iec559
<< endl;
}
Whether float objects conform to iec559 standards: 1
Whether double objects conform to iec559 standards: 1
Whether int objects conform to iec559 standards: 0
Whether unsigned char objects conform to iec559 standards: 0
Tests if a type has an integer representation.
static constexpr bool is_integer = false;
true
if the type has an integer representation; false
if not.
All predefined integer types have an integer representation.
// numeric_limits_is_integer.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have an integral representation: "
<< numeric_limits<float>::is_integer
<< endl;
cout << "Whether double objects have an integral representation: "
<< numeric_limits<double>::is_integer
<< endl;
cout << "Whether int objects have an integral representation: "
<< numeric_limits<int>::is_integer
<< endl;
cout << "Whether unsigned char objects have an integral representation: "
<< numeric_limits<unsigned char>::is_integer
<< endl;
}
Whether float objects have an integral representation: 0
Whether double objects have an integral representation: 0
Whether int objects have an integral representation: 1
Whether unsigned char objects have an integral representation: 1
Tests if a type has a modulo representation.
static constexpr bool is_modulo = false;
true
if the type has a modulo representation; false
if not.
A modulo representation is a representation where all results are reduced modulo some value. All predefined unsigned integer types have a modulo representation.
// numeric_limits_is_modulo.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have a modulo representation: "
<< numeric_limits<float>::is_modulo
<< endl;
cout << "Whether double objects have a modulo representation: "
<< numeric_limits<double>::is_modulo
<< endl;
cout << "Whether signed char objects have a modulo representation: "
<< numeric_limits<signed char>::is_modulo
<< endl;
cout << "Whether unsigned char objects have a modulo representation: "
<< numeric_limits<unsigned char>::is_modulo
<< endl;
}
Whether float objects have a modulo representation: 0
Whether double objects have a modulo representation: 0
Whether signed char objects have a modulo representation: 1
Whether unsigned char objects have a modulo representation: 1
Tests if a type has a signed representation.
static constexpr bool is_signed = false;
true
if the type has a signed representation; false
if not.
The member stores true for a type that has a signed representation, which is the case for all predefined floating-point and signed integer types.
// numeric_limits_is_signaled.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have a signed representation: "
<< numeric_limits<float>::is_signed
<< endl;
cout << "Whether double objects have a signed representation: "
<< numeric_limits<double>::is_signed
<< endl;
cout << "Whether signed char objects have a signed representation: "
<< numeric_limits<signed char>::is_signed
<< endl;
cout << "Whether unsigned char objects have a signed representation: "
<< numeric_limits<unsigned char>::is_signed
<< endl;
}
Whether float objects have a signed representation: 1
Whether double objects have a signed representation: 1
Whether signed char objects have a signed representation: 1
Whether unsigned char objects have a signed representation: 0
Tests if a type has an explicit specialization defined in the class template numeric_limits
.
static constexpr bool is_specialized = false;
true
if the type has an explicit specialization defined in the class template; false
if not.
All scalar types other than pointers have an explicit specialization defined for class template numeric_limits
.
// numeric_limits_is_specialized.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have an explicit "
<< "specialization in the class: "
<< numeric_limits<float>::is_specialized
<< endl;
cout << "Whether float* objects have an explicit "
<< "specialization in the class: "
<< numeric_limits<float*>::is_specialized
<< endl;
cout << "Whether int objects have an explicit "
<< "specialization in the class: "
<< numeric_limits<int>::is_specialized
<< endl;
cout << "Whether int* objects have an explicit "
<< "specialization in the class: "
<< numeric_limits<int*>::is_specialized
<< endl;
}
Whether float objects have an explicit specialization in the class: 1
Whether float* objects have an explicit specialization in the class: 0
Whether int objects have an explicit specialization in the class: 1
Whether int* objects have an explicit specialization in the class: 0
Returns the most negative finite value.
static constexpr Type lowest() throw();
Returns the most negative finite value.
Returns the most negative finite value for the type (which is typically min()
for integer types and -max()
for floating-point types). The return value is meaningful if is_bounded
is true
.
Returns the maximum finite value for a type.
static constexpr Type max() throw();
The maximum finite value for a type.
The maximum finite value is INT_MAX for type int
and FLT_MAX for type float
. The return value is meaningful if is_bounded is true
.
// numeric_limits_max.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "The maximum value for type float is: "
<< numeric_limits<float>::max( )
<< endl;
cout << "The maximum value for type double is: "
<< numeric_limits<double>::max( )
<< endl;
cout << "The maximum value for type int is: "
<< numeric_limits<int>::max( )
<< endl;
cout << "The maximum value for type short int is: "
<< numeric_limits<short int>::max( )
<< endl;
}
Returns the number of decimal digits required to make sure that two distinct values of the type have distinct decimal representations.
static constexpr int max_digits10 = 0;
Returns the number of decimal digits that are required to make sure that two distinct values of the type have distinct decimal representations.
The member stores the number of decimal digits required to make sure that two distinct values of the type have distinct decimal representations.
Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.
static constexpr int max_exponent = 0;
The maximum integral radix-based exponent representable by the type.
The member function return is meaningful only for floating-point types. The max_exponent
is the value FLT_MAX_EXP for type float
.
// numeric_limits_max_exponent.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The maximum radix-based exponent for type float is: "
<< numeric_limits<float>::max_exponent
<< endl;
cout << "The maximum radix-based exponent for type double is: "
<< numeric_limits<double>::max_exponent
<< endl;
cout << "The maximum radix-based exponent for type long double is: "
<< numeric_limits<long double>::max_exponent
<< endl;
}
The maximum radix-based exponent for type float is: 128
The maximum radix-based exponent for type double is: 1024
The maximum radix-based exponent for type long double is: 1024
Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.
static constexpr int max_exponent10 = 0;
The maximum integral base 10 exponent representable by the type.
The member function return is meaningful only for floating-point types. The max_exponent
is the value FLT_MAX_10 for type float
.
// numeric_limits_max_exponent10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The maximum base 10 exponent for type float is: "
<< numeric_limits<float>::max_exponent10
<< endl;
cout << "The maximum base 10 exponent for type double is: "
<< numeric_limits<double>::max_exponent10
<< endl;
cout << "The maximum base 10 exponent for type long double is: "
<< numeric_limits<long double>::max_exponent10
<< endl;
}
The maximum base 10 exponent for type float is: 38
The maximum base 10 exponent for type double is: 308
The maximum base 10 exponent for type long double is: 308
Returns the minimum normalized value for a type.
static constexpr Type min() throw();
The minimum normalized value for the type.
The minimum normalized value is INT_MIN for type int
and FLT_MIN for type float
. The return value is meaningful if is_bounded is true
or if is_signed is false
.
// numeric_limits_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The minimum value for type float is: "
<< numeric_limits<float>::min( )
<< endl;
cout << "The minimum value for type double is: "
<< numeric_limits<double>::min( )
<< endl;
cout << "The minimum value for type int is: "
<< numeric_limits<int>::min( )
<< endl;
cout << "The minimum value for type short int is: "
<< numeric_limits<short int>::min( )
<< endl;
}
The minimum value for type float is: 1.17549e-038
The minimum value for type double is: 2.22507e-308
The minimum value for type int is: -2147483648
The minimum value for type short int is: -32768
Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.
static constexpr int min_exponent = 0;
The minimum integral radix-based exponent representable by the type.
The member function is meaningful only for floating-point types. The min_exponent
is the value FLT_MIN_EXP for type float
.
// numeric_limits_min_exponent.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The minimum radix-based exponent for type float is: "
<< numeric_limits<float>::min_exponent
<< endl;
cout << "The minimum radix-based exponent for type double is: "
<< numeric_limits<double>::min_exponent
<< endl;
cout << "The minimum radix-based exponent for type long double is: "
<< numeric_limits<long double>::min_exponent
<< endl;
}
The minimum radix-based exponent for type float is: -125
The minimum radix-based exponent for type double is: -1021
The minimum radix-based exponent for type long double is: -1021
Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.
static constexpr int min_exponent10 = 0;
The minimum integral base 10 exponent representable by the type.
The member function is meaningful only for floating-point types. The min_exponent10
is the value FLT_MIN_10_EXP for type float
.
// numeric_limits_min_exponent10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The minimum base 10 exponent for type float is: "
<< numeric_limits<float>::min_exponent10
<< endl;
cout << "The minimum base 10 exponent for type double is: "
<< numeric_limits<double>::min_exponent10
<< endl;
cout << "The minimum base 10 exponent for type long double is: "
<< numeric_limits<long double>::min_exponent10
<< endl;
}
The minimum base 10 exponent for type float is: -37
The minimum base 10 exponent for type double is: -307
The minimum base 10 exponent for type long double is: -307
Returns the representation of a quiet not a number (NAN) for the type.
static constexpr Type quiet_NaN() throw();
The representation of a quiet NAN for the type.
The return value is meaningful only if has_quiet_NaN is true
.
// numeric_limits_quiet_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The quiet NaN for type float is: "
<< numeric_limits<float>::quiet_NaN( )
<< endl;
cout << "The quiet NaN for type int is: "
<< numeric_limits<int>::quiet_NaN( )
<< endl;
cout << "The quiet NaN for type long double is: "
<< numeric_limits<long double>::quiet_NaN( )
<< endl;
}
The quiet NaN for type float is: 1.#QNAN
The quiet NaN for type int is: 0
The quiet NaN for type long double is: 1.#QNAN
Returns the integral base, referred to as radix, used for the representation of a type.
static constexpr int radix = 0;
The integral base for the representation of the type.
The base is 2 for the predefined integer types, and the base to which the exponent is raised, or FLT_RADIX, for the predefined floating-point types.
// numeric_limits_radix.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The base for type float is: "
<< numeric_limits<float>::radix
<< endl;
cout << "The base for type int is: "
<< numeric_limits<int>::radix
<< endl;
cout << "The base for type long double is: "
<< numeric_limits<long double>::radix
<< endl;
}
The base for type float is: 2
The base for type int is: 2
The base for type long double is: 2
Returns the maximum rounding error for the type.
static constexpr Type round_error() throw();
The maximum rounding error for the type.
// numeric_limits_round_error.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The maximum rounding error for type float is: "
<< numeric_limits<float>::round_error( )
<< endl;
cout << "The maximum rounding error for type int is: "
<< numeric_limits<int>::round_error( )
<< endl;
cout << "The maximum rounding error for type long double is: "
<< numeric_limits<long double>::round_error( )
<< endl;
}
The maximum rounding error for type float is: 0.5
The maximum rounding error for type int is: 0
The maximum rounding error for type long double is: 0.5
Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.
static constexpr float_round_style round_style = round_toward_zero;
A value from the float_round_style
enumeration that describes the rounding style.
The member stores a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.
The round style is hard coded in this implementation, so even if the program starts up with a different rounding mode, that value will not change.
// numeric_limits_round_style.cpp
// compile with: /EHsc
#include <iostream>
#include <float.h>
#include <limits>
using namespace std;
int main( )
{
cout << "The rounding style for a double type is: "
<< numeric_limits<double>::round_style << endl;
_controlfp_s(NULL,_RC_DOWN,_MCW_RC );
cout << "The rounding style for a double type is now: "
<< numeric_limits<double>::round_style << endl;
cout << "The rounding style for an int type is: "
<< numeric_limits<int>::round_style << endl;
}
The rounding style for a double type is: 1
The rounding style for a double type is now: 1
The rounding style for an int type is: 0
Returns the representation of a signaling not a number (NAN) for the type.
static constexpr Type signaling_NaN() throw();
The representation of a signaling NAN for the type.
The return value is meaningful only if has_signaling_NaN is true
.
// numeric_limits_signaling_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "The signaling NaN for type float is: "
<< numeric_limits<float>::signaling_NaN( )
<< endl;
cout << "The signaling NaN for type int is: "
<< numeric_limits<int>::signaling_NaN( )
<< endl;
cout << "The signaling NaN for type long double is: "
<< numeric_limits<long double>::signaling_NaN( )
<< endl;
}
Tests whether a type can determine that a value is too small to represent as a normalized value before rounding it.
static constexpr bool tinyness_before = false;
true
if the type can detect tiny values before rounding; false
if it cannot.
Types that can detect tinyness were included as an option with IEC 559 floating-point representations and its implementation can affect some results.
// numeric_limits_tinyness_before.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float types can detect tinyness before rounding: "
<< numeric_limits<float>::tinyness_before
<< endl;
cout << "Whether double types can detect tinyness before rounding: "
<< numeric_limits<double>::tinyness_before
<< endl;
cout << "Whether long int types can detect tinyness before rounding: "
<< numeric_limits<long int>::tinyness_before
<< endl;
cout << "Whether unsigned char types can detect tinyness before rounding: "
<< numeric_limits<unsigned char>::tinyness_before
<< endl;
}
Whether float types can detect tinyness before rounding: 1
Whether double types can detect tinyness before rounding: 1
Whether long int types can detect tinyness before rounding: 0
Whether unsigned char types can detect tinyness before rounding: 0
Tests whether trapping that reports on arithmetic exceptions is implemented for a type.
static constexpr bool traps = false;
true
if trapping is implemented for the type; false
if it is not.
// numeric_limits_traps.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float types have implemented trapping: "
<< numeric_limits<float>::traps
<< endl;
cout << "Whether double types have implemented trapping: "
<< numeric_limits<double>::traps
<< endl;
cout << "Whether long int types have implemented trapping: "
<< numeric_limits<long int>::traps
<< endl;
cout << "Whether unsigned char types have implemented trapping: "
<< numeric_limits<unsigned char>::traps
<< endl;
}
Whether float types have implemented trapping: 1
Whether double types have implemented trapping: 1
Whether long int types have implemented trapping: 0
Whether unsigned char types have implemented trapping: 0