Mathematical module based on Python

Table of contents

1. math module

1.1 Mathematical constants

1.2 Common functions

2. Decimal module

2.1 When to use decimal

2.2 Using decimals


1. math module

The math library is a built-in math function provided by Python. Because the complex number type is often used in scientific calculations, the general calculation is not commonly used. The math library does not support complex number types, only integers and floating point numbers

1.1 Mathematical constants

math.pi pi

math.e natural constant e

math.inf is positive infinity, negative infinity is -math.inf

math.nan non-floating point number mark, NaN

1.2 Common functions

math.cell(floating point number) # round up

math.floor(floating point number) # round down

round(float) # rounded

math.fabs(value) # Absolute value returns floating point number

abs(number) # Absolute value returns integer, floating point number

math.fmod(x,y) # The remainder of x/y; return value: floating point number

math.pow(base, power) # Calculate the Nth power of a value, return value: floating point type

math.sqrt(value) # The square root returns a floating point number

fsum(sequence) # return all elements in the sequence and return value: floating point number

sum(sequence) # Add and sum the values ​​of a sequence Return value: value type (varies according to the value type in the sequence)

math.modf(value) # Split a floating-point number into a tuple of decimal and integer parts Return value: tuple

math.trunc(value) # Return the integer part of the floating point number, return value: integer

math.copysign(value 1, value 2) # Copy the positive and negative values ​​of the second number to the first number, return value: floating point number

math.factorial(x) # returns the factorial of x, if x is not an integer or negative, ValueError: return value: integer

math.gcd(x,y) # returns the greatest common divisor of integers x and y, return value: integer

print(math.e)
print(math.pi)
print(math.inf)
print(math.nan)
print(math.ceil(12.13))
print(math.floor(12.13))
print(round(13.14))
print(math.fabs(-1))
print(math.fabs(2))
print(abs(-1))
print(math.fmod(6, 2))
print(math.pow(2, 3))
print(math.sqrt(8))
print(math.fsum((1, 2, 3, 4, 5, 7, 1)))
print(sum(range(101)))
print(math.modf(12.34))
print(math.trunc(11.2))
print(math.copysign(1, -1))
print(math.factorial(4))
print(math.gcd(2, 7))

2. Decimal module

The decimal module provides a Decimal data type for floating-point calculations. Compared with the built-in binary floating-point implementation float, this type is helpful for financial applications and other occasions that require precise decimal representation, control precision, and control rounding to fit the law or Specify the requirements to ensure the accuracy of decimal digits, or the scenario where the user wants the calculation result to be consistent with the manual calculation. Decimal reproduces manual mathematical operations, which ensures that binary floating-point numbers cannot precisely maintain data precision. High-precision Decimal can perform modulo operations and equivalent operations that binary floating-point numbers cannot perform.

2.1 When to use decimal

The result of adding decimals in python will be incorrect, which is due to the accuracy of scientific notation

print(2.02 + 3.01)

2.2 Using decimals

Set precision: decimal.getcontext().prec = num # num is the number of valid digits

decimal.getcontext().prec = 4
print(decimal.Decimal(2.02) + decimal.Decimal(3.01))

Set the number of decimal places: quantize()

print(decimal.Decimal(3.1415926).quantize(decimal.Decimal("0.000")))

Guess you like

Origin blog.csdn.net/xiao__dashen/article/details/125381632