Python模块之Math

Python模块之Math

Module Math

import math

pi:数字常量,圆周率

print(’{0:12s}’.format(‘pi’), math.pi)

radians:把角度x转换成弧度

print(’{0:12s}’.format(‘radians’), math.radians(180))

degrees:把x从弧度转换成角度

print(’{0:12s}’.format(‘degrees’), math.degrees(math.pi))

sin:求x(x为弧度)的正弦值

print(’{0:12s}’.format(‘sin’), math.sin(math.radians(30)))

cos:求x的余弦,x必须是弧度

print(’{0:12s}’.format(‘cos’), math.cos(60), math.cos(math.pi/3))

tan:返回x(x为弧度)的正切值

print(’{0:12s}’.format(‘log’), math.tan(math.radians(45)))

ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x

print(’{0:12s}’.format(‘ceil’), math.ceil(12.3), math.ceil(-11.2))

copysign:把y的正负号加到x前面,可以使用0

print(’{0:12s}’.format(‘copysign’), math.copysign(-12, -10), math.copysign(12, -10), math.copysign(-12, 10))

e:表示一个常量

print(’{0:12s}’.format(‘e’), math.e)

exp:返回math.e,也就是2.71828的x次方

print(’{0:12s}’.format(‘exp’), math.exp(2))

expm1:返回math.e的x(其值为2.71828)次方的值减1

print(’{0:12s}’.format(‘expm1’), math.expm1(2))

log:log(x, i)返回以i为底x的自然对数,默认以e为基数

print(’{0:12s}’.format(‘log’), math.log(9, 3), math.log(math.e))

log10:返回x的以10为底的对数

print(’{0:12s}’.format(‘log10’), math.log10(100))

log1p:返回x+1的自然对数(基数为e)的值

print(’{0:12s}’.format(‘log1p’), math.log1p(math.e-1))

log2:返回x的基2对数

print(’{0:12s}’.format(‘log2’), math.log2(8))

fabs:返回x的绝对值

print(’{0:12s}’.format(‘fabs’), math.fabs(-3.1))

factorial:取x的阶乘的值

print(’{0:12s}’.format(‘factorial’), math.factorial(5))

floor:取小于等于x的最大的整数值,如果x是一个整数,则返回自身

print(’{0:12s}’.format(‘floor’), math.floor(-2.4))

fmod:得到x/y的余数,其值是一个浮点数

print(’{0:12s}’.format(‘fmod’), math.fmod(5, 3), math.fmod(4, 5))

frexp:返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围

print(’{0:12s}’.format(‘frexp’), math.frexp(2))

fsum:对迭代器里的每个元素进行求和操作

print(’{0:12s}’.format(‘fsum’), math.fsum([1, 2, 3, 4]), math.fsum((1, 2, 3)))

gcd:返回x和y的最大公约数

print(’{0:12s}’.format(‘gcd’), math.gcd(3, 15))

hypot:返回(x2 + y2)的平方根

print(’{0:12s}’.format(‘hypot’), math.hypot(3, 4))

isfinite:如果x不是正无穷大或负无穷大,则返回True,否则返回False

print(’{0:12s}’.format(‘isfinite’), math.isfinite(math.inf), math.isfinite(0))

isinf:如果x是正无穷大或负无穷大,则返回True,否则返回False

print(’{0:12s}’.format(‘isinf’), math.isinf(math.inf), math.isinf(0))

isnan:如果x不是数字True,否则返回False

print(’{0:12s}’.format(‘isnan’), math.isnan(3), math.isnan(-2))

ldexp:返回x*(2**i)的值

print(’{0:12s}’.format(‘ldexp’), math.ldexp(3, 2))

modf:返回由x的小数部分和整数部分组成的元组

print(’{0:12s}’.format(‘modf’), math.modf(-2.344))

pow:返回x的y次方,即x**y

print(’{0:12s}’.format(‘pow’), math.pow(2, 5))

sqrt:求x的平方根

print(’{0:12s}’.format(‘sqrt’), 在这里插入代码片math.sqrt(81))

trunc:返回x的整数部分

print(’{0:12s}’.format(‘trunc’), math.trunc(-3.14))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值