Re: Comparing Long data types and Double data types
From: Duane Bozarth (dp_bozarth_at_swko.dot.net)
Date: 07/14/04
- Next message: Andrew Faust: "Re: export access to sql server"
- Previous message: Joe: "RE: Running in IDE and using Executable provide different results"
- In reply to: Karl E. Peterson: "Re: Comparing Long data types and Double data types"
- Next in thread: Duane Bozarth: "Re: Q: Comparing Long data types and Double data types"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 14 Jul 2004 14:14:40 -0500
"Karl E. Peterson" wrote:
...snip prelude to question for comparison algorithm for reals...
> Anyone have a nice/elegant/efficient routine for this?
OK, Karl, I've translated a version of the Fortran routine I posted a
day or so ago that is in VB. The Fortran intrinsic SPACING is simpler
to get than I remembered. This comparison routine is valid specifically
for Singles, but the Spacing routine returns values for either Singles
or Doubles. Packaging isn't the greatest, but it seems to be correct
from the <VERY> limited testing I did...I stuck on the Max function that
was needed to compile/test...
Perhaps someone will find it useful...the constants are specific for
IEEE R*4 and R*8, of course.
Function Spacing(val As Variant) As Variant
Const sngTiny As Single = 2 ^ -126
Const dblTiny As Double = 2 ^ -1022
Dim x As Double
Dim e As Long, p As Long
Select Case VarType(val)
Case vbSingle
If val = 0! Then
Spacing = sngTiny
Exit Function
End If
p = 24
e = Int(Log(Abs(val)) / Log(2#)) + 1
Spacing = CSng(2 ^ (e - p))
Case vbDouble
If val = 0# Then
Spacing = dblTiny
Exit Function
End If
p = 53
e = Int(Log(Abs(val)) / Log(2#)) + 1
Spacing = CDbl(2 ^ (e - p))
End Select
End Function
Function Compare_Float(ByVal x As Single, _
ByVal y As Single, _
Optional ByVal ulp As Long) As Boolean
Dim rel As Single
rel = 1!
If (Not IsMissing(ulp)) Then
rel = CSng(Abs(ulp))
End If
Compare_Float = Abs(x - y) < (rel * Spacing(Max(Abs(x), Abs(y))))
End Function
Function Max(x As Single, y As Single)
If x > y Then
Max = x
Else
Max = y
End If
End Function
- Next message: Andrew Faust: "Re: export access to sql server"
- Previous message: Joe: "RE: Running in IDE and using Executable provide different results"
- In reply to: Karl E. Peterson: "Re: Comparing Long data types and Double data types"
- Next in thread: Duane Bozarth: "Re: Q: Comparing Long data types and Double data types"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|