Right or Wrong the Gambas Math on Var1 -= var2 + 1

Post

Posted
Rating:
#1 (In Topic #1478)
Regular
sergioabreu is in the usergroup ‘Regular’
Hello

 A bug on a program made me bring this to discussion:
 I expected to subtract something from a variable, and then adding 1 to the value, but Gambas behaved like there were parentheses giving me a surprising might wrong result!

 I suspect - I know I can be wrong too - that this Math operation can be a Gambas hidden bug:

 

Code

x -= y + 1  ' Without parentheses!
If I didn'd use parentheses, so I would expect that Gambas did:
 

Code

 x -= y ' FIRSTLY
' AND THEN
 x = x + 1
 
  For instance, 3 -= 2 + 1  could be 2 but gambas gives me 0 !

The problem is this "auto parentheses" gives a DIFFERENT result ! It behaves as the code were:

Code

x -= (y + 1)
That means: FIRSTLY sum (y to 1) and then do the x auto subtraction.

Do you think that   X -= Y + 1 could be correct to be done as  (X -=Y)  first and then + 1 too ??
After all, parentheses exist to force what we desire to do first…

Open to ideas…
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
sergioabreu is in the usergroup ‘Regular’
This forced me to NOT USE self subtraction and change the code from

Code

X -= Y + 1  ' That is elegant

' ... to :

X = X - Y + 1   ' In order to work!
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned

sergioabreu said

Do you think that   X -= Y + 1 could be correct to be done as  (X -=Y)  first and then + 1 too ??
After all, parentheses exist to force what we desire to do first…

Open to ideas…


Absolutely NOT!

It works how it works.

+ - etc are arithmetic operators  /cat/arithop - Gambas Documentation
-= += etc are assignment operators.  /cat/assignop - Gambas Documentation

with an "assignment" the right hand side is calculated first then assigned to the left.

There is also an evaluation order /cat/evalorder - Gambas Documentation

I do not understand it all fully, I just deal with it as it is.

maybe it does not fit in with what you expect but what about the thousands of gambas programs out there you would break by changing something like how that works?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Quincunxian is in the usergroup ‘Blogger’
 BOMDAS.
Brackets, Of, Multiplication, Addition, Subtraction.
This is the common mathematical hierarchical evaluation of any equation.
so in x -= 2+1 both side of the equals sign are evaluated before the -= or +=  evaluation is executed.
-= or += are seen as the last process of the equation so BOMDASE if you like.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Yes, the assignment is always the last thing to be done.
Consider, in pseudo assembler x-=y+1

Code

LDA 1
ADA @"Y" (or in fact any previously set value)
DEC RA
STA "X"
or, in fact consider any command that performs an assignment..

Code

mything += do_this ( do_that ( do_the_other ( to_something )))  / 100.0
I would never have expected that would decrement the first RHS term before the division. Would you?

Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
sergioabreu is in the usergroup ‘Regular’
Thansk to all

I was just WONDERING, and I confess I didn't know that -= is the last thing to be evaluated, since it APEARS first on the LEFT position

I think that without parentheses should behave as the NATURAL order of the expression because….

Code

A  - B + C
'is different of
A - (B+C)
'just because of these parentheses
'the Natural evaluation order (HUMANLY SEEING) would be
' (A - B)
'then + C
'and IF I WANTED to sum first, I COULD/SHOULD add parentheses

<COLOR color="#800040">But OK, I understood that it is "how it works". Self assignment is NOT seen as  a normal math operation </COLOR>

THANKS TO ALL, I knew I could be wrong, I am just a dreamer. I will then use parentheses or:

Code

X = X - Y + 1
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
sergioabreu is in the usergroup ‘Regular’

thatbruce said

Yes, the assignment is always the last thing to be done.
Consider, in pseudo assembler x-=y+1

Code

LDA 1
ADA @"Y" (or in fact any previously set value)
DEC RA
STA "X"
 
Division is not pesent in the ORIGINAL question (ONLY subtraction and sum). So please don't insert multiplication or division in the discussion since it is NOT related with the EQUATION presented.

My "hipotetic" Assembly to follow the natural order would be:
Lets use A and B that are real registers:
Ax = 3
Bx = 2
Ax -= Bx + 1

Code

MOV Ax, 3
MOV Bx, 2
SUB Ax, Bx   '  subtracts Bx from Ax
ADD Ax, 1
Since the Assembler does only TWO operands by time, never analyse 3 at the same time. The parentheses in SUM would produce:
Ax = 3
Bx = 2
Ax -= (Bx + 1)

Code

MOV Ax, 3
MOV Bx, 2
ADD Bx, 1    '  Just because of the parentheses
SUB Ax, Bx   '  subtracts Bx from Ax

<COLOR color="#408000">In fact -= and += PUT ghost PARENTHESES in the equation. Now I UNDERSTAND the behavior. THANKS !!</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#8
Banned

sergioabreu said

Thansk to all

I was just WONDERING, and I confess I didn't know that -= is the last thing to be evaluated, since it APEARS first on the LEFT position

I think that without parentheses should behave as the NATURAL order of the expression because….
(snip)

<COLOR color="#800040">But OK, I understood that it is "how it works". Self assignment is NOT seen as  a normal math operation </COLOR>

THANKS TO ALL, I knew I could be wrong, I am just a dreamer. I will then use parentheses or:

Code

X = X - Y + 1

But don't use parenthesis on the assignment.
Both of these lines are the same but equally wrong

Code (gambas)

  1. (X -= Y) + 1
  2. (X = X - Y) + 1
  3.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.