When working with real numbers, you can use the familiar math module, 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 function int (x)
- discards the fractional part of the real number x
2 function round (x)
- rounds the real number x to the nearest integer (if the fractional part of the number is 0.5, then the number is rounded to the nearest even number)
Приведем наиболее полезные функции, содержащиеся в модуле math
.
Функция |
Описание |
Округление |
int(x) |
Rounds a number to zero. This is a standard function, you do not need to connect the math module to use it. |
round(x) |
Rounds a number to the nearest integer. If the fractional part of a number is 0.5, then the number is rounded to the nearest even number. |
round(x, n) |
Rounds the number x to n characters after the period. This is a standard function, you do not need to connect the math module to use it. |
floor(x) |
Rounds the number down: floor(1.5) == 1 , floor(-1.5) == -2 |
ceil(x) |
Rounds the number up: ceil(1.5) == 2 , ceil(-1.5) == -1 |
abs(x) |
Module (absolute value). This is a standard feature. |
Корни, логарифмы |
sqrt(x) |
Square root. Using: math.sqrt(x) .
Example:
print(math.sqrt(4))
output: 2.0 |
log(x) |
The natural logarithm. When called as math.log(x, b) returns base b logarithm. |
e |
The base of natural logarithms e = 2,71828... |
Тригонометрия |
sin(x) |
The sine of the angle given in radians |
cos(x) |
The cosine of the angle given in radians |
tan(x) |
Radius tangent |
asin(x) |
Arcsine, returns a value in radians |
acos(x) |
Arccosine, returns the value in radians |
atan(x) |
Arc tangent, returns the value in radians |
atan2(y, x) |
Polar angle (in radians) of the point with coordinates (x, y). |
degrees(x) |
Converts an angle specified in radians to degrees. |
radians(x) |
Converts an angle specified in degrees to radians. |
pi |
Constant π = 3.1415... |