Python. Module math. Number-theoretic and representation functions




Module math. Number-theoretic and representation functions


Contents


Search other websites:

1. Including the math module. Function math.ceil(). Rounding up

To use the number-theoretic and representation functions from the math module, you must first connect this module

import math

The math.ceil(x) function returns a real number y that satisfies the following conditions:

  • the value of y is equal to the nearest integer x;
  • the value of y is greater than or equal to x.

The argument x can only be an integer or a real number. If you try to specify a complex number as the argument x, then the interpreter will report an error.

Example.

import math

# for a real argument, positive numbers
x = 2.65
y = math.ceil(x) # y = 3.0

x = 3.0 # y = 3.0
y = math.ceil(x)

x = 2.000001
y = math.ceil(x) # y = 3.0

# real argument, negative numbers
x = -2.0001 # y = -2.0
y = math.ceil(x)

# an integer argument
x = 13
y = math.ceil(x) # y = 13.0

 

2. Function math.copysign(x,y). Assigning an argument based on the sign of a number

The math.copysign(x, y) function returns a result of a real type that is equal to the value of the argument x but with the sign of the argument y. Arguments x, y can be only integer or real type.

Example.

import math

# Function math.copysign(x,y)

x = 3.64
y = -5.44
z = math.copysign(x, y) # z = -3.64 => value - x, sign - y

x = -2.85
y = 0.00001
z = math.copysign(x, y) # z = 2.85

# case when y = -0.0
x = 2.85
y = -0.0 # real negative number
z = math.copysign(x, y) # z = -2.85

# case when y = 0.0
x = 3.18
y = 0.0
z = math.copysign(x, y) # z = 3.18

 

3. Function math.fabs(x). The absolute value of a number

The math.fabs(x) function returns the absolute value of the argument x. The function returns a real result. The argument x can be either real or integer type. Argument x cannot be of complex type.

Example.

# Function math.fabs(x) - a module of x
import math

x = -15.55
y = math.fabs(x) # y = 15.55

x = 0.01
y = math.fabs(x) # y = 0.01

# for integer argument x
x = -15
y = math.fabs(x) # y = 15.0 - real result


 

4. Function factorial(x). The calculation of factorial

The factorial(x) function returns the factorial of the integer argument x. The result of the function is an integer type. It is forbidden to use the argument x of a real type, unless the fractional part is 0 (for example, 5.0).

Example.

# функция math.factorial(x)
import math

x = 5
y = math.factorial(x) # y = 120

x = 5.0 # allowed since the fractional part is 0
y = math.factorial(x) # y = 120

#x = 5.5 - Forbidden, error
#y = math.factorial(x) # y = 120

 

5. Function floor(x). Rounding down

The math.floor(x) function returns a real number y that satisfies the following conditions:

  • the value of y is equal to the nearest integer x;
  • the value of y is less than or equal to x.

The argument x can only be an integer or a real number. If try to specify a complex number as the argument x, then the interpreter will report an error.

Example.

# Function math.floor(x)
import math

x = 5.2
y = math.floor(x) # y = 5.0

x = 5.99
y = math.floor(x) # y = 5.0

x = -5.2
y = math.floor(x) # y = -6.0

x = -6.0
y = math.floor(x) # y = -6.0

 

6. Function math.fmod(x, y). Remainder of division

The fmod(x, y) function returns the remainder of the division of two numbers. Each of the arguments x, y can be an integer or a real type. The function is an analogue of the % operator.

The difference between the fmod() function and the % operator is as follows. For integer arguments, the % operator returns the result of an integer type. At the same time, the fmod(x, y) function for arguments of an integer type returns a result of a real type.

The operator % can also be used for arguments of a real type. However, in some cases, the x%y operator may not be entirely computable for real arguments. Therefore, this operator is recommended for integer arguments. And for real arguments it is better to use the fmod(x, y) function.

Example.

# Function math.fmod(x)
import math

# for real arguments
x = 5.2
y = 0.4
y = math.fmod(x, y) # y = 0.3999999999999999

# for integer arguments
x = 15
y = 4
z = math.fmod(x, y) # z = 3.0 - real result
v = x % y # v = 3 - integer result

 

7. Function math.frexp(x). Calculation of the normalized mantissa and the unshifted order of the number

This function returns the normalized mantissa m and the unshifted order e of the real number x as a pair (tuple). Computational values satisfy the ratio:

Example.

# Function math.frexpr(x)
import math

x = 2.8
y = math.frexp(x) # y = (0.7, 2)

# zero value x
x = 0.0
y = math.frexp(x) # y = (0.0, 0)

 

8. Function math.fsum(). Sum of numbers in an iterated object

The math.fsum(x) function calculates the exact sum of floating-point numbers in an iterated object x. The function prevents loss of accuracy by tracking some intermediate partial sums.

Example.

# Function math.fsum(x)
import math

y = math.fsum([0.02, 0.02, 0.02, 0.02, 0.02]) # y = 0.1

 

9. Function math.gcd(a, b). The greatest common divisor of two numbers

The function math.gcd(a, b) is implemented in versions of Python 3.5 and higher. This function returns the largest common divisor of arguments a, b. If a and b are nonzero, then the result of the function is the largest number by which a and b are divisible whole.

Example.

# Function gcd(a,b)
import math

a = 15
b = 5
c = math.gcd(a,b) # c = 5

a = 60
b = 40
c = math.gcd(a,b) # c = 20

a = -60
b = 24
c = math.gcd(a,b) # c = 12

 

10. Function math.isclose()

This function is used in versions of Python 3.5 and higher. The function isclose(a, b, rel_tol, abs_tol) returns True if the numbers a, b are close to one to one in value. Otherwise, the function returns False.

The closeness of two numbers is calculated based on:

  • relative tolerance rel_tol;
  • absolute tolerance abs_tol.

Relative and absolute deviations can be specified explicitly. If the relative and absolute deviations are not specified, then the rel_tol and abs_tol arguments take the following values:

  • rel_tol = 1e-09;
  • abs_tol = 0.0.

If you need to set a relative error of 3%, then rel_tol = 0.03. The abs_tol value defines the maximum tolerance between the numbers: abs_tol = | a-b |. The abs_tol value can always be at least zero.

Example.

# Function isclose(a,b,rel_tol,abs_tol)
import math

a = 5.000001
b = 5.0
c = math.isclose(a,b) # c = False

a = 5.0000000000001
b = 5.0
c = math.isclose(a,b) # c = True

 

11. Function isfinite(x). Definition of finite

The isfinite(x) function was introduced in Python 3.2 and later. This function returns True if both conditions are true:

  • the value of x is finite;
  • the value of x is not equal to NaN.

Otherwise, the function returns False. A value of x = 0.0 is considered limited (finite).

Example.

# Function isfinite(x)
import math

x = 0.0
f = math.isfinite(x) # f = True

 

12. Function isinf(x). Definition of infinity

The isinf(x) function returns True if x is bounded infinity or negative infinity.

Example.

# Function isinf(x)
import math

x = 0.0
f = math.isinf(x) # f = False

 

13. Function isnan(x). Determining the value of NaN (not a number)

The isnan(x) function returns True if x is equal to NaN (not a number). Otherwise, the function returns False. The NaN value is a special value (or state) that takes an undefined result. NaN occurs due to some mathematical operations and does not contain any other value. The NaN value can occur due to operations that give an indefinite result, for example, dividing zero by zero, multiplying zero by infinity, etc.

Example.

# Function isnan(x)
import math

x = 0.0/5
f = math.isnan(x) # f = False

 

14. Function math.ldexp(x, i). Inverse function to math.frexp(x)

The ldexp(x, i) function returns the value x**(2*i). The function is inverse to the math.frexp(x) function.

Example.

# Function ldexp(x, i)
import math

x = 3.0
i = 2
y = math.ldexp(x,i) # y = 12.0

 

15. Function math.modf(x). Isolation of fractional and integer parts of a real number

The math.modf(x) function returns a pair of real numbers, which includes:

  • fractional part of the number x (the value of the number after the decimal point);
  • the integer part of x.

Example.

# Function modf(x)
import math

x = 3.23
y = math.modf(x) # y = (0.22999999999999998, 3.0)

x = -81.02
y = math.modf(x) # y = (-0.01999999999999602, -81.0)

 

16. Function math.remainder(x, y). The remainder x relative to y

The math.remainder(x, y) function returns a real number, which is the remainder of x relative to y. The arguments x and y are assumed to be finity. Also, the value of y may be nonzero.

The result of the function is calculated by the formula

x – n · y

where n is the nearest integer to the exact value of the factor x/y.

Example.

# Function remainder(x,y)
import math

x = 3.2
y = 3.2
z = math.remainder(x, y) # z = 0.0

x = 3.7
y = 4.2
z = math.remainder(x, y) # z = -0.5

x = 4.7
y = 3.2
z = math.remainder(x, y) # z = 1.5

x = 15.0
y = 3.3
z = math.remainder(x, y) # z = -1.4999999999999991

x = -6.0
y = 2.1
z = math.remainder(x, y) # z = 0.30000000000000027

 

17. Function math.trunc(x). Determining the integer part of a number

The math.trunc(x) function returns an integer value that is equal to the integer part of x.

Example.

# Function trunc(x)
import math

x = 2.8
y = math.trunc(x) # y = 2

x = -3.001
y = math.trunc(x) # y = -3

x = 2.0000001
y = math.trunc(x) # y = 2

 


Related topics