久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python 3 浮點小數(shù)點/精度

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
本文介紹了Python 3 浮點小數(shù)點/精度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在讀取一個帶有浮點數(shù)的文本文件,所有這些都帶有 1 或 2 個小數(shù)點.我正在使用 float() 將一行轉(zhuǎn)換為浮點數(shù),如果失敗則引發(fā) ValueError .我將所有浮點數(shù)存儲在一個列表中.打印出來時,我想將其打印為小數(shù)點后 2 位的浮點數(shù).

I am reading a text file with floating point numbers, all with either 1 or 2 decimal points. I am using float() to convert a line into a float, and raising a ValueError if that fails. I am storing all floats in a list. When printing it out, I'd like to print it out as a 2 decimal places floating point.

假設(shè)我有一個帶有數(shù)字 -3,65, 9,17, 1 的文本文件.我閱讀了每個文件,并將它們轉(zhuǎn)換為浮點數(shù)并將它們附加到列表中.現(xiàn)在在 Python 2 中,調(diào)用 float(-3.65) 會返回 -3.65.然而,在 Python 3 中,float(-3.65) 返回-3.6499999999999999`,這會失去其精度.

Assume I have a text file with the numbers -3,65, 9,17, 1. I read each one, and once I convert them to float and append them to a list. Now in Python 2, calling float(-3.65) returns -3.65. In Python 3 however, float(-3.65) returns-3.6499999999999999` which loses its precision.

我想打印浮點數(shù)列表,[-3.6499999999999999, 9.1699999999999999, 1.0] 僅包含 2 個小數(shù)點.按照 '%.1f' % round(n, 1) 的行做一些事情會返回一個字符串.如何返回浮點數(shù)的所有兩個小數(shù)點的列表,而不是字符串?到目前為止,我使用 [round(num, 2) for num in list] 對其進行了四舍五入,但需要設(shè)置小數(shù)點/精度而不是 round().

I want to print the list of floats, [-3.6499999999999999, 9.1699999999999999, 1.0] with 2 decimal points only. Doing something along the lines of '%.1f' % round(n, 1) would return a string. How can I return a list of all two decimal points of floats, and not strings? So far, I rounded it using [round(num, 2) for num in list] but would need to set the decimal points / precision instead of round().

推薦答案

一句話,你不能.

3.65 不能完全表示為 float.您得到的數(shù)字是與 3.65 最接近的數(shù)字,具有精確的 float 表示.

3.65 cannot be represented exactly as a float. The number that you're getting is the nearest number to 3.65 that has an exact float representation.

(舊的?)Python 2 和 3 之間的區(qū)別純粹是由于默認格式.

The difference between (older?) Python 2 and 3 is purely due to the default formatting.

我在 Python 2.7.3 和 3.3.0 中都看到了以下內(nèi)容:

I am seeing the following both in Python 2.7.3 and 3.3.0:

In [1]: 3.65
Out[1]: 3.65

In [2]: '%.20f' % 3.65
Out[2]: '3.64999999999999991118'

有關(guān)精確的十進制數(shù)據(jù)類型,請參閱 decimal.Decimal.

For an exact decimal datatype, see decimal.Decimal.

這篇關(guān)于Python 3 浮點小數(shù)點/精度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個數(shù)字在舍入到 n 個有效十進制數(shù)字時是否幾乎相等的函數(shù))
主站蜘蛛池模板: 六月天婷婷| 在线日韩 | 福利视频在线 | 激情av小说 | 日本福利视频 | 日韩av大全 | 久久久久久久成人 | 成人高清视频在线观看 | 欧美激情视频网站 | 国产成人三级一区二区在线观看一 | 免费av片 | 国产三级在线观看 | 青青操影院 | 中文字幕国产 | 免费看a级片| 亚洲欧洲av| 国产精品福利在线 | av片在线看 | 天堂一区二区三区 | 国产无遮挡又黄又爽又色 | 国产一区精品在线 | www国产亚洲精品久久网站 | 国产精品视频一区二区三区 | 夜夜狠狠擅视频 | 久久av一区二区 | aaa国产精品| 精品久久久久久一区二区里番 | 免费观看黄色大片 | 91福利网 | 国产欧美日韩一区 | 日韩久久综合 | 久久久精品一区二区三区 | 99视频网 | 久久久久久一区 | 免费性视频 | 亚洲一级特黄 | 黄视频网站在线观看 | 国产一区福利 | 色综合视频在线观看 | 97视频在线观看免费 | 极品在线视频 |