2 = 2.66?

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I've been looking and looking at the code below and can't figure out what I'm doing wrong, so I'm wondering if it could somehow be an error in B4A.

The array NumTopCards() is an Integer.
avgSpds is a Double.
As you can see in the Logs Tab, NumTopCards(p, Spd) = 2 and avgSpds = 2.666, so the single-stepper should not get past the first line of code to the Log line and the highlighted line, but it obviously does????


Int compared Double wrong.jpg




I created the following test app and it performed as expected.
B4X:
Dim i As Int
Dim j As Double
i = 2
j = 2.1
If j > i Then
   Msgbox("j is bigger","")
End If
 

kickaha

Well-Known Member
Licensed User
Longtime User
Looks like it is the way numbers are dealt with internally (I remember seeing a post by Erel on that, but do not remember the details).

Try this code to better see what is happening - although it does not show why!
B4X:
Dim i As Int
Dim j As Double
Dim k As Double

i = 2
j = 2.1
k = i

If j > i Then
   Msgbox("j is bigger","")
End If
   
If i >= j Then
   Msgbox("i is bigger","")
End If

If k >= j Then
   Msgbox("is i still bigger - even as k","NO it is not")
End If

As a workround, use math
B4X:
If (i - j) >= 0 Then
   Msgbox("i is bigger","Not using math!")
End If
 
Last edited:

agraham

Expert
Licensed User
Longtime User
This looks like a subtle compiler bug to me

For some reason the compiler is casting the Double to an Int when comparing

//BA.debugLineNum = 49;BA.debugLine="If i = j Then";
if (_i== (int)(_j)) {

Rather than just comparing them and letting the Java compiler promote the Int to a Double which happens if you reorder the variables in the comparison..

//BA.debugLineNum = 49;BA.debugLine="If j = i Then";
if (_j==_i) {


@Erel - surely this is not intended behaviour?
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Just a question, in your code you test with:
AllQty(p, p, Spd)

But in the Log() you look at:
AllQty(p, AllOthers, Spd)

Is this really what you are looking for ?

Yes, but thanks for asking.

The top line of the code shown is
Else If NumTopCards(p, Spd) >= avgSpds) _
and not shown is that avgSpds is
AllQty(p, AllOthers, Spd) / numWithSpds,
which is why I was Logging those two variables.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
This is a bug and it will be fixed in the next update. As agraham wrote you should change the order of variables and start with the double variable.

Thanks agraham and erel, and while I'm at it, if you notice in the screen shot in my original post, I get these apparently random red bars up and down the scroll bar. Are these glitches, or do they indicate something?
 
Top