如何在 VBA 中从 IEEE-754 64 位(双精度)浮点数中提取尾数、指数和符号数据?谢谢
Edit(约翰科尔曼评论后)。在发布原始问题之前,我已经四处寻找解决方案,但只能找到如何在 C 中执行此操作(例如使用具有位字段的结构)。找不到 VBA 的任何内容。我尝试过使用 VBA 的位运算符(即 AND、OR、NOT、XOR),但这似乎没有给出预期的结果。例如,以单精度 IEEE 32 位浮点数表示的 1 表示为
0 01111111 00000000000000000000000
其中第一位用于符号,接下来的 8 位用于(有偏差的)指数,最后 23 位用于尾数。将 NOT 应用于 1 应该返回
1 10000000 11111111111111111111111
十进制为 -3.9999998,但 VBA 中的以下代码返回 -2,表示为
1 10000000 00000000000000000000000
x = Not 1!
Debug.Print x
我没有看到在我的 OP 中发布此内容的意义。
我想我已经找到了做到这一点的方法。以下功能DoubleToBin
返回表示 IEEE-754 双浮点数的 64 位字符串。它使用 VBA“技巧”通过组合来传递原始数据,而不使用 API 例程(例如 MemCopy (RtlMoveMemory))LSet
具有相同大小的用户定义类型。一旦我们有了位串,我们就可以从中提取所有组件。
Type TDouble
Value As Double
End Type
Type TArray
Value(1 To 8) As Byte
End Type
Function DoubleToArray(DPFloat As Double) As Variant
Dim A As TDouble
Dim B As TArray
A.Value = DPFloat
LSet B = A
DoubleToArray = B.Value
End Function
Function DoubleToBin(DPFloat As Double) As String
Dim ByteArray() As Byte
Dim BitString As String
Dim i As Integer
Dim j As Integer
ByteArray = DoubleToArray(DPFloat)
For i = 8 To 1 Step -1
j = 2 ^ 7
Do While j >= 1
If (ByteArray(i) And j) = 0 Then
BitString = BitString & "0"
Else
BitString = BitString & "1"
End If
j = j \ 2
Loop
Next i
DoubleToBin = BitString
End Function
这里是如何运作的——我现在接受我自己的答案吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)