如何在 VB6 中声明 MAX_DOUBLE?

发布于 2024-07-22 18:57:16 字数 571 浏览 10 评论 0原文

根据VB6的MSDN帮助

浮点值可以表示为 mmmEeee 或 mmmDeee,其中 mmm 是尾数,eee 是指数(10 的幂)。 Single 数据类型的最大正值为 3.402823E+38,即 3.4 乘以 10 的 38 次方; Double 数据类型的最大正值是 1.79769313486232D+308,即大约 1.8 乘以 10 的 308 次方。 使用 D 分隔数字文字中的尾数和指数会导致该值被视为 Double 数据类型。 同样,以相同的方式使用 E 会将值视为单一数据类型。

现在,在 VB6 IDE 中,我尝试输入此内容

const MAX_DOUBLE as Double = 1.79769313486232D+308

,但是,一旦我离开该行,IDE 就会抛出错误 6(溢出)

当您尝试进行的分配超出了分配目标的限制时,就会发生溢出。 ...

那么我如何定义 MAX_DOUBLE (和 MIN_DOUBLE )?

According to the MSDN help for VB6

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.

Now in the VB6 IDE I've tried to enter this

const MAX_DOUBLE as Double = 1.79769313486232D+308

however, as soon as I move away from that line the IDE throws an Error 6 (Overflow)

An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. ...

So how do I get MAX_DOUBLE (and MIN_DOUBLE for that matter) defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

未蓝澄海的烟 2024-07-29 18:57:16

编辑:
解决了!

Const test As Double = 1.79769313486231E+308 + 5.88768018655736E+293

仔细检查二进制级别,这应该是你能达到的最高水平。 您可以继续添加 1 等值,但它会产生一个等于而不大于的数字。
输出是这样的:
01111111|11101111|11111111|11111111|11111111|11111111|11111111|11111111
这确实是 DoubleMax

Old:
您可以只使用 正无穷大

Edit:
Solved it!

Const test As Double = 1.79769313486231E+308 + 5.88768018655736E+293

Double checked it down to the binary level, that should be as high as you can go. You can keep adding values like 1 etc but it yields a number equal to, not greater than.
Output is this:
01111111|11101111|11111111|11111111|11111111|11111111|11111111|11111111
Which is indeed DoubleMax

Old:
You could just use Positive infinity.

早茶月光 2024-07-29 18:57:16

它必须是一个常量吗? 您可以通过使用 Byte 数组中的 CopyMemory 设置正确的位模式,将 MAX_DOUBLE 的准确值获取到变量中。

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,这更容易。

Dim Min As Double
Dim Bits As Byte

Bits = 1
CopyMemory ByVal VarPtr(Min), Bits, 1

Debug.Print Min

Does it have to be a Const? You can get the exact value of MAX_DOUBLE into a variable by setting the correct bit pattern using CopyMemory from a Byte array.

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

Edit: I forgot that you also asked about MIN_DOUBLE, which is even easier.

Dim Min As Double
Dim Bits As Byte

Bits = 1
CopyMemory ByVal VarPtr(Min), Bits, 1

Debug.Print Min
魔法少女 2024-07-29 18:57:16

明显的实用解决方法:稍微减少数量。

Const MAX_DOUBLE As Double = 1.79769313486231E+308

我想这在大多数情况下就足够了。

Obvious pragmatic workaround: reduce the number slightly.

Const MAX_DOUBLE As Double = 1.79769313486231E+308

I imagine that'll be adequate in most situations.

披肩女神 2024-07-29 18:57:16

使用“E”表示数字中的指数,而不是像下面这样的“D”。

Public Const MAX_DOUBLE = 1.79769313486232E+308

[编辑]

看看下面的这个链接,滚动到底部。 这个具体的代码示例展示了如何使用这个构造。 希望这有帮助。

http://www.experts-exchange.com /Programming/Languages/.NET/Visual_Basic.NET/Q_22555684.html

Use an "E" for exponent in the number instead of a "D" like this below.

Public Const MAX_DOUBLE = 1.79769313486232E+308

[edit]

Take a look at this link below, scroll to the bottom. This concrete code example shows how this construct is being employed. Hopefully this helps.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22555684.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文