[Blitzmax 1.50] incorrect precedence order for self assign operators?

Community Forums/Bug Reports/[Blitzmax 1.50] incorrect precedence order for self assign operators?

col(Posted May) [#1]
Local offset:float = 4.0
Local correct:float = 2.0
Local wrong:float = 2.0

correct = correct * 0.5 + offset
wrong :* 0.5 + offset

Print correct
Print wrong


There may be a valid reason why the self assign operator is evaluated after the assigned value expression?


Floyd(Posted May) [#2]
My first thought was that 'wrong :* 0.5 + offset' was not valid code and would not compile.

In C n++ would add 1 to n. But n++ is also an expression which has a value, the value of n after the increment. ( On second thought n++ is post increment. The increment takes place after the expression is evaluated. )
I was reading your code as (wrong :* 0.5) + offset which I thought would be invalid, assuming wrong :* 0.5 does not have a value. It just alters the variable wrong.

The BlitzMax Language Reference has a section on expressions. But I think the self assign feature was added to the language later and the documentation was never updated. So that doesn't help.

Apparently BlitzMax is expecting wrong :* expression, where everything after :* is the expression.

Here is a different look at the issue, attempting to use n :+ 1 as if it had a value.

n = 2
Print n
Print n :+ 1



col(Posted May) [#3]
Yeah I just looked through the source ( here and here). The right hand expression is applied to the operator as this...

a :* 5 + offset         ' multiply assign statement with expression of '5 + offset'
     ^^^^^^^^^^

a = a * 5 + offset      ' 1 expression made up of * and + expressions assigned to 'a'
    ^^^^^^^^^^^^^^


Ultimately ':operator' is treated as a statement.


TomToad(Posted May) [#4]
Under Help/Language/Variables/Assigning Variables

Once declared, a variable's value may be changed using an assignment statement:

Variable = Expression

You can also perform 'modifying' assignments, which are shorthand for Variable = Variable Operator Expression. The syntax for modifying assignments is:


I would assume that Expression referred to everything right of the assign operator, so
wrong :* 0.5 + offset would be interpreted as wrong = wrong * (0.5 + offset)


EdzUp MkII(Posted June) [#5]
seems the glitch is the :* operator is this a becomes thing like MX2 has :=?

I know in the past its supposed to be the same as blah = blah * but maybe it was changed somewhen


TomToad(Posted June) [#6]
The operator is functioning correctly. Notice that the description says Variable = Variable Operator Expression, it performs the operation on the entire expression on the right, not just the first term.

It is intended to behave just like c++ *= operator
#include <stdio.h>

int main()
{
	float offset = 4.0;
	float correct = 2.0;
	float wrong = 2.0;
	
	correct = correct * 0.5 + offset;
	wrong *= 0.5 + offset;
	
	printf("%f\n%f",correct,wrong);
}

outputs
5.000000
9.000000
same as BlitzMAX


col(Posted June) [#7]
And there was me thinking it was just shorthand representation, but it indeed does make a difference in the way the calculation is performed.