IEEE-754 32位单精度浮点数计算VB源码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VB IEEE-754 32位单精度浮点数计算源码

Option Explicit

Private Function GetData(TmpHex As String) As String

Dim TmpBin As String

Dim TmpMi As Integer

On Error Resume Next

TmpBin = HexToBin(TmpHex)

Label1.Caption = TmpBin & " 长度" & Len(TmpBin) & "位,第1位1为负数,0为正数"

TmpMi = BinToOct(Mid(TmpBin, 2, 8)) - 127

GetData = Round(BinToOct("1." & Mid(TmpBin, 10, 23)) * (2 ^ TmpMi), 6)

If Left(TmpBin, 1) = "1" Then GetData = "-" & GetData

End Function

Private Function HexToBin(TmpHex As String) As String

Dim n As Integer

Dim I As Integer

Dim TmpBin As String

On Error Resume Next

For n = 1 To Len(TmpHex)

I = Val("&H" & Mid(TmpHex, n, 1))

TmpBin = ""

While I > 0

TmpBin = CStr(I Mod 2) & TmpBin

I = I \ 2

Wend

HexToBin = HexToBin & Right("0000" & TmpBin, 4)

Next n

End Function

Private Function BinToOct(TmpBin As String) As Double

Dim n As Integer

Dim TmpS() As String

On Error Resume Next

TmpS = Split(TmpBin, ".")

For n = 1 To Len(TmpS(0))

If Mid(TmpS(0), n, 1) = "1" Then BinToOct = BinToOct + (2 ^ (Len(TmpS(0)) - n)) Next n

If UBound(TmpS) = 1 Then

For n = 1 To Len(TmpS(1))

If Mid(TmpS(1), n, 1) = "1" Then BinToOct = BinToOct + (2 ^ (-1 * n)) Next n

End If

End Function

相关文档
最新文档