-
Dec 20th, 2021, 01:25 PM
#1
NaN and Inf numbers for IEEE Singles in VB6
I needed this the other day, so I coded it up (IEEE Single Precision):
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
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
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 20th, 2021, 03:22 PM
#2
Re: NaN and Inf numbers for IEEE Singles in VB6
Btw, PtrAdd seems not used at all.
cheers,
</wqw>
Last edited by wqweto; Dec 20th, 2021 at 05:36 PM.
-
Dec 20th, 2021, 05:31 PM
#3
Re: NaN and Inf numbers for IEEE Singles in VB6
Originally Posted by wqweto
Btw, PtrAdd seems not to be used at all.
Yeah, I thought about stripping it out, but what the heck. I'm not sure what the rules are about where blocks of memory can be split, but I suspect it's far larger than 4 bytes.
Or, was I not even using it? Oh yeah, I'm not. I was using it in the "Double" stuff, and just didn't take it out. Oh well, that's a freebie.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 20th, 2021, 05:37 PM
#4
Re: NaN and Inf numbers for IEEE Singles in VB6
LOL :-))
Sorry, I fixed my post above.
cheers,
</wqw>
-
Dec 20th, 2021, 05:48 PM
#5
Re: NaN and Inf numbers for IEEE Singles in VB6
Interestingly, I also learned that NaN and Inf Singles will cast into Doubles without any problems, either explicitly (CDbl) or implicitly. But I guess that makes sense. I had just never thought about it until recently.
I haven't tested the vice-versa (NaN or Inf Double cast to a Single), but I'd be shocked if it didn't work.
EDIT: Yeah, it works just fine.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|