format(float(5)/float(7), '.%20f')
出力を0.714285714285714301575
round(float(n)/float(d) * 10**20)/10**20
出力が0.714285714286
私は間違っていますか? floatの最大精度はどれくらいですか? Pythonの2.xの
format(float(5)/float(7), '.%20f')
出力を0.714285714285714301575
round(float(n)/float(d) * 10**20)/10**20
出力が0.714285714286
私は間違っていますか? floatの最大精度はどれくらいですか? Pythonの2.xの
のPython 3.xのこの
のような私の作品
>>> '{:.9f}'.format(8/3)
'2.666666667'
>>> '{:.15f}'.format(8/3)
'2.666666666666667'
>>> '{:.20f}'.format(8/3)
'2.66666666666666651864'
>>> '{:.20f}'.format(8.0/3)
'2.66666666666666651864'
>>> '{:.20f}'.format(8/3.0)
'2.66666666666666651864'
>>> '{:.20f}'.format(float(8)/3)
'2.66666666666666651864'
>>> '{:.20f}'.format(8/float(3))
'2.66666666666666651864'
OPは '5/7'を変換しようとしていますが、なぜこれらの値を使用しませんでしたか? – martineau
いいえ、私はランダムに選んだ理由はありません。あなたは5/7も使えます。 –
'{:.20f}' format '(5/7.0)は' 0.71428571428571430157 'に正しい文字列を与えますが、floatとfloat(' {:。20f} ')が必要です。 (5/7.0))不正確な結果が得られます。 –
あなたがdecimal型を使用してみました... 'from decimal import * 小数点(5)/小数点( 7) ' https://docs.python.org/2/library/decimal.html – jcfollower
こちらのヒントをご覧ください:https://docs.python.org/3/tutorial/floatingpoint.html#representation-error – Arseniy
私は小数点(5)/小数点(7)を丸めますか? –