Real numbers

float f = 6.5;
double r = 5.0;

The value 5.0 is a number represented as a decimal fraction (has an integer and fractional part). In computer science, such numbers are called float numbers.
A float is a data type composed of a number that is not an integer, because it includes a fraction represented in decimal format. The integer and fractional parts are separated by a dot, not a comma, as in mathematics.
Even if the fractional part of the number is zero, as in the variable r in the example, the translator will still create a real variable in memory. The point, as it were, is a signal for the translator that it is necessary to create a real variable.

Very large and very small numbers are written using a "floating point" (in the so-called scientific format).

In a scientific format, the number is represented in the form of a mantissa (a significant part of the number) and order. When recording, the mantissa and the order are separated from each other by the letter e (denotes 10 to some extent).
For example, you can save the value of the electron charge (\(1,60217662 \times 10^{-19}\)) in a variable by writing in the following form
float El = 1.60217662e-19 # for a positive order, the + sign can be omitted
Almost all real numbers cannot be stored in computer memory with perfect accuracy, since a limited number of bits are allocated for their storage. Therefore, in calculations with real numbers, errors associated with inaccuracy of the representation are accumulated. Moreover, the less space is allocated, the greater this error will be. In order to reduce the error in C ++, they use the double type, which stores a real number with double precision in memory (it takes eight bytes in memory, while the type float takes 4 bytes).

To get more accurate calculations, use the double type in the program.
 

Input of real numbers

You can enter several real variables from the input stream and write them into variables in the standard way:
double x, y;
cin >> x >> y;
The first number falls into the x variable, the second into y variable.

 

Output of real numbers

By default, cout in C++ outputs 6 decimal places for real numbers. This means that unless otherwise specified, a real number will be outputed to 6 digits. If the number has less digits after the decimal point, the missing digits will be replaced by zeros. If the number contains more digits after the decimal point, it will be rounded to 6.

If you do not explicitly specify the output precision using the setprecision function or using the fixed flag, the number will be output with that precision. For example, 81220.545 is rounded to 81220.5, since the last sign is "5" in the fifth decimal place (the 6th digit).

The output format can be customized. To do this, use the additional library iomanip - manipulators that control the output.
The fixed manipulator is used for fixed-point output, and the scientific manipulator is used for scientific output. Then you need to define the number of digits in the fractional part with the setprecision() manipulator. With the setw() manipulator you can set the total number of positions to output a number.

 
Example
#include<iomanip>
...
double x = 1.0/6;
// set to output 9 digits in the fractional part
cout << fixed << setprecision (9);  
cout << setw(12) << x << endl;                            
Output
_0.166666672                  

All operators can be written in one line:
cout << fixed << setprecision(9) << setw(12) << x << endl;

To be sure that the output is accurate, use either a fixed format, with the default number of decimal places

cout << fixed << x << endl;

Operations with real numbers. Standart library cmath

When working with real numbers, you can use the directive сmath, which contains a large number of built-in functions. When solving problems, one often has to round real numbers to the nearest integer values. There are two functions for this.
 
REMEMBER
  1. with explicit type conversion ( double x=1.5; int y = int (x))  -  the fractional part of the real number is cut off (y = 1). 
  2. function floor(x) - returns the value of x rounded down to its nearest integer x
  3. function ceil(x) -  returns the value of x  rounded up to its nearest integer.

Here are the most useful functions contained in the directive cmath.
function description
Rounding
round(x)
C++ 11
returns the integral value that is nearest to x, with halfway cases rounded away from zero.
trunc(x)
C++ 11
rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x
floor(x) returns the value of \(x\) rounded down to its nearest integer   floor(1.5) == 1floor(-1.5) == -2
ceil(x) returns the value of \(x\)  rounded up to its nearest integer ceil(1.5) == 2ceil(-1.5) == -1
abs(x) returns the absolute value of x
fabs(x) returns the absolute value of a floating x
Roots, logarithms
sqrt(x) returns the square root of x.  y = sqrt(x). The function takes a single non-negative argument. If negative argument is passed to sqrt() function, domain error occurs.
pow(x, y) returns the value of x to the power of y. \(x^y\)
log(x) returns the natural logarithm of a number.
exp(x) returns the base-e exponential function of x, which is e raised to the power xex.
Trigonometry
sin(x) returns the sine of an angle of x radians.
cos(x) returns the cosine of an angle of x radians.
tan(x) returns the tangent of an angle of x radians
asin(x) returns the principal value of the arc sine of x, expressed in radians. In trigonometrics, arc sine is the inverse operation of sine.
acos(x) returns the principal value of the arc cosine of x, expressed in radians. In trigonometrics, arc cosine is the inverse operation of cosine.
atan(x) returns the principal value of the arc tangent of x, expressed in radians. In trigonometrics, arc tangent is the inverse operation of tangent
atan2(y, x) returns the principal value of the arc tangent of y/x, expressed in radians. To compute the value, the function takes into account the sign of both arguments in order to determine the quadrant.
M_PI A constant that stores the value of pi. To use it, you must at the beginning of the program write the line
#define _USE_MATH_DEFINES