Android Question Again problems with 64-Bit-Integers

W. Graf

Member
Licensed User
Longtime User
Hi!

I don't want to annoy you. But I'm still having big problems with the 64-bit-integers in B4A.
Please try following code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim q As Long
   Dim q2 As Long
   Dim q3 As Long

   q = 7782220156096217002
   q2 = 2
   q3 = q / q2

   Log (q3)
   Return
End Sub

The result should be: 3891110078048108501 (you can test it in Windows-calculator).
But B4A/Java returns: 3891110078048108544 (many bits were set to 0 ?! ...:

110101111111111111111111111111111111111111111111111111110101010 = 7782220156096217002

011010111111111111111111111111111111111111111111111111111010101 = 3891110078048108501 = CORRECT (Windows-calculator)

011011000000000000000000000000000000000000000000000000000000000 = 3891110078048108544 = WRONG!!! (B4A / Java)


Does Java cannot handle 64-bit-integers? Or is it a bug in B4A (but B4A "only" translates the BASIC-syntax to Java - it does not do any calculations, right?).

The problem occurs in Debug- and Release-mode, on the emulator and on a physical device.

Normally I would say, that I'm doing something wrong, because if this is a real bug, then many people would be affected. But then I think, the code above is very short and really simple. What can I do wrong in these few lines?!
In the moment, I need the division to shift bits in a 64-Bit-Integer. But generally my trustfulness to Java is decreasing ... :-(

If you have any ideas, I am looking forward to read it. Thank you very much in advance!
Nice greetings,
Wolfgang
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
but B4A "only" translates the BASIC-syntax to Java - it does not do any calculations, right?
B4A does many many things. It could have skipped the Java code if there were significant advantages for skipping it.

Division in B4A is always made with doubles. There is no "integer division". You can use BigNumbers to avoid these issues.
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Hello and good morning!

Thank you all very much for your replies and advices!

So you mean, the only way is to use the BigNumbers-library, right? I tried to use the builtin-64-bit-integer ("long") from the core-library (less dependencies) but if this is not possible, then I have to use the library "BigNumbers".

But this also means, that I never may use the 64-bit-integer ("long") datatype in B4A, because this datatype is computing wrong results, correct? Or do you know a situation, where 64-bit-long-integers are reliable?
Why does B4A use the double-datatype for the divison? Is there no native support for 64-bit-integers (like in C++)?

Thank you and have a good start in this new week.
BR Wolfgang
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use long variables. The division operator doesn't do an integer division. This means that it returns a double. Otherwise the result of 5 / 3 was 1.

Adding support for integer division:
B4X:
Sub Process_Globals

End Sub

Sub Globals
   Private meJO As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Log(IntegerDivision(7782220156096217002, 2))
End Sub

Sub IntegerDivision(n1 As Long, n2 As Long) As Long
   If meJO.IsInitialized = False Then meJO.InitializeContext
   Return meJO.RunMethod("integerDivision", Array(n1, n2))
End Sub

#if JAVA
public static long integerDivision(long n1, long n2) {
   return n1 / n2;
}
#end if
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Hi Erel,

this is a very important information (long-division returns double) for future use. Thx!

Your code (Integer-Division via Java-inline) helped me and it works very good!! :)
Thank you for your help! Have a great day!

Nice greetings,
Wolfgang
 
Upvote 0
Top