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
- with explicit type conversion (
double x=1.5; int y = int (x))
- the fractional part of the real number is cut off (y = 1).
- function
floor(x)
- returns the value of x
rounded down to its nearest integer x
.
- 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) == 1 , floor(-1.5) == -2 |
ceil(x) |
returns the value of \(x\) rounded up to its nearest integer ceil(1.5) == 2 , ceil(-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 x: ex . |
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
|