当前位置:编程学习 > VB >>

奇怪的结果

Option Explicit
Private Sub Command1_Click()

Dim x0 As Single
Dim x1 As Single
Dim y0 As Single
Dim y1 As Single
Dim i As Single
Dim a As Long

x0 = 0
x1 = 0
y0 = 0
y1 = 0
a = 0


Picture1.Scale (0, 10)-(10, 0)
    Open "d:\10.txt" For Output As #1
    
        For i = 0.1 To 10 Step 0.1
            a = a + 1
            x1 = x1 + 0.1
            y1 = y1 + 0.1
        Picture1.Line (x0, y0)-(x1, y1)
            
            Print #1, a; Tab(10); x1; Tab(50); y1
            x0 = x1
            y0 = y1
            
       Next
       
    Close #1

End Sub
上述代码执行的结果:

 1        .1                                      .1 
 2        .2                                      .2 
 3        .3                                      .3 
 4        .4                                      .4 
 5        .5                                      .5 
 6        .6                                      .6 
 7        .7                                      .7 
 8        .8000001                                .8000001 
 9        .9000001                                .9000001 
 10       1                                       1 
 11       1.1                                     1.1 
 12       1.2                                     1.2 
 13       1.3                                     1.3 
 14       1.4                                     1.4 
 15       1.5                                     1.5 
 16       1.6                                     1.6 
 17       1.7                                     1.7 
 18       1.8                                     1.8 
 19       1.9                                     1.9 
 20       2                                       2 
 21       2.1                                     2.1 
 22       2.2                                     2.2 
 23       2.3                                     2.3 
 24       2.4                                     2.4 
 25       2.5                                     2.5 
 26       2.6                                     2.6 
 27       2.7                                     2.7 
 28       2.799999                                2.799999 
 29       2.899999                                2.899999 
 30       2.999999                                2.999999 
 31       3.099999                                3.099999 
 32       3.199999                                3.199999 
 33       3.299999                                3.299999 
 34       3.399999                                3.399999 
 35       3.499999                                3.499999 
 36       3.599999                                3.599999 
 37       3.699999                                3.699999 
 38       3.799999                                3.799999 
 39       3.899998                                3.899998 
 40       3.999998                                3.999998 
 41       4.099998                                4.099998 
 42       4.199998                                4.199998 
 43       4.299998                                4.299998 
 44       4.399998                                4.399998 
 45       4.499998                                4.499998 
 46       4.599998                                4.599998 
 47       4.699998                                4.699998 
 48       4.799998                                4.799998 
 49       4.899998                                4.899998 
 50       4.999998                                4.999998 
 51       5.099998                                5.099998 
 52       5.199997                                5.199997 
 53       5.299997                                5.299997 
 54       5.399997                                5.399997 
 55       5.499997                                5.499997 
 56       5.599997                                5.599997 
 57       5.699997                                5.699997 
 58       5.799997                                5.799997 
 59       5.899997                                5.899997 
 60       5.999997                                5.999997 
 61       6.099997                                6.099997 
 62       6.199996                                6.199996 
 63       6.299996                                6.299996 
 64       6.399996                                6.399996 
 65       6.499996                                6.499996 
 66       6.599996                                6.599996 
 67       6.699996                                6.699996 
 68       6.799996                                6.799996 
 69       6.899996                                6.899996 
 70       6.999996                                6.999996 
 71       7.099996                                7.099996 
 72       7.199996                                7.199996 
 73       7.299995                                7.299995 
 74       7.399995                                7.399995 
 75       7.499995                                7.499995 
 76       7.599995                                7.599995 
 77       7.699995                                7.699995 
 78       7.799995                                7.799995 
 79       7.899995                                7.899995 
 80       7.999995                                7.999995 
 81       8.099995                                8.099995 
 82       8.199995                                8.199995 
 83       8.299995                                8.299995 
 84       8.399996                                8.399996 
 85       8.499996                                8.499996 
 86       8.599997                                8.599997 
 87       8.699997                                8.699997 
 88       8.799997                                8.799997 
 89       8.899998                                8.899998 
 90       8.999998                                8.999998 
 91       9.099998                                9.099998 
 92       9.199999                                9.199999 
 93       9.299999                                9.299999 
 94       9.4                                     9.4 
 95       9.5                                     9.5 
 96       9.6                                     9.6 
 97       9.700001                                9.700001 
 98       9.800001                                9.800001 
 99       9.900002                                9.900002 


奇怪的是:8,9行怎么是0.8000001  ,0.9000001,后面又正常了一段;
28-94又不正常,94-96正常,后面又不正常,
请大家帮我分析下其中的原因,谢谢。
--------------------编程问答-------------------- 谁叫你用single --------------------编程问答-------------------- DOUBLE类型也不对,小数也就2种形式.
你觉得用那种类型可以得到预计的效果,谢谢 --------------------编程问答-------------------- 这是浮点数运算的结果.
补充:VB ,  基础类
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,