math
code:
# ****************************************************************
"""
math:
math.ceil(vars)---向上取整,内置函数round()四舍五入
math.floor(vars)---向下取整
math.pow(vars,n)---计算数值的n次方,返回值为浮点数
math.sqrt(vars)---开平方运算,返回值为浮点数
math.fabs(vars)---计算绝对值,返回值为浮点数
math.modf(vars)---把数值拆分成小数和整数组成的的元素,返回值为元素
math.copysign(x,y)---把第二个参数的正负符号拷贝给第一个参数,返回值为第一个参数+第二个参数符号,返回值为浮点数
math.fsum(container)---将一个容器类型中的元素求和,返回值为和的浮点数(容器类型中的元素必须为number)
math.factorial(x)---返回一个整数返回x的阶乘。 如果x不是整数或为负数时则将引发 ValueError。
还要很多函数见网址:https://docs.python.org/zh-cn/3/library/math.html
"""
import math
var_num = 99.99
print(f"var_num = {var_num}")
print(f"math.ceil(var_num) = {math.ceil(var_num)}")
print(f"math.floor(var_num) = {math.floor(var_num)}")
print(f"math.pow(9,2) = {math.pow(9,2)}")
print(f"math.sqrt(81) = {math.sqrt(81)}")
print(f"math.fabs(-99.99) = {math.fabs(-99.99)}")
print(f"math.modf(-99.99) = {math.modf(-99.99)}")
print(f"math.copysign(99,-99.99) = {math.copysign(99,-99.99)}")
print(f"math.fsum([1,2,3,4]]) = {math.fsum([1,2,3,4])}")
print(f"math.factorial(4) = {math.factorial(4)}")
运行结果:
E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Study_Basic_Grammar/_54Build_in_module_math.py
var_num = 99.99
math.ceil(var_num) = 100
math.floor(var_num) = 99
math.pow(9,2) = 81.0
math.sqrt(81) = 9.0
math.fabs(-99.99) = 99.99
math.modf(-99.99) = (-0.9899999999999949, -99.0)
math.copysign(99,-99.99) = -99.0
math.fsum([1,2,3,4]]) = 10.0
math.factorial(4) = 24
Process finished with exit code 0