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

浮點數和字符串轉換的奇怪行為

Strange behaviour with floats and string conversion(浮點數和字符串轉換的奇怪行為)
本文介紹了浮點數和字符串轉換的奇怪行為的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經在 python shell 中輸入了這個:

I've typed this into python shell:

>>> 0.1*0.1
0.010000000000000002

我預計 0.1*0.1 不是 0.01,因為我知道以 10 為底的 0.1 在以 2 為底的周期性.

I expected that 0.1*0.1 is not 0.01, because I know that 0.1 in base 10 is periodic in base 2.

>>> len(str(0.1*0.1))
4

我預計會得到 20 個,因為我在上面看到了 20 個字符.為什么我得到 4?

I expected to get 20 as I've seen 20 characters above. Why do I get 4?

>>> str(0.1*0.1)
'0.01'

好的,這就解釋了為什么我 len 給我 4,但為什么 str 返回 '0.01'?

Ok, this explains why I len gives me 4, but why does str return '0.01'?

>>> repr(0.1*0.1)
'0.010000000000000002'

為什么 str 圓而 repr 不圓?(我已閱讀 this answer,但我想知道他們是如何決定何時 str舍入一個浮點數,當它不舍入時)

Why does str round but repr not? (I have read this answer, but I would like to know how they have decided when str rounds a float and when it doesn't)

>>> str(0.01) == str(0.0100000000001)
False
>>> str(0.01) == str(0.01000000000001)
True

所以浮動的準確性似乎有問題.我認為 Python 會使用 IEEE 754 單精度浮點數.所以我已經這樣檢查了:

So it seems to be a problem with the accuracy of floats. I thought Python would use IEEE 754 single precicion floats. So I've checked it like this:

#include <stdint.h>
#include <stdio.h> // printf

union myUnion {
    uint32_t i; // unsigned integer 32-bit type (on every machine)
    float f;    // a type you want to play with
};

int main() {
    union myUnion testVar;
    testVar.f = 0.01000000000001f;
    printf("%f
", testVar.f);

    testVar.f = 0.01000000000000002f;
    printf("%f
", testVar.f);

    testVar.f = 0.01f*0.01f;
    printf("%f
", testVar.f);
}

我明白了:

0.010000
0.010000
0.000100

Python 給了我:

Python gives me:

>>> 0.01000000000001
0.010000000000009999
>>> 0.01000000000000002
0.010000000000000019
>>> 0.01*0.01
0.0001

為什么 Python 會給我這些結果?

Why does Python give me these results?

(我使用 Python 2.6.5.如果您知道 Python 版本的差異,我也會對它們感興趣.)

(I use Python 2.6.5. If you know of differences in the Python versions, I would also be interested in them.)

推薦答案

repr 的關鍵要求是它應該往返;也就是說,eval(repr(f)) == f 在所有情況下都應該給出 True.

The crucial requirement on repr is that it should round-trip; that is, eval(repr(f)) == f should give True in all cases.

在 Python 2.x(2.7 之前)中,repr 通過使用 %.17g 格式執行 printf 并丟棄尾隨零來工作.IEEE-754 保證這是正確的(對于 64 位浮點數).從 2.7 和 3.1 開始,Python 使用了一種更智能的算法,可以在某些情況下找到更短的表示,其中 %.17g 給出了不必要的非零終端數字或終端九.請參閱 3.1 中有哪些新功能? 和 問題 1580.

In Python 2.x (before 2.7) repr works by doing a printf with format %.17g and discarding trailing zeroes. This is guaranteed correct (for 64-bit floats) by IEEE-754. Since 2.7 and 3.1, Python uses a more intelligent algorithm that can find shorter representations in some cases where %.17g gives unnecessary non-zero terminal digits or terminal nines. See What's new in 3.1? and issue 1580.

即使在 Python 2.7 下,repr(0.1 * 0.1) 也會給出 "0.010000000000000002".這是因為0.1 * 0.1 == 0.01在IEEE-754解析和算術下是False;也就是說,最接近 0.1 的 64 位浮點值,當與自身相乘時,會產生一個不是最接近 0.1 的 64 位浮點值的 64 位浮點值代碼>0.01:

Even under Python 2.7, repr(0.1 * 0.1) gives "0.010000000000000002". This is because 0.1 * 0.1 == 0.01 is False under IEEE-754 parsing and arithmetic; that is, the nearest 64-bit floating-point value to 0.1, when multiplied by itself, yields a 64-bit floating-point value that is not the nearest 64-bit floating-point value to 0.01:

>>> 0.1.hex()
'0x1.999999999999ap-4'
>>> (0.1 * 0.1).hex()
'0x1.47ae147ae147cp-7'
>>> 0.01.hex()
'0x1.47ae147ae147bp-7'
                 ^ 1 ulp difference

reprstr (pre-2.7/3.1) 的區別在于 str 格式有 12 位小數,而不是 17 位,這是不可往返的,但在許多情況下會產生更具可讀性的結果.

The difference between repr and str (pre-2.7/3.1) is that str formats with 12 decimal places as opposed to 17, which is non-round-trippable but produces more readable results in many cases.

這篇關于浮點數和字符串轉換的奇怪行為的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
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 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 成人精品一区亚洲午夜久久久 | 国产精品99久久久久久久久久久久 | 99色播 | 在线播放国产一区二区三区 | 精品在线一区二区三区 | 午夜综合 | 人人亚洲 | 91精品国产综合久久香蕉922 | 三级成人在线 | 97狠狠干 | 免费看a | 久久天天躁狠狠躁夜夜躁2014 | 羞羞视频在线观看 | 自拍第一页 | 超碰电影 | 一区二区三区欧美在线观看 | 美女天天干天天操 | 自拍偷拍欧美 | 秋霞影院一区二区 | 日韩欧美综合在线视频 | 欧美aa在线 | 成人福利影院 | 中文字幕综合 | 午夜精品 | 一区 | 午夜精品在线观看 | 黄色一级毛片 | 一区二区三区在线免费观看 | 国产精品美女久久久久久久久久久 | 国产精品日韩欧美一区二区三区 | 国产一区二区三区四区在线观看 | 久久一日本道色综合久久 | 国产亚洲精品久久yy50 | 久久这里只有 | 91麻豆精品国产91久久久久久 | 亚洲欧美视频一区二区 | 久久精品网 | 久久亚洲国产精品日日av夜夜 | 精品国产伦一区二区三区观看方式 | 久久精品在线 | 欧美激情亚洲 |