Skip to content

Instantly share code, notes, and snippets.

@Greedquest
Last active July 7, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Greedquest/4e0b9cea0e183ddd28cdbcfd12b0885b to your computer and use it in GitHub Desktop.
Save Greedquest/4e0b9cea0e183ddd28cdbcfd12b0885b to your computer and use it in GitHub Desktop.
VBA get special double values like +- inf and NaN
Attribute VB_Name = "IEEE754"
'@Folder("Tests.Utils")
'Modified from https://stackoverflow.com/a/896292/6609896
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 Function GetIEEE754SpecialValue(ByVal value As abIEEE754SpecialValues) As Double
Select Case value
Case abIEEE754SpecialValues.abInfinityPos
GetIEEE754SpecialValue = BuildDouble(byt6:=240, byt7:=127)
Case abIEEE754SpecialValues.abInfinityNeg
GetIEEE754SpecialValue = BuildDouble(byt6:=240, byt7:=255)
Case abIEEE754SpecialValues.abNaNQuiet
GetIEEE754SpecialValue = BuildDouble(byt6:=255, byt7:=255)
Case abIEEE754SpecialValues.abNaNSignalling
GetIEEE754SpecialValue = BuildDouble(byt6:=248, byt7:=255)
Case abIEEE754SpecialValues.abDoubleMax
GetIEEE754SpecialValue = BuildDouble(255, 255, 255, 255, 255, 255, 239, 127)
Case abIEEE754SpecialValues.abDoubleMin
GetIEEE754SpecialValue = BuildDouble(255, 255, 255, 255, 255, 255, 239, 255)
End Select
End Function
Private Function BuildDouble( _
Optional ByVal byt0 As Byte = 0, _
Optional ByVal byt1 As Byte = 0, _
Optional ByVal byt2 As Byte = 0, _
Optional ByVal byt3 As Byte = 0, _
Optional ByVal byt4 As Byte = 0, _
Optional ByVal byt5 As Byte = 0, _
Optional ByVal byt6 As Byte = 0, _
Optional ByVal byt7 As Byte = 0 _
) As Double
Dim bdTmp As ByteDouble
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
Dim tdRtnVal As TypedDouble
LSet tdRtnVal = bdTmp
BuildDouble = tdRtnVal.value
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment