Comparison Operator, '<> Not equal to', did not work on my macros

On Sheet1, summation of range B8:B13 is 1104.61 and summation of range C8:C13 is 1104.61 on Macro Sub TestEqualOperatorOnSheet1 .
Why did If DrASOE2 <> CrASOE2 Then (line 16) return true ?

Sub	TestEqualOperatorOnSheet1
	Dim oSheet As Object
	Dim iASOE2, rASOE2, rrASOE2 As Integer
	Dim DrASOE2, CrASOE2 As Double
	DrASOE2 = 0
	CrASOE2 = 0
	oSheet = ThisComponent.Sheets.getByName("Sheet1")
	ThisComponent.CurrentController.setActiveSheet(oSheet)
	For 	iASOE2 = 1 To 20
		If	UCase(oSheet.getCellByPosition(0, iASOE2).String) = "NEW" Then
			rrASOE2 = rASOE2
			rASOE2 = iASOE2
MsgBox DrASOE2 & chr(10) & CrASOE2 & chr(10) & iASOE2,, "DrASOE2" & chr(10) & "CrASOE2"  & chr(10) & "iASOE2" 
			If 	DrASOE2 <> CrASOE2 Then
				MsgBox "Total debit amount is not equal to credit amount in batch of row " & rrASOE2 + 1 & ".",,"Error"
				Stop
			End If
			DrASOE2 = 0
			CrASOE2 = 0
			DrASOE2 = DrASOE2 + oSheet.getCellByPosition(1, iASOE2).Value
			CrASOE2 = CrASOE2 + oSheet.getCellByPosition(2, iASOE2).Value
		Else
			DrASOE2 = DrASOE2 + oSheet.getCellByPosition(1, iASOE2).Value
			CrASOE2 = CrASOE2 + oSheet.getCellByPosition(2, iASOE2).Value
		End If
	Next
MsgBox DrASOE2 & chr(10) & CrASOE2,, "DrASOE2" & chr(10) & "CrASOE2"			
MsgBox "Total debit amount is equal to credit amount in every batch."	
End Sub

It also happened with Sub TestEqualOperatorOnSheet2 .
And it also happened with Sub TestEqualOperatorOnSheet3 if I placed the same range on Sheet3.

Please click on menu Test LibreOffice Calc BASIC to run macros.

LOCalcBASIC_EqualOperator.ods (33.0 KB)

LibreOffice:
Version: 7.3.7.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.7-0ubuntu0.22.04.1
Calc: threaded

OS:
Ubuntu 22.04

Probably they are not equal, bit-wise, it only appears so in the formatted display string. Don’t test for (non-)equal with binary floating point values of calculation results. If you’re interested in monetary values then compare only values rounded to two decimals.

If Round(DrASOE2,2) <> Round(CrASOE2,2) Then

For further information on accuracy and precision of IEEE 754 binary double floating point values see 0.1 + 0.2 ≠ 0.3.

1 Like

It’s also possible to check the difference of the two values to be lower than a given very small “epsilon” value.

Dim Epsilon As Double
Epsilon = 0.000001  'very small possible delta
If (Abs(DrASOE2 - CrASOE2) < Epsilon) Then ...
'DrASOE2 and CrASOE2 can be considered as equal
End If
2 Likes