Android Question Bug?

RB Smissaert

Well-Known Member
Licensed User
Noticed that setting a Boolean based on a only slightly complex calculation can give the wrong result:

Problem with this code:

B4X:
 'see if we need vertical buttons
 If iMinimumTotalButtonWidth > Parent.Width - 10dip Then
  bVerticalButtons = True
 Else
  'see if we need buttons with different widths
  Log("Show, total button width with uniform buttons: " & iTotalButtonWidthWithUniformButtons)
  Log("Show, Parent.Width: " & Parent.Width)
  If 10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip > Parent.Width Then
   bNonUniformButtonWidths = True
  End If
 End If

Fine if I do this:

B4X:
 'see if we need vertical buttons
 If iMinimumTotalButtonWidth > Parent.Width - 10dip Then
  bVerticalButtons = True
 Else
  'see if we need buttons with different widths
  iTotalButtonWidthWithUniformButtons = 10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip
  Log("Show, total button width with uniform buttons: " & iTotalButtonWidthWithUniformButtons)
  Log("Show, Parent.Width: " & Parent.Width)
  bNonUniformButtonWidths = iTotalButtonWidthWithUniformButtons > Parent.Width
 End If

Is this a bug or am I missing something?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
You should create a small project with the non-working code and upload it.

I will, but this is very simple, in the first code example bNonUniformButtonWidths can be set to True, when it should be False.
All the variables in both code blocks are exactly the same. As far as I can see the resulting Boolean bNonUniformButtonWidths
should therefor be the same in both codes.

RBS
 
Upvote 0

agraham

Expert
Licensed User
It looks like an operator precedence problem to me, try parenthesising the left hand expression.
B4X:
  If (10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip) > Parent.Width Then
   bNonUniformButtonWidths = True
  End If
I don't know what the exact order of precedence is in BA.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
It looks like an operator precedence problem to me, try parenthesising the left hand expression.
B4X:
  If (10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip) > Parent.Width Then
   bNonUniformButtonWidths = True
  End If
I don't know what the exact order of precedence is in BA.

Will try that, but logically it shouldn't make a difference.
Only the parentheses around iButtonsToAdd - 1 are needed.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
You should create a small project with the non-working code and upload it.

I can't reproduce this is in the most simple demo project, but I think agraham is pointing in the right direction:

This produces the right Boolean value (False):

B4X:
If (10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip) > Parent.Width Then
bNonUniformButtonWidths = True
End If

Whereas this gives the wrong Boolean value (True):

B4X:
If 10dip + iWidestButtonWidth * iButtonsToAdd + (iButtonsToAdd - 1) * 5dip > Parent.Width Then
bNonUniformButtonWidths = True
End If

The only difference between the above 2 code blocks is the added brackets.
No idea why I can't reproduce this, maybe because the real app takes up more memory or maybe because
the above 2 codeblocks in the real app run in a ResumableSub and the demo app had no ResumableSub (will try that).

RBS
 
Upvote 0
Top