Right or Wrong the Gambas Math on Var1 -= var2 + 1
Posted
#1
(In Topic #1478)
Regular

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! Code
x -= y ' FIRSTLY
' AND THEN
x = x + 1
The problem is this "auto parentheses" gives a DIFFERENT result ! It behaves as the code were:
Code
x -= (y + 1)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…
Posted
Regular

Code
X -= Y + 1 ' That is elegant
' ... to :
X = X - Y + 1 ' In order to work!
Posted
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?
Posted
Expert


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
I code therefore I am
Posted
Regular

Consider, in pseudo assembler x-=y+1
Code
LDA 1
ADA @"Y" (or in fact any previously set value)
DEC RA
STA "X"
Code
mything += do_this ( do_that ( do_the_other ( to_something ))) / 100.0
Posted
Regular

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
Posted
Regular

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.thatbruce said
Yes, the assignment is always the last thing to be done.
Consider, in pseudo assembler x-=y+1Code
LDA 1
ADA @"Y" (or in fact any previously set value)
DEC RA
STA "X"
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
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>
Posted
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)
- (X -= Y) + 1
- (X = X - Y) + 1
1 guest and 0 members have just viewed this.



