PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   疑難雜症區 (https://www.pcdvd.com.tw/forumdisplay.php?f=34)
-   -   Microsoft Visual Basic 6.0 加法問題 (https://www.pcdvd.com.tw/showthread.php?t=664432)

Midgard 2006-10-20 06:10 PM

Microsoft Visual Basic 6.0 加法問題
 
請問各位
一個很簡單的 VB加法,程式碼如下

Dim temp As Single

Private Sub Command1_Click()

Label1.Caption = temp
temp = temp + 0.1

End Sub


選擇在Label1裡觀看加法情形,發現有趣的情況
Command1每按一次,Label1秀出結果
依序如下

0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8000001
0.9000001
1
1.1
1.2

有人可以解釋那0.000001的來歷嗎 ?
希望有高手解答 ~ :)

kec 2006-10-21 06:00 AM

因為浮點數無法精確地儲存某些數值。

http://en.wikipedia.org/wiki/Floating-point_number
引用:
Floating-point numbers are intended to approximate the mathematical real numbers. But while the real numbers form a continuum that can be subdivided without limit, floating-point numbers have only finite resolution—they can only represent discrete points on the real number line. (In double precision representation, consecutive points differ by about 1 part in 1016.) That is, they can only represent a subset of the reals.

Midgard 2006-10-21 11:29 AM

引用:
作者kec
因為浮點數無法精確地儲存某些數值。

http://en.wikipedia.org/wiki/Floating-point_number


嗯 ~ 大約了解你的意思了 感謝 ~


所有的時間均為GMT +8。 現在的時間是10:19 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。