The C++ standard library's module
is a header file in the C++ standard library used to define macros and constants related to floating-point numbers. These macros and constants provide information about the precision and range of floating-point representations, mainly from the C standard library's header file in your C++ program.
Floating-point basics
In C++, floating-point numbers are a data type used to represent decimals. C++ provides two basic floating-point types:
float: Single-precision floating-point, usually occupying 4 bytes.double: Double-precision floating-point, usually occupying 8 bytes.
Definition and syntax
In C++, you can use float or double to define floating-point variables. For example:
float f = 3.14f; // 使用 f 后缀表示浮点数字面量
double d = 2.718;
Floating-point operations in the standard library
Although there is no dedicated 'cfloat' module in the C++ standard library, header provides many functions for floating-point operations, for example:
sqrt: Computes the square rootpow: Computes the powersin,cos,tan: Computes trigonometric functions
Constants provided by
- Floating-point range
FLT_MIN:float: The smallest positive value of the type.FLT_MAX:float: The largest positive value of the type.DBL_MIN:double: The smallest positive value of the type.DBL_MAX:double: The largest positive value of the type.LDBL_MIN:long double: The smallest positive value of the type.LDBL_MAX:long double: The largest positive value of the type.
- Floating-point precision
FLT_DIG:float: The number of significant digits of the type.DBL_DIG:double: The number of significant digits of the type.LDBL_DIG:long double: The number of significant digits of the type.
- Smallest negative exponent
FLT_MIN_EXP:float: The smallest negative exponent of the type.DBL_MIN_EXP:double: The smallest negative exponent of the type.LDBL_MIN_EXP:long double: The smallest negative exponent of the type.
- Largest positive exponent
FLT_MAX_EXP:float: The largest positive exponent of the type.DBL_MAX_EXP:double: The largest positive exponent of the type.LDBL_MAX_EXP:long double: The largest positive exponent of the type.
- Machine epsilon
FLT_EPSILON:float: The machine epsilon of the type, representing the smallest floating-point number distinguishable from 1.0.DBL_EPSILON:double: The machine epsilon of the type.LDBL_EPSILON:long double: The machine epsilon of the type.
Example
The following example shows how to use 's provided constants:
Example
#include
#include
int
main
(
)
{
// Output the range and precision of float
std
::
cout
<<
"float:\n"
;
std
::
cout
<<
"Min: "
<<
FLT_MIN
<<
'\n'
;
std
::
cout
<<
"Max: "
<<
FLT_MAX
<<
'\n'
;
std
::
cout
<<
"Epsilon: "
<<
FLT_EPSILON
<<
'\n'
;
std
::
cout
<<
"Digits: "
<<
FLT_DIG
<<
'\n'
;
// Output the range and precision of double
std
::
cout
<<
"\ndouble:\n"
;
std
::
cout
<<
"Min: "
<<
DBL_MIN
<<
'\n'
;
std
::
cout
<<
"Max: "
<<
DBL_MAX
<<
'\n'
;
std
::
cout
<<
"Epsilon: "
<<
DBL_EPSILON
<<
'\n'
;
std
::
cout
<<
"Digits: "
<<
DBL_DIG
<<
'\n'
;
// Output the range and precision of long double
std
::
cout
<<
"\nlong double:\n"
;
std
::
cout
<<
"Min: "
<<
LDBL_MIN
<<
'\n'
;
std
::
cout
<<
"Max: "
<<
LDBL_MAX
<<
'\n'
;
std
::
cout
<<
"Epsilon: "
<<
LDBL_EPSILON
<<
'\n'
;
std
::
cout
<<
"Digits: "
<<
LDBL_DIG
<<
'\n'
;
return
0
;
}
Output result:
float:
Min: 1.17549e-38
Max: 3.40282e+38
Epsilon: 1.19209e-07
Digits: 6
double:
Min: 2.22507e-308
Max: 1.79769e+308
Epsilon: 2.22045e-16
Digits: 15
long double:
Min: 2.22507e-308
Max: 1.79769e+308
Epsilon: 2.22045e-16
Digits: 15
Below is an example using header's functions to operate on floating-point numbers:
Example
#include
#include
int
main
(
)
{
double
num
=
9.0
;
double
root
=
sqrt
(
num
)
;
// Compute square root
double
power
=
pow
(
2.0
,
3.0
)
;
// Compute 2 to the power of 3
std
::
cout
<<
"The square root of "
<<
num
<<
" is "
<<
root
<<
std
::
endl
;
std
::
cout
<<
"The power of 2 to the 3 is "
<<
power
<<
std
::
endl
;
return
0
;
}
Output result:
The square root of 9 is 3
The power of 2 to the 3 is 8
Notes
- The precision of floating-point numbers is limited, so you may encounter precision issues when performing floating-point arithmetic.
- When comparing whether two floating-point numbers are equal, you should use a small error tolerance to judge, rather than directly using
==operator.
Although 'cfloat' is not part of the C++ standard library, C++ provides powerful floating-point support and related math function libraries. By using header, you can conveniently perform various floating-point operations. I hope this article helps beginners better understand floating-point operations in C++.