gpt4 book ai didi

vb6 - 你如何让 VB6 用 +infinity、-infinity 和 NaN 初始化 double ?

转载 作者:行者123 更新时间:2023-12-04 04:45:20 26 4
gpt4 key购买 nike

VB6 似乎并没有让将 +infinity、-infinity 和 NaN 存储到双变量中那么容易。如果可以的话,这会有所帮助,这样我就可以在复数的上下文中与这些值进行比较。如何?

最佳答案

一些不同的事情。正如您从 Pax 的示例中看到的,您实际上只需要查找 IEEE 754 标准,然后将您的字节插入正确的位置。我要给你的唯一警告是 MicroSoft has deprecated RtlMoveMemory因为它有可能产生溢出类型的安全问题。作为替代方案,您可以在“纯”VB 中使用用户定义类型和 LSet 进行一些仔细的强制转换来完成此操作。 (另请注意,有两种类型的 NaN。)

Option Explicit

Public Enum abIEEE754SpecialValues
abInfinityPos
abInfinityNeg
abNaNQuiet
abNaNSignalling
abDoubleMax
abDoubleMin
End Enum

Private Type TypedDouble
value As Double
End Type

Private Type ByteDouble
value(7) As Byte
End Type

Public Sub Example()
MsgBox GetIEEE754SpecialValue(abDoubleMax)
End Sub

Public Function GetIEEE754SpecialValue(ByVal value As abIEEE754SpecialValues) As Double
Dim dblRtnVal As Double
Select Case value
Case abIEEE754SpecialValues.abInfinityPos
dblRtnVal = BuildDouble(byt6:=240, byt7:=127)
Case abIEEE754SpecialValues.abInfinityNeg
dblRtnVal = BuildDouble(byt6:=240, byt7:=255)
Case abIEEE754SpecialValues.abNaNQuiet
dblRtnVal = BuildDouble(byt6:=255, byt7:=255)
Case abIEEE754SpecialValues.abNaNSignalling
dblRtnVal = BuildDouble(byt6:=248, byt7:=255)
Case abIEEE754SpecialValues.abDoubleMax
dblRtnVal = BuildDouble(255, 255, 255, 255, 255, 255, 239, 127)
Case abIEEE754SpecialValues.abDoubleMin
dblRtnVal = BuildDouble(255, 255, 255, 255, 255, 255, 239, 255)
End Select
GetIEEE754SpecialValue = dblRtnVal
End Function

Public Function BuildDouble( _
Optional byt0 As Byte = 0, _
Optional byt1 As Byte = 0, _
Optional byt2 As Byte = 0, _
Optional byt3 As Byte = 0, _
Optional byt4 As Byte = 0, _
Optional byt5 As Byte = 0, _
Optional byt6 As Byte = 0, _
Optional byt7 As Byte = 0 _
) As Double
Dim bdTmp As ByteDouble, tdRtnVal As TypedDouble
bdTmp.value(0) = byt0
bdTmp.value(1) = byt1
bdTmp.value(2) = byt2
bdTmp.value(3) = byt3
bdTmp.value(4) = byt4
bdTmp.value(5) = byt5
bdTmp.value(6) = byt6
bdTmp.value(7) = byt7
LSet tdRtnVal = bdTmp
BuildDouble = tdRtnVal.value
End Function

最后一个旁注,您也可以通过这种方式获得 NaN:
Public Function GetNaN() As Double
On Error Resume Next
GetNaN = 0 / 0
End Function

关于vb6 - 你如何让 VB6 用 +infinity、-infinity 和 NaN 初始化 double ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com