Android Question equality and inequality operators with b4a long type

b4xfr

Member
Licensed User
B4X:
Dim along As Long = 40000000000000100
Dim along2 As Long = 40000000000000000
	
		If (along = along) Then
			Log("along is equal to itself")
		else
			Log("along is not equal to itself")
		End If
		
	
		If (along2 = along2) Then
			Log("along2 is equal to itself")
		else
			Log("along2 is not equal to itself")
		End If

this code output :
along is not equal to itself
along2 is equal to itself

I don't understand what is my mistake. If someone can explain me, thank you very much.
 

Star-Dust

Expert
Licensed User
Longtime User
Simple, buy a new device, I've tried your code on an old Android 4.0 and it works perfectly

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
along is equal to itself
along2 is equal to itself
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I copied and pasted your code into a new project and it gives the expected results.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tried it too, works as expected.

upload_2017-11-14_14-9-39.png
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Inequality confirmed on Lenovo K6 (Android 7.0) and Equality on K10000 (Android 6). Si it really depends on the device
 
Upvote 0

b4xfr

Member
Licensed User
thank you very much for a so fast answer.
I use a real device and android 7.0 for testing. The problem could be that i use VirtualBox. I will investigate more.
 
Upvote 0

b4xfr

Member
Licensed User
I have tested the code in release mode. And it work as expected. So the problem appears only in debug mode.

B4X:
Dim along As Long = 40000000000000100
     Dim along2 As Long = 40000000000000000
   
     If (along = along) Then
       ToastMessageShow("along is equal to itself",False)
       Log("along is equal to itself")
     Else
       ToastMessageShow("along is not equal to itself",False)
       Log("along is not equal to itself")
     End If
     
   
     If (along2 = along2) Then
       ToastMessageShow("along2 is equal to itself",False)
       Log("along2 is equal to itself")
     Else
       ToastMessageShow("along2 is not equal to itself", False)
       Log("along2 is not equal to itself")
     End If
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
It appears only in rapid debug (not legacy) mode, not in Activity_Create, when comparing local vars (not if they are globally declared), and only if they differ in the latest digits

So it "could seem" as if the comparison block logic was performed not with the numbers themselves but with their string representation, but only taking into account up to a given length?
Just a guesso_O

EDIT:
After doing a "Project Clean" it works ok
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
As a rule of thumb, you can do it (clean project) as a check if you see "strange(r) things"
With legacy debugger it doesn't happen, but it's not as fast as the rapid debugger
 
Upvote 0

b4xfr

Member
Licensed User
With legacy debugger it doesn't happen, but it's not as fast as the rapid debugger
In fact I have only one debug mode. I don't know how to activate the "legacy debugger".
compilation mode.png

I will avoid if (a=b) and use preferably if (a-b=0), if (a-b)<>0, it worked before i made a "clean project".
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is indeed a bug here. It happens in some cases in rapid debug mode (default debugger).

The rapid debugger has two pipelines. One is fast and static and the other is slower and more dynamic / sophisticated. This is why it can be tricky to reproduce it.

It can be reproduced with:
B4X:
Dim along As Long = 40000000000000100
Log(along = along)
Log("put breakpoint here")

I guess that it is related to a conversion between double to long.

I recommend you to keep using the rapid debugger. You can put this code in a different sub. It will run properly as long as you don't modify the code. Clean the project if you do modify it to force a full compilation.
 
Upvote 0

b4xfr

Member
Licensed User
Ok.
Thank you very much. Indeed i can see that with the break point there is a latency before a log appear (with a false result) in the console.

The following code works well. In my point of view, this code will acces the value of the long and compute a difference, and my ancient code behaves as if it tested two different objects. A long versus a Long.

B4X:
Dim along As Long = 40000000000000100
   Log(along - along = 0)
   Log("put breakpoint here")
 
Upvote 0
Top