How do I declare MAX_DOUBLE in VB6?
根据VB6的MSDN帮助
Floating-point values can be expressed as mmmEeee or mmmDeee, in which mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type.
现在在VB6 IDE中,我尝试输入此
1 | const MAX_DOUBLE as Double = 1.79769313486232D+308 |
但是,一旦我离开该行,IDE就会引发错误6(溢出)
An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. ...
那么如何定义MAX_DOUBLE(以及与此相关的MIN_DOUBLE)?
编辑:
解决了!
1 | Const test As Double = 1.79769313486231E+308 + 5.88768018655736E+293 |
仔细检查到二进制级别,该级别应尽可能高。您可以继续添加诸如1等的值,但它会产生一个等于但不大于的数字。
输出是这样的:
01111111 | 11101111 | 11111111 | 11111111 | 11111111 | 11111111 | 11111111 | 11111111
确实是DoubleMax
旧:
您可以使用正无穷大。
它必须是Const吗?通过使用Byte数组中的CopyMemory设置正确的位模式,可以将MAX_DOUBLE的确切值获取到变量中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Private Declare Sub CopyMemory Lib"kernel32" Alias"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Dim Max As Double Dim Idx As Long Dim Bits(0 To 7) As Byte For Idx = 0 To 5 Bits(Idx) = 255 Next Bits(6) = 239 ' = 11101111 Bits(7) = 127 For Idx = 0 To 7 CopyMemory ByVal VarPtr(Max) + Idx, Bits(Idx), 1 Next Debug.Print Max |
编辑:我忘记了您也询问了MIN_DOUBLE,它甚至更简单。
1 2 3 4 5 6 7 | Dim Min As Double Dim Bits As Byte Bits = 1 CopyMemory ByVal VarPtr(Min), Bits, 1 Debug.Print Min |
务实的解决方法:稍微减少数量。
1 | Const MAX_DOUBLE As Double = 1.79769313486231E+308 |
我想\\在大多数情况下就足够了。
在数字中使用" E "作为指数,而不是如下所示的" D "。
1 | Public Const MAX_DOUBLE = 1.79769313486232E+308 |
[编辑]
在下面查看此链接,滚动到底部。这个具体的代码示例显示了如何使用此构造。希望这会有所帮助。
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22555684.html