Variable addition in macro - treats variable as strings...?

I have a simple gSender macro, but simple addition seems to be giving issues - it concatenates the values as strings rather than summing them… gSender Edge 1.3.0 and gSender Live 1.2.2, both on Apple.

Macro to illustrate this, and the console log resulting follows. Machine is at X zero (G54) position to start;

; Manually set some variables, then sum them - this works
%VAR1 = -10.2
%VAR2 = -5.2
(Var1: [VAR1])
(Var2: [VAR2])
(Subt: [VAR1-VAR2])
(Adds: [VAR1+VAR2])
%VAR3 = VAR1-VAR2
%VAR4 = VAR1+VAR2
(Sub: [VAR3])
(Add: [VAR4])
; Use %posx system relative macro to populate variables, then sum them - this DOESN’T work
; The subtract works OK, the sum appears to concatenate the numbers as string.
%POS1 = posx
G0 X1
%wait
%POS2 = posx
(1: [POS1])
(2: [POS2])
%SUM = POS1+POS2
(Sum: [SUM])

Output when run:

ok
feeder (Var1: -10.2)
ok
feeder (Var2: -5.2)
ok
feeder (Subt: -4.999999999999999)
ok
feeder (Adds: -15.399999999999999)
ok
feeder (Sub: -4.999999999999999)
ok
feeder (Add: -15.399999999999999)
ok
feeder G0 X1
ok
feeder G4 P0.5
ok
feeder (1: 0.000)
ok
feeder (2: 1.000)
ok
feeder (Sum: 0.0001.000)
ok

There has to be a simple solution to this… but I cannot seem to figure out what! Why do variables not sum correctly if they were populated from the %posx system variable?
Tested in Live 1.2.2 and Edge 1.3.0 and get the same, curious result. Also posted as an Issue in GitHub

Weird…
Can’t test now, but try:

...
%POS1 = Number(posx)
G0 X1
%wait
%POS2 = Number(posx)
...

as a workaround.

1 Like

@NeilFerreri Success, thank you. I modified my test macro to use Number() and addition now works correctly, in both Live 1.2.2 and Edge 1.3.0.

Manual:

%VAR1 = -10.2
%VAR2 = -5.2
%VAR3 = VAR1-VAR2
%VAR4 = VAR1+VAR2
(Sub: [VAR3])
(Add: [VAR4])

%POS1 = Number(posx)
G0 X1
%wait
%POS2 = Number(posx)
(P1: [POS1])
(P2: [POS2])
%POS3 = POS1-POS2
%POS4 = POS1+POS2
(Sub: [POS3])
(Add: [POS4])

Output:

ok
feeder (Sub: -4.999999999999999)
ok
feeder (Add: -15.399999999999999)
ok
feeder G0 X1
ok
feeder G4 P0.5
ok
feeder (P1: 0)
ok
feeder (P2: 1)
ok
feeder (Sub: -1)
ok
feeder (Add: 1)
ok

1 Like

We’re going to be looking at what might be causing this behind the scenes - as far as we’re aware all position variables should already be being run through Number, so wondering what is going on when they are then stored in variables.

2 Likes

We’ve made some changes to the expression parser to perform a check if a expression variable looks numberish - if it is it’ll be converted explicitly to a Number type so any maths that need to be run won’t accidentally turn into strings. This will appear in the next Edge release, with it coming to Main shortly after.

2 Likes

This certainly appears to be fixed in Edge 1.3.1, thank you.

3 Likes