python 浮點(diǎn)數(shù)四舍五入需要注意的地方
本文主要分享基于python的數(shù)據(jù)分析三方庫(kù)pandas,numpy的一次爬坑經(jīng)歷,發(fā)現(xiàn)并分析了python語(yǔ)言對(duì)于浮點(diǎn)數(shù)精度處理不準(zhǔn)確的問(wèn)題,并在最后給出合理的解決方案。如果你也在用python處理數(shù)據(jù),建議看一下,畢竟0.1的誤差都可能造成比較大的影響。
問(wèn)題出現(xiàn)
早上到了公司,領(lǐng)導(dǎo)發(fā)了幾個(gè)文件過(guò)來(lái),說(shuō)這兩天測(cè)試環(huán)境跑出來(lái)的數(shù)據(jù),與實(shí)際情況有所出入,看看哪出的問(wèn)題,盡快解決···
開(kāi)始排查
先對(duì)比數(shù)據(jù),發(fā)現(xiàn)并不是所有的數(shù)據(jù)都出現(xiàn)問(wèn)題,只有10%左右的數(shù)據(jù)有這個(gè)問(wèn)題,說(shuō)明應(yīng)該不是邏輯上的問(wèn)題,初步判斷可能為個(gè)別情況需要特殊處理,考慮不周導(dǎo)致檢查梳理各個(gè)運(yùn)算模塊,用debug斷點(diǎn)調(diào)試一波,確定了數(shù)據(jù)出現(xiàn)偏差的模塊通過(guò)單獨(dú)測(cè)試這個(gè)單元模塊最終確定,涉及到兩數(shù)相除結(jié)果為0.5(浮點(diǎn)數(shù))的地方有問(wèn)題預(yù)期結(jié)果:np.round(0.5)=1,實(shí)際運(yùn)算結(jié)果:np.round(0.5)=0,于是我做了如下的試驗(yàn)
# 基于python3.7版本 >>> import numpy as np # 先看看 0 < x < 1 這個(gè)范圍的結(jié)果,發(fā)現(xiàn)有問(wèn)題 >>> np.round(0.50) 0.0 >>> np.round(0.51) 1.0 >>> np.round(0.49) 0.0 # 我擔(dān)心是不是只有小數(shù)點(diǎn)為.5的都會(huì)呈現(xiàn)這種問(wèn)題,所以測(cè)試了 x > 1的結(jié)果,發(fā)現(xiàn)還是有問(wèn)題 >>> np.round(1.5) 2.0 >>> np.round(2.5) 2.0 >>> np.round(3.5) 4.0 >>> np.round(4.5) 4.0
通過(guò)對(duì)比,發(fā)現(xiàn)確實(shí)涉及到.5的值會(huì)有些和預(yù)想的不同,看看啥原因
分析問(wèn)題
確實(shí)發(fā)現(xiàn)了關(guān)于浮點(diǎn)數(shù)(.5出現(xiàn)了理解上的偏差),看看官方文檔怎么解釋這個(gè)現(xiàn)象
numpy.around(a, decimals=0, out=None)[source] Evenly round to the given number of decimals. # 對(duì)于恰好介于四舍五入的十進(jìn)制值之間的中間值(.5),NumPy會(huì)四舍五入為最接近的偶數(shù)值。 # 因此1.5和2.5四舍五入為2.0,-0.5和0.5四舍五入為0.0,依此類(lèi)推。 For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc. # np.around使用快速但有時(shí)不精確的算法來(lái)舍入浮點(diǎn)數(shù)據(jù)類(lèi)型。 # 對(duì)于正小數(shù),它等效于np.true_divide(np.rint(a * 10 **小數(shù)),10 **小數(shù)), # 由于IEEE浮點(diǎn)標(biāo)準(zhǔn)[1]和 十次方縮放時(shí)引入的錯(cuò)誤 np.around uses a fast but sometimes inexact algorithm to round floating-point datatypes. For positive decimals it is equivalent to np.true_divide(np.rint(a * 10**decimals), 10**decimals), which has error due to the inexact representation of decimal fractions in the IEEE floating point standard [1] and errors introduced when scaling by powers of ten
其實(shí)也就是說(shuō):對(duì)于帶有.5這種剛好介于中間的值,返回的是相鄰的偶數(shù)值 白話解釋?zhuān)喝绻粋€(gè)數(shù)字帶有浮點(diǎn)數(shù)(.5),整數(shù)部分為偶數(shù),則返回這個(gè)偶數(shù);整數(shù)部分奇數(shù),則返回這個(gè)奇數(shù)+1的偶數(shù) 規(guī)律解釋?zhuān)喝绻麛?shù)部分能夠整除2,則返回整數(shù)部分;如果整數(shù)部分不能整除2,則返回整數(shù)部分 +1解決問(wèn)題
先不做任何改動(dòng),看下數(shù)據(jù)誤差的情形
# 我們?yōu)榱讼瓤聪卢F(xiàn)象,構(gòu)造如下案例 import pandas as pd import numpy as np df = pd.DataFrame({'num1': [1, 1, 1.5, 5, 7.5], 'num2': [2, 3, 1, 6, 3]}) df['真實(shí)值'] = df['num1'] / df['num2'] # 看下round函數(shù)過(guò)后的結(jié)果 df['偏差值'] = np.round(df['num1'] / df['num2'])
原始結(jié)果圖片如下
不做處理,期望值和偏差值不等的情況出現(xiàn)
我的解決方案
我根據(jù)我的精度要求,構(gòu)建精度范圍所需要保留的小數(shù)點(diǎn)的最后一位,通過(guò)這個(gè)數(shù)字是否為5,判斷是否需要向上取整 舉例來(lái)說(shuō),本案例中我只需要保留整數(shù)部分的數(shù)據(jù),那么我只需要確定小數(shù)點(diǎn)后第一位是否是數(shù)字5就可以了上代碼
import pandas as pd import numpy as np import math df = pd.DataFrame({'除數(shù)': [1, 1, 1.5, 5, 7.5], '被除數(shù)': [2, 3, 1, 6, 3]}) # 記錄真實(shí)值 df['真實(shí)值'] = df['除數(shù)'] / df['被除數(shù)'] # 記錄整數(shù)部分 df['輔助整數(shù)列'] = df['真實(shí)值'].apply(lambda x: math.modf(x)[1]) # 記錄小數(shù)部分,因?yàn)槲业淖詈蠼Y(jié)果精度為只保留整數(shù)部分,所以我只需要保留一個(gè)小數(shù)點(diǎn)位進(jìn)行判斷是否需要進(jìn)位操作 df['輔助小數(shù)列'] = df['真實(shí)值'].apply(lambda x: str(math.modf(x)[0]).split('.')[1][0]) # 小數(shù)點(diǎn)后的第一位是為5,則向上取整,不是5則調(diào)用原np.round就行了 df['期望值修正'] = df.apply(lambda x: x.輔助整數(shù)列 + 1 if (x.輔助小數(shù)列 == '5') else np.round(x.真實(shí)值), axis=1)
結(jié)果如下所示
以上就是python 四舍五入需要注意的地方的詳細(xì)內(nèi)容,更多關(guān)于python 四舍五入的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)3. Jsp servlet驗(yàn)證碼工具類(lèi)分享4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)5. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)6. XML在語(yǔ)音合成中的應(yīng)用7. jscript與vbscript 操作XML元素屬性的代碼8. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解9. 基于PHP做個(gè)圖片防盜鏈10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
