Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

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:

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:

Constants provided by

  1. Floating-point range
  1. Floating-point precision
  1. Smallest negative exponent
  1. Largest positive exponent
  1. Machine epsilon

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 // Include math functions

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

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++.