2009-10-14 68 views
4

我缺少什麼:奇怪numpy.float96行爲

In [66]: import numpy as np 

In [67]: np.float(7.0/8) 
Out[67]: 0.875 #OK 

In [68]: np.float32(7.0/8) 
Out[68]: 0.875 #OK 

In [69]: np.float96(7.0/8) 
Out[69]: -2.6815615859885194e+154 #WTF 

In [70]: sys.version 
Out[70]: '2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]' 

編輯。 在Cygwin上面的代碼工作確定:

$ python 
Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) 
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import numpy as np 
>>> np.float(7.0/8) 
0.875 
>>> np.float96(7.0/8) 
0.875 

對於完整性,我檢查這個代碼在普通的Python(未IPython中):

C:\temp>python 
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on 
win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import numpy as np 
>>> np.float(7.0/8) 
0.875 
>>> np.float96(7.0/8) 
-2.6815615859885194e+154 
>>> 

編輯

我看到了三個錯誤關於Numpy的trac站點的報告(976,902884),但這個似乎與字符串表示沒有關係。所以我開了一個新的bug(1263)。將更新這裏的進步

+0

我不能在linux(ubuntu 64位)上重現這個,因爲它沒有'float96',只有'float128',所以這個問題可能是Windows特定的。 – 2009-10-14 12:41:11

+0

無法在Mac雪豹上重現,原因相同。 – 2009-10-14 12:56:37

+0

我希望看到bug報告的結果:) – Nope 2009-10-15 17:08:54

回答

2

也能正常工作對我來說:

In [1]: import numpy as np 

In [2]: np.float(7.0/8) 
Out[2]: 0.875 

In [3]: np.float96(7.0/8) 
Out[3]: 0.875 

什麼numpy的是您使用?我使用Python 2.6.2和Numpy 1.3.0,我在64位Vista上。

我試圖運行32位XP與Python 2.5.2和1.2.1 numpy的另一臺計算機上同樣的事情讓我吃驚,我得到:

In [2]: np.float96(7.0/8) 
Out[2]: -2.6815615859885194e+154 

經過一番調查後,安裝Python 2.6。 3和32位XP numpy的1.3.0,我發現:

In [2]: np.float96(7.0/8) 
Out[2]: 0.875 

所以必須處於或舊版本numpy的在舊版本的Python中的錯誤的錯誤...

2

問題是由MinGW的編譯器(用於正式numpy的二進制的)和MS運行時(該一個printf的是來自)之間的不兼容而引起的。

MS編譯器認爲long double和double是等效類型,MS C運行時(包括printf)也是如此。由於某種原因,Mingw將long double定義爲足夠大以容納80位擴展精度數字,但是當然MS printf不知道它,並且無法正確打印long double。

我們通過使用我們自己的格式化函數繞過了一些問題,但我認爲真正的解決方法是強制使用long double作爲使用mingw構建時的雙倍同義詞。我認爲這將會爲numpy 1.5.0完成。