Python 数学计算模块详解
基本数学运算
常用数学计算和数值处理。
import math
def basic_math_operations():
circle_area = math.pi * math.pow(5, 2)
square_root = math.sqrt(16)
natural_log = math.log(math.e)
return circle_area, square_root, natural_log
def round_numbers(number):
ceil_value = math.ceil(number)
floor_value = math.floor(number)
truncate_value = math.trunc(number)
return ceil_value, floor_value, truncate_value
三角函数计算
角度与弧度的转换与计算。
def trigonometric_calculations(angle_degrees):
angle_radians = math.radians(angle_degrees)
sin_value = math.sin(angle_radians)
cos_value = math.cos(angle_radians)
tan_value = math.tan(angle_radians)
return {
"sin": sin_value,
"cos": cos_value,
"tan": tan_value
}
def inverse_trigonometric(value):
asin_value = math.degrees(math.asin(value))
acos_value = math.degrees(math.acos(value))
atan_value = math.degrees(math.atan(value))
return {
"asin": asin_value,
"acos": acos_value,
"atan": atan_value
}
对数运算
不同底数的对数计算。
def logarithm_operations(number):
natural_log = math.log(number)
log2_value = math.log2(number)
log10_value = math.log10(number)
return {
"ln": natural_log,
"log2": log2_value,
"log10": log10_value
}
def custom_base_log(number, base):
return math.log(number) / math.log(base)
数论函数
整数相关的数学运算。
def number_theory_operations(n, m):
factorial_n = math.factorial(n)
gcd_value = math.gcd(n, m)
lcm_value = abs(n * m) // math.gcd(n, m)
return {
"factorial": factorial_n,
"gcd": gcd_value,
"lcm": lcm_value
}
双曲函数
双曲线三角函数计算。
def hyperbolic_functions(x):
sinh_value = math.sinh(x)
cosh_value = math.cosh(x)
tanh_value = math.tanh(x)
return {
"sinh": sinh_value,
"cosh": cosh_value,
"tanh": tanh_value
}
数值精度处理
处理浮点数精度问题。
def handle_precision(value, precision=2):
rounded = round(value, precision)
formatted = format(value, f".{precision}f")
return {
"rounded": rounded,
"formatted": formatted,
"is_close": math.isclose(value, rounded,
rel_tol=1e-9)
}
特殊函数
特殊数学函数计算。
def special_functions(x):
gamma_value = math.gamma(x)
erf_value = math.erf(x)
return {
"gamma": gamma_value,
"error_function": erf_value
}