|
查看: 1195|回复: 4
|
vb6 费解的问题
[复制链接]
|
|
|
我用了vb6 有 n年了,今天才发现这个问题:
Dim a As Long
a = 20000 + 20000
error "Overflow" 会出现。
有谁知道是什么问题吗? |
|
|
|
|
|
|
|
|
|
|
发表于 21-1-2009 07:11 PM
|
显示全部楼层
试试 a=32768 + 2
如果一样的问题,那应该是 32bits limit for Long type |
|
|
|
|
|
|
|
|
|
|
发表于 21-1-2009 07:40 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 25-1-2009 01:32 PM
|
显示全部楼层
这好像是vb6的bug。。。
如果你的计算中,第一和第二或者之后的都是integer type(16 bits),那么计算结果也是integer type。。。所以,20000 + 20000 = 40000,就出现overflow。。。最后,vb 返回给 a 的答案就是 overflow。。。- Private Sub Form_Load()
- On Error GoTo myerror
- Dim a As Long
- a = 32767 + 1 '= 32768 (+ve integer limit, overflow error)
- a = 32768 + 1 '= 32769 (ok, because the 1st parameter already long type)
- a = 32767& + 1 '= 32768 (ok, because we force the 1st parameter to long type)
- a = 32767 + CLng(1) '= 32768 (ok, because we force the 2nd parameter to long type)
- a = 32766 - 1 + 2 '(ok)
- a = 32766 + 2 - 1 'Overflow error
- 'Overflow, because 32766 + 2 > 32767, even
- 'the above calculation also result in 32767
- Exit Sub
- myerror:
- MsgBox "Runtime error!!!, " & Err.Description
- Resume Next
- End Sub
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 11-2-2009 01:25 PM
|
显示全部楼层
回复 4# meemee 的帖子
应该java也是这样,
因为程式
1。A = B + C
是重右边运行起,然后存到左边要符合
Integer = Integer
Long = Long
2。数字全部默认为Integer (如果少过32767)
3。当Integer + Integer = Integer
当Integer + Long = Long (或相反)
 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|