Python basis (XIX): Mathematics related modules

1 Introduction

Let's look at the math-related Python modules, as follows:

Module description
math Provides access to mathematical functions for C standard defined (not applicable for plural)
cmath It provides some complex mathematical functions on
decimal Provide support for the quick and correct rounding decimal floating-point operations
fractions Provide support for fractional arithmetic
random Pseudo-random number generator various distributions
statistics Providing statistics for calculating the mathematical function of digital data

This article specific about relatively common module: math, decimal, and random.

2. math module

First look at the math module included in the content, as follows:

>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

Next, look at the specific functions and constants used for the module.

ceil (x)
returns the upper limit of x, i.e. the smallest integer greater than or equal to x. Facie example:

import math

x = -1.5
print(math.ceil(x))

floor (x)
returns x rounded down, the largest integer less than or equal to x. Facie example:

import math

x = -1.5
print(math.floor(x))

FABS (x)
returns the absolute value of x. Facie example:

import math

x = -1.5
print(math.fabs(x))

fmod (x, y)
Returns the remainder of x / y is, as a float. Facie example:

import math

x = 3
y = 2
print(math.fmod(x, y))

factorial (x)
returns the factorial of x, ValueError will be raised if x is not an integer or a negative number. Facie example:

import math

x = 3
print(math.factorial(3))

pow (x, y)
returns the y-th power of x. Facie example:

import math

x = 3
y = 2
print(math.pow(x, y))

fsum (iterable)
returns an iterator in all elements. Facie example:

import math

print(math.fsum((1, 2, 3, 4, 5)))

gcd (x, y)
returns the greatest common divisor of the integers x and y. Facie example:

import math

x = 9
y = 6
print(math.gcd(x, y))

sqrt (x)
returns the square root of x. Facie example:

import math

x = 9
print(math.sqrt(x))

the trunc (x)
returns the integer part of x. Facie example:

import math

x = 1.1415926
print(math.trunc(x))

exp (x)
returns the power of x e. Facie example:

import math

x = 2
print(math.exp(2))

log (x [, base])
Returns x logarithm, base-default as e. Facie example:

import math

x = 10
y = 10
# 不指定底数
print(math.log(x))
# 指定底数
print(math.log(x, y))

constant

import math

# 常量 e
print(math.e)
# 常量 π
print(math.pi)

Tan (x)
returns the tangent of x radians. Facie example:

import math

print(math.tan(math.pi / 3))

atan (x)
returns the arctangent of x. Facie example:

import math

print(math.atan(1))

sin (x)
returns the sine of x radians. Facie example:

import math

print(math.sin(math.pi / 3))

asin (x)
Returns the arc sine value of x. Facie example:

import math

print(math.asin(1))

cos (x)
returns the cosine of x radians. Facie example:

import math

print(math.cos(math.pi / 3))

ACOS (x)
Returns the arc cosine of x. Facie example:

import math

print(math.acos(1))

3. decimal module

decimal module is correctly rounded decimal floating point arithmetic offers support, compared to the built-in floating-point types float, it can be more precise control precision, able to support high precision finance.

decimal work in a separate context, may be used getContext () to view the current context, as follows:

>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

We can see from the results above prec=28, the accuracy of which is the default, we can use getcontext().prec = xxxto re-set accuracy. Followed by a look at a specific example.

Basic operations

import decimal

d1 = decimal.Decimal(1.1)
d2 = decimal.Decimal(9.9)
print(d1 + d2)
print(d1 - d2)
print(d1 * d2)
print(d1 / d2)

Results of the:

11.00000000000000044408920985
-8.800000000000000266453525910
10.89000000000000127009514017
0.1111111111111111160952773272

The above results are obtained with the default precision, accuracy and then we re-set the next look:

import decimal

decimal.getcontext().prec = 2
d1 = decimal.Decimal(1.1)
d2 = decimal.Decimal(9.9)
print(d1 + d2)
print(d1 - d2)
print(d1 * d2)
print(d1 / d2)

Results of the:

11
-8.8
11
0.11

4. random module

random module can generate a random number, we look at their usual function.

random ()
Returns [0.0, 1.0) a random floating point range. Facie example:

import random

print(random.random())

uniform (a, b)
returns the [a, b) within a range of floating-point numbers randomly. Facie example:

import random

print(random.uniform(1.1, 9.9))

randint (a, b)
returns the [a, b] a random integer within the range. Facie example:

import random

print(random.randint(1, 10))

randrange (start, stop [, step
]) return [start, stop) within a range in steps of a random integer step. Facie example:

import random

print(random.randrange(1, 10))
print(random.randrange(1, 10, 2))

choice (seq)
returns a random element from a non-empty sequence seq. Facie example:

import random

print(random.choice('123456'))
print(random.choice('abcdef'))

shuffle (x [, random])
the random sequence x upset position. Facie example:

import random

l = [1, 2, 3, 4, 5, 6]
random.shuffle(l)
print(l)

sample (population, k)
returns the list of unique length k of elements selected from a set of overall sequence or, for non-repeating random sampling. Facie example:

import random

l = [1, 2, 3, 4, 5, 6]
print(random.sample(l, 3))

Reference:
https://docs.python.org/3/library/numeric.html


Published 47 original articles · won praise 1000 · Views 170,000 +

Guess you like

Origin blog.csdn.net/ityard/article/details/104085908