Re: Comparing Long data types and Double data types

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Duane Bozarth (dp_bozarth_at_swko.dot.net)
Date: 07/14/04


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



Relevant Pages

  • Re: integrating exp(-x_1*x_2*...*x_s)
    ... > Why do you want to write a fortran routine to do this? ... I would like to have a routine that returns the exact value of this ... integration processes. ... absolute error i also need the exact value of the integral... ...
    (sci.math.num-analysis)
  • Re: Kann damit Leider nichts Anfangen- Problem liegt woanders!!
    ... "Karl Traunseider" schrieb: ... entschuldige Karl, aber Dir scheint die Bedeutung der Page_Load Routine ... > Schnitzel mit Pommes. ...
    (microsoft.public.de.german.entwickler.dotnet.asp)
  • Re: calling fortran from c++
    ... On looking at your C++ code, and considering that the Fortran routine ... which loads up the character arg with some string and returns. ...
    (comp.lang.fortran)
  • RE: source code for erfc with complex variable
    ... In a previous article, "Haifang Li" wrote: ... >I need a Fortran routine to evaluate the complementary error function erfc ... >anyone point me to a routine which can evaluate erfcwhere z is complex. ...
    (comp.lang.fortran)
  • wortweise durch Textmarke
    ... gibt es eine Routine, mit deren Hilfe man sich wortweise durch den gesamten Bereich einer Textmarke "hangelt" und beim letzen Wort aufhört? ... Karl ...
    (microsoft.public.de.word.vba)