Four: Python provides high-precision Decimal class and Fraction class for fraction calculation:
import Decimal
from fractions import Fraction as F
print(0.1)print(F(1,3))#表示分数1/3print(1/F(5,6))#表示分数6/5#Python用于数值计算的函数print(abs(-3.4))print(min(3.4,2.8))print(max(3.4,2.8))print(pow(0.3,4))print(round(2.8))#取最接近的整数
print(round(2.3))print(round(-2,3))
运行结果如下:3.42.83.40.008132-2
Five: Import the math module:
import math
print("*"*50)print(math.pi)print(math.e)
#浮点计算可能产生两个异常值,inf和nan,也可能抛出一个OverflowError异常。
#当浮点数超过float类型数值表示范围时,产生的结果是无穷大(inf)
#并不是所有溢出的值都用inf表示,结果是inf还是异常是底层C Python决定的
print("*"*50)
x =10.0**200
y = x*x
print(y)print(math.isinf(y))
#如果除inf的结果是未定的,则结果是nan(未定的数值).
#因为nan不能和其他值进行比较,所以只能用函数ianan()检查nan
print("*"*50)
x =(10.0)**200*(10.0)**200
y = x/x
print(y)print(math.isnan(y))
运行结果如下:**************************************************3.1415926535897932.718281828459045**************************************************
inf
True
**************************************************
nan
True
Six: You can also use the function isfinite() to check whether a value is a regular value or a special value inf or nan
import math
for i in [math.pi,math.nan,math.inf]:print(math.isfinite(i))
运行结果如下:
True
False
False
Seven: equality comparison of floating-point numbers
1.isclose(a,b,*,rel_tol=le-09,abs_tol=0.0): The default parameters rel_tol and abs_tol represent relative error and absolute error respectively
import math
a =1.0-0.99
b =100-99.99print(a==b)print(math.isclose(a, b))
运行结果如下:
False
True
Eight: floating-point number converted to integer
1. The function trunc() truncates the decimal part of a floating-point number
2. The function floor() can be converted to the largest integer smaller than it
3. The function ceil() converts a floating point number to the smallest integer larger than it
import math
a =1.8print(math.trunc(a))print(math.floor(a))print(math.ceil(a))
运行结果如下
112
4.sqrt(x) is used to calculate the square root of a number x
5.pow(x,y) is used to calculate the y power of x, similar to x ** y, but pow (x, y) can be guaranteed to operate according to floating point numbers, and x ** y can only return an int or float
6.exp(x) Exponential function with natural constant as the base
7.expml(x) calculate e**x-1
8. The logarithmic function log(x,base) has base and some variants log2(x) log10(x) log1p(x):
from math import *print(sqrt(9))print(pow(3,3))print(exp(1))print(expm1(1))print(log10(10))print(log1p(1))
#什么都不写就是ln
print(log(e))
运行结果如下:3.027.02.7182818284590451.7182818284590451.00.69314718055994531.0
Nine: Angles and radians
1. The function radians() converts the angle value into radians
2. The function degrees() converts a radian value into an angle value
from math import *print(radians(45))print(degrees(pi))
运行结果如下:0.7853981633974483180.0
Ten: The math module contains various trigonometric functions and inverse trigonometric functions such as sin, cos, tan, atan.
from math import *print(sin(0))print(cos(0))print(tan(0))print(degrees(atan(1)))#转化成角度了
运行结果如下:0.01.00.045.0
11. Random number: random module
import random
l =[1,2,3,4]print(random.random())#0-1的随机小数print(random.randint(100,900))#100-900的随机数print(random.choice(l))#任意选取一个列表中的元素
random.shuffle(l)#打乱列表print(l)'''
0.9724452742237619
188
1
[1, 2, 4, 3]
'''