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

為什么 4*0.1 的浮點(diǎn)值在 Python 3 中看起來(lái)不錯(cuò),但

Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn#39;t?(為什么 4*0.1 的浮點(diǎn)值在 Python 3 中看起來(lái)不錯(cuò),但 3*0.1 不好看?)
本文介紹了為什么 4*0.1 的浮點(diǎn)值在 Python 3 中看起來(lái)不錯(cuò),但 3*0.1 不好看?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我知道大多數(shù)小數(shù)都沒(méi)有精確的浮點(diǎn)表示(浮點(diǎn)數(shù)學(xué)有問(wèn)題嗎?).p>

但我不明白為什么 4*0.1 可以很好地打印為 0.4,但 3*0.1 不是,當(dāng)這兩個(gè)值實(shí)際上都有丑陋的十進(jìn)制表示:

>>>3*0.10.30000000000000004>>>4*0.10.4>>>從十進(jìn)制導(dǎo)入十進(jìn)制>>>十進(jìn)制(3*0.1)十進(jìn)制('0.30000000000000000444089209850062616169452667236328125')>>>十進(jìn)制(4*0.1)十進(jìn)制('0.400000000000000002220446049250313080847263336181640625')

解決方案

簡(jiǎn)單的答案是因?yàn)?3*0.1 != 0.3 由于量化(舍入)誤差(而 4*0.1== 0.4 因?yàn)槌艘?2 的冪通常是精確"運(yùn)算).Python 試圖找到可以四舍五入到所需值的最短字符串,因此它可以將 4*0.1 顯示為 0.4,因?yàn)樗鼈兪窍嗟鹊模荒軐?3*0.1 顯示為 0.3 因?yàn)樗鼈儾幌嗟?

您可以使用 Python 中的 .hex 方法查看數(shù)字的內(nèi)部表示(基本上是 exact 二進(jìn)制浮點(diǎn)值,而不是 base-10近似).這有助于解釋幕后發(fā)生的事情.

>>>(0.1).hex()'0x1.999999999999ap-4'>>>(0.3).hex()'0x1.3333333333333p-2'>>>(0.1*3).hex()'0x1.3333333333334p-2'>>>(0.4).hex()'0x1.999999999999ap-2'>>>(0.1*4).hex()'0x1.999999999999ap-2'

0.1 是 0x1.999999999999a 乘以 2^-4.一個(gè)"最后表示數(shù)字 10 - 換句話說(shuō),二進(jìn)制浮點(diǎn)中的 0.1 非常輕微 比精確"大.值 0.1(因?yàn)樽罱K的 0x0.99 向上舍入為 0x0.a).當(dāng)您將此乘以 4(2 的冪)時(shí),指數(shù)會(huì)向上移動(dòng)(從 2^-4 到 2^-2),但數(shù)字不會(huì)改變,因此 4*0.1 == 0.4.

但是,當(dāng)您乘以 3 時(shí),0x0.99 和 0x0.a0 (0x0.07) 之間的微小差異會(huì)放大為 0x0.15 錯(cuò)誤,在最后一個(gè)位置顯示為一位數(shù)錯(cuò)誤.這會(huì)導(dǎo)致 0.1*3 比 0.3 的舍入值非常輕微大.

Python 3 的浮點(diǎn) repr 被設(shè)計(jì)為 round-trippable,也就是說(shuō),顯示的值應(yīng)該完全可以轉(zhuǎn)換為原始值(float(repr(f)) == f 對(duì)于所有浮點(diǎn)數(shù) f).因此,它不能以完全相同的方式顯示0.30.1*3,否則兩個(gè)不同的數(shù)字會(huì)在往返后最終相同.因此,Python 3 的 repr 引擎選擇顯示一個(gè)帶有輕微明顯錯(cuò)誤的文件.

I know that most decimals don't have an exact floating point representation (Is floating point math broken?).

But I don't see why 4*0.1 is printed nicely as 0.4, but 3*0.1 isn't, when both values actually have ugly decimal representations:

>>> 3*0.1
0.30000000000000004
>>> 4*0.1
0.4
>>> from decimal import Decimal
>>> Decimal(3*0.1)
Decimal('0.3000000000000000444089209850062616169452667236328125')
>>> Decimal(4*0.1)
Decimal('0.40000000000000002220446049250313080847263336181640625')

解決方案

The simple answer is because 3*0.1 != 0.3 due to quantization (roundoff) error (whereas 4*0.1 == 0.4 because multiplying by a power of two is usually an "exact" operation). Python tries to find the shortest string that would round to the desired value, so it can display 4*0.1 as 0.4 as these are equal, but it cannot display 3*0.1 as 0.3 because these are not equal.

You can use the .hex method in Python to view the internal representation of a number (basically, the exact binary floating point value, rather than the base-10 approximation). This can help to explain what's going on under the hood.

>>> (0.1).hex()
'0x1.999999999999ap-4'
>>> (0.3).hex()
'0x1.3333333333333p-2'
>>> (0.1*3).hex()
'0x1.3333333333334p-2'
>>> (0.4).hex()
'0x1.999999999999ap-2'
>>> (0.1*4).hex()
'0x1.999999999999ap-2'

0.1 is 0x1.999999999999a times 2^-4. The "a" at the end means the digit 10 - in other words, 0.1 in binary floating point is very slightly larger than the "exact" value of 0.1 (because the final 0x0.99 is rounded up to 0x0.a). When you multiply this by 4, a power of two, the exponent shifts up (from 2^-4 to 2^-2) but the number is otherwise unchanged, so 4*0.1 == 0.4.

However, when you multiply by 3, the tiny little difference between 0x0.99 and 0x0.a0 (0x0.07) magnifies into a 0x0.15 error, which shows up as a one-digit error in the last position. This causes 0.1*3 to be very slightly larger than the rounded value of 0.3.

Python 3's float repr is designed to be round-trippable, that is, the value shown should be exactly convertible into the original value (float(repr(f)) == f for all floats f). Therefore, it cannot display 0.3 and 0.1*3 exactly the same way, or the two different numbers would end up the same after round-tripping. Consequently, Python 3's repr engine chooses to display one with a slight apparent error.

這篇關(guān)于為什么 4*0.1 的浮點(diǎn)值在 Python 3 中看起來(lái)不錯(cuò),但 3*0.1 不好看?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 丁香婷婷激情 | 毛片tv| 超碰在线免费播放 | 国产亚洲视频在线观看 | 国产小视频在线 | 黄色网址av | 天天躁日日躁狠狠躁 | 福利网站在线观看 | 麻豆一区二区三区 | 国产在线网站 | 久久99精品久久久久久 | 色婷婷视频在线观看 | a级黄色片| 在线观看国产小视频 | 免费的毛片 | 亚洲精品视频免费在线观看 | 国产永久视频 | 欧美中文字幕 | 日本精品国产 | 中文字幕在线看片 | 久久黄色免费视频 | 五月天在线观看 | 伊人久久av | 香蕉在线观看 | 免费一级a毛片 | 日韩激情一区 | www操| 在线播放中文字幕 | 911亚洲精品| 香蕉在线观看视频 | 国产成人午夜 | 91精品国产乱码久久久久 | 亚洲欧美另类在线 | 久久亚洲天堂 | 国产主播av | 六月激情| 免费人成| 99精品久久久久久 | 日韩免费在线 | 国产网站视频 | 国产精品偷乱一区二区三区 |