The <cfloat> module in the C++ standard library.

<cfloat>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 such as precision and range of floating-point representation, mainly from the C standard library's<float.h>Header file.

Floating-point basics

In C++, a floating-point number is 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 usefloatordoubleto define floating-point variables. For example:

float f = 3.14f; // 使用 f 后缀表示浮点数字面量
double d = 2.718;

Floating-point operations in the standard library

Although the C++ standard library does not have a dedicated "cfloat" module,<cmath>the header file provides many functions for floating-point operations, such as:

  • sqrtCalculate square root
  • pow: calculate power
  • sin、cos、tanCalculate trigonometric functions

<cfloat>Provided constants

1. Floating-point range

  • FLT_MIN:floatThe smallest positive number of the type.
  • FLT_MAX:floatThe largest positive number of the type.
  • DBL_MIN:doubleThe smallest positive number of the type.
  • DBL_MAX:doubleThe largest positive number of the type.
  • LDBL_MIN:long doubleThe smallest positive number of the type.
  • LDBL_MAX:long doubleThe largest positive number of the type.

2. Floating-point precision

  • FLT_DIG:floatThe number of significant digits of the type.
  • DBL_DIG:doubleThe number of significant digits of the type.
  • LDBL_DIG:long doubleThe number of significant digits of the type.

3. Minimum negative exponent

  • FLT_MIN_EXP:floatThe smallest negative exponent of the type.
  • DBL_MIN_EXP:doubleThe smallest negative exponent of the type.
  • LDBL_MIN_EXP:long doubleThe smallest negative exponent of the type.

4. Maximum positive exponent

  • FLT_MAX_EXP:floatThe largest positive exponent of the type.
  • DBL_MAX_EXP:doubleThe largest positive exponent of the type.
  • LDBL_MAX_EXP:long doubleThe largest positive exponent of the type.

5. Machine epsilon

  • FLT_EPSILON:floatthe machine epsilon of a type, representing the smallest floating-point number that can be distinguished from 1.0 and is greater than 1.0.
  • DBL_EPSILON:doubleThe machine epsilon of the type.
  • LDBL_EPSILON:long doubleThe machine epsilon of the type.

Example

The following example demonstrates how to use<cfloat>Provided constants:

Example

#include <iostream>
#include <cfloat>

int main() {
    // output the range and precision of float type
    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 type
    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 type
    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:

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<cmath>functions in the header file to operate on floating-point numbers in a C++ program:

Example

#include <iostream>
#include <cmath> // includes mathematical functions

int main() {
    double num = 9.0;
    double root = sqrt(num); // calculate square root
    double power = pow(2.0, 3.0); // calculate 2 to the 3rd power

    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:

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 precision issues may be encountered when performing floating-point arithmetic.
  • When comparing whether two floating-point numbers are equal, a small error tolerance should be used for judgment, rather than directly using==operator.

Although "cfloat" is not part of the C++ standard library, C++ provides powerful floating-point support and related mathematical function libraries. By using<cmath>header file, you can conveniently perform various floating-point operations. I hope this article can help beginners better understand floating-point operations in C++.

other extensions