Double類型精度問題引起的錯誤

來源:互聯網
上載者:User

Double類型精度問題引起的錯誤

情境說明

研發同事讓把某個double類型欄位的值四捨五入保留2位小數,mysql中round(col,2)可以實現四捨五入並且保留2位小數,但是神奇的事情發生了:發現有的四捨五入是正確的,而有的不是我們想要的結果,如下:簡單類比此情境:

yujx>drop table dd;
yujx>create table dd (a double);
yujx>insert into dd values(956.745),(231.34243252),(321.43534),(5464.446);
yujx>select a,round(a,2) from dd;
+--------------+------------+
| a            | round(a,2) |
+--------------+------------+
|      956.745 |    956.74 |     #可以看到並不是我們期望的956.75
| 231.34243252 |    231.34 |
|    321.43534 |    321.44 |
|    5464.446 |    5464.45 |
+--------------+------------+
4 rows in set (0.00 sec)

如上,有的是正確的,有的不是我們期望的值,那麼問題來了,為什麼呢?

通過詢問和網上搜尋後,發現可能是因為double的精度問題導致的

查閱MySQL官當關於double、Float、DECIMAL, NUMERIC類型的介紹,如下:

FLOAT, DOUBLE

#為了說明問題,摘了官當的部分內容,關於更詳細的介紹請直接查看官當。

12.2.3 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE

#可以看到標題已經指出 Float和double是近似值

The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

 

Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.5.8, “Problems with Floating-Point Values

因為Float、Double儲存的是一個近似值而不是確切的值,試圖使用它們儲存一個確切的值可以會導致問題。它們依賴於不同平台和不同實現方式,而官當在章節Section B.5.5.8, “Problems with Floating-Point Values中舉例說明了此問題

參考:http://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html

“Problems with Floating-Point Values”

B.5.4.8 Problems with Floating-Point Values

Floating-point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an SQL statement may not be the same as the value represented internally. Attempts to treat floating-point values as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. The FLOAT and DOUBLE data types are subject to these issues. For DECIMALcolumns, MySQL performs operations with a precision of 65 decimal digits, which should solve most common inaccuracy problems.

由於浮點數儲存的是近似值而不是確切的值,某些時候可能導致混亂。一個浮點數值在SQL語句作為內部表示的值可能不同。試圖使用float、double來儲存確切的值可能會出現問題,他們也依賴不同平台和實現方式。而對應DECIMAL類型,MySQL作為65位精度進行操作,可以解決此類精度問題。

如下例子證明使用Double運算遇到的異常錯誤

此結果是錯誤的,儘管前5條的a和b的值看起來是不滿足a<>b條件的。

此現象取決於各種因素,如電腦架構、編譯器版本或者最佳化層級等。例如,不同的CPU評估的浮點數不同

如果將欄位d1和d2改成DECIMAL類型,將不會存在此問題。如下:

綜上,如果想精確的儲存浮點數值,應該使用DECIMAL.比如金額等。

DECIMAL, NUMERIC

11.2.2 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC

The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it

is important to preserve exact precision, for example with monetary data. In MySQL, NUMERIC is

implemented as DECIMAL。

DECIMAL和NUMBERIC儲存的是確切的數值,使用它們可以保證精確度,例如用於儲存金額資料。在MySQL中,NUMBERIC和DECIMAL以同樣的類型實現。

double在其他平台

double類型不只是在MySQL中存在精度錯誤的問題,在Oracle、Java等平台同樣存在此問題,如下:

簡單的0.2+0.4,但是返回的結果不是0.6。

回到問題開始

如果換成DECIMAL類型,round的結果將是正確的,如下:

yujx>drop table dd;
yujx>create table dd (a double,b decimal(30,10));
yujx> insert into dd

values(956.745,956.745),(231.34243252,231.34243252),(321.43534,321.43534),

(5464.446,5464.446);
yujx>select a,round(a,2) from dd;

綜上

只為說明一個問題,如果想要確切的儲存小數(例如,金額等),建議使用DECIMAL類型,而不是DOUBLE、float類型。

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.