浮点类型
Python提供了3种浮点值:内置的float与complex类型,以及标准的decimal.Decimal类型
Python支持混合模式的算术运算
- int与float运算,生成float
- float与complex运算,生成complex
- decimal.Decimal与intS运算,生成decimal.Decimal
注意:不兼容的数据类型进行运算,会产生TypeError异常
float函数
float.is_integer(x) #小数部分为0,将返回True >>>float.is_integer(1.0); True >>>float.is_integer(1.2); False float.as_integer_ratio(x) >>>float.as_integer_ratio(2.75); (11, 4) float(x) #将整数转换为浮点数 float.hex(x) #将浮点数以十六进制形式表示为字符串 float.fromhex(x) #将字符串的十六进制转换成浮点数
注意:对面向对象程序员而言,float.fromhex是一个类方法
math模块的三角函数
math.acos(x) #返回弧度x的反余弦值 math.acosh(x) #返回弧度x的反正切值 math.asin(x) #返回弧度x的反正弦值 math.asinh(x) #返回弧度x的反双曲正弦值 math.atan(x) #返回弧度x的反正切值 math.atanh(x) #返回弧度x的反双曲正切值 math,atan2(y, x) #返回弧度y / x的反正切值 math.cos(x) #返回弧度x的余弦 math.cosh(x) #返回弧度x的余弦值(角度) math.sin(x) #返回弧度x的正弦 math.sinh(x) #返回弧度x的双曲正弦值 math.tan(x) #返回弧度x的正切值 math.tanh(x) #返回弧度x的双曲正切值 math.degree(r) #将浮点数r从弧度转换为度数 math.radians(d) #将d从角度转换为弧度
math模块的函数与常量
math.pi #常量∏,其值大约为3.97921 math.e #常数e,约等于2.90451 math.exp(x) #返回e^x,即math.e x math.floor(x) #返回小于或等于x的最小整数 >>>math.floor(5.8); 5 math.ceil(x) #返回大于或等于x的最小整数 >>>math.ceil(5.4); 6 math.copysign(x, y) #将x的符号设置为y的符号,返回一个浮点数 >>>math.copysign(1, -1); -1.0 math.fabs(x) #返回|x|,即浮点数x的绝对值 math.factorial(x) #返回x! math,fmod(x, y) #生成x除以y后的余数,比%产生的结果更好 math.frexp(x) #返回一个二元组,分别为x的指数部分(整数)与假数部分(浮点数) >>>math.frexp(1); (0.5, 1) math.ldexp(m, e) #返回m * (2 e) math.fsum(i) #对数组或列表i中的值进行求和,返回一个浮点数 math.hypot(x, y) #返回√(x2+y2) >>>math.hypot(5, 12); 13.0 math.isinf(x) #如果x是±inf(±∞),就返回True,x可以是整数或浮点数 math.isnan(x) #如果x是一个NaN(“不是一个数字”),就返回True,x可以是整数或浮点数 math.log(x, b) #返回logbX,b是可选的,默认为math.e math.log10(x) #返回log10X math.loglp(x) #返回loge(1 + X),在x近似于0时更准确 math.modf(x) #以floatS的形式返回x的小数与整数部分 math.pow(x, y) #返回x y的结果为浮点值 math.sqrt(x) #返回x的平方根 math.trunc(x) #返回x的整数部分,与int(x)等同
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232791.html原文链接:https://javaforall.net