I needed this the other day, so I coded it up (IEEE Single Precision):
I had already done it for IEEE Doubles, found here. That thread also has a bit more context you may enjoy reading.
Just FYI, I often use the "f" as a Hungarian prefix to denote Single precision (I think "float").
Enjoy,
Elroy
Code:
Option Explicit
'
Public Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
'
Public Function fNaN() As Single
' Math (add, subtract, multiply, divide) can be done on these, but nothing changes.
' They can NOT be used in "if fNaN = fNaN Then", or an overflow will result. Use fIsNaN().
GetMem4 &H7FFFFFFF, fNaN
End Function
Public Function fInf() As Single
GetMem4 &H7F800000, fInf
End Function
Public Function fIsNaN(ByVal f As Single) As Boolean
fIsNaN = fIsNanOrInf(f) And Not fIsInf(f)
End Function
Public Function fIsInf(f As Single) As Boolean
Dim i As Long
GetMem4 f, i
i = i And &H7FFFFFFF ' Make sure it's positive.
fIsInf = i = &H7F800000
End Function
Public Function fIsNeg(f As Single) As Boolean
' This works even on fNaN and fInf.
Dim i As Long
GetMem4 f, i
fIsNeg = (i And &H80000000) <> 0&
End Function
Public Function fIsNanOrInf(f As Single) As Boolean
Dim i As Long
GetMem4 f, i
i = i And &H7F800000 ' Strip off sign bit and the entire fraction part.
fIsNanOrInf = i = &H7F800000
End Function
Public Function PtrAdd(ByVal Ptr As Long, ByVal iOffset As Long) As Long
' For adding (or subtracting) a small number from a pointer.
' Use PtrAddEx for adding (or subtracting) large numbers from a pointer.
PtrAdd = (Ptr Xor &H80000000) + iOffset Xor &H80000000
End Function
Just FYI, I often use the "f" as a Hungarian prefix to denote Single precision (I think "float").
Enjoy,
Elroy