Pythonベースのデジタル精度
711 ワード
1.9//2 #
0.0
1.9/2 #
0.95
import math
math.floor(1/2) #
0
math.ceil(1/2) #
round(0.3) #
0
round(0.8) #
1
round(1.22222,2) # 2
1.22
「数学理論から言えば、四捨五入、round(10.5、0)は11に進むべきだが、python 3.5のdocになると、ドキュメントは「values are rounded to the closest multiple of 10 to the power minus ndigits」になる.if two multiples are equally close, rounding is done toward the even choice.「両側から同じ距離にある場合は偶数の側に残ります.たとえばround(0.5)とround(-0.5)は0に残り、round(1.5)は2に残ります.」
round(10.500000001,0)
11.0
round(10.5,0)
10.0
計算結果の精度が予想と一致しない解決方法:
>>> 10-9.9
0.09999999999999964
>>> (10*100-9.9*100)/100
0.1