Floating point comparison in VB6

What's the best way to test two singles for equality in VB6?

I want to check two single values ​​for equality up to 7 significant digits.

This MSDN article recommends using something like

If Abs(a - b) <= Abs(a / 10 ^ 7) Then
    valuesEqual = True
End If

However, this may be unsuccessful for certain values, for example.

Public Sub Main()

    Dim a As Single
    Dim b As Single

    a = 0.50000005
    b = 0.50000014

    Debug.Print "a = " & a
    Debug.Print "b = " & b
    Debug.Print "a = b: " & (a = b)
    Debug.Print "SinglesAreEqual(a, b): " & SinglesAreEqual(a, b)

    // Output:
    // a = 0.5000001
    // b = 0.5000001
    // b = b: False
    // SinglesAreEqual(a, b): False

End Sub

Private Function SinglesAreEqual(a As Single, b As Single) As Boolean

    If Abs(a - b) <= Abs(a / 10 ^ 7) Then
        SinglesAreEqual = True
    Else
        SinglesAreEqual = False
    End If

End Function

The easiest way to get the result I need is to convert the values ​​to strings, but it seems terribly ugly:

Private Function SinglesAreEqual(a As Single, b As Single) As Boolean

    SinglesAreEqual = (Str$(a) = Str$(b))

End Function

Are there any better ways?

+3
source share
3 answers

CAD/CAM, . , fComp, , . fComp , . . , .

fComp , , . , .

Public Function pRound(ByVal Value As Double, ByVal Power As Double) As Double
    Dim TempValue As Double
    Dim tSign As Double
    TempValue = Value
    tSign = TempValue
    TempValue = Abs(TempValue)
    TempValue = TempValue * 10 ^ (Power * -1)
    TempValue = Fix(TempValue + 0.5)
    TempValue = TempValue / 10 ^ (Power * -1)
    pRound = TempValue * Sign(tSign)
End Function

,

RoundedNumber = pRound (MyValue, -6)

, .

+3

, ,

If Abs(a - b) < 0.000001 Then

0.000001

+2

I do not believe that you can use a data type singlefor many significant digits. Instead, you will need to use double:

Dim a As Single
Dim s As String

s = "0.50000005"
a =  0.50000005

Debug.Print s & " " & a

The above outputs:

0.50000005
0.5000001
+2
source

Source: https://habr.com/ru/post/1763608/


All Articles