Strange behaviour of += operator

Depending on the naming of variables and the space character after the += operator do I get errors:

Example 1: name of variable numi

grafik

Line 4 produces an error:
grafik

Example 2: name of variable i

grafik

This doesn’t produce any error.

I’ve the feeling that I missed something really stupid. However, I couldn’t figure it out…

Thanks a lot for your answers and your great piece of software.

My system

  • OS: Ubuntu 20.04.4 LTS
  • Octave version: GNU Octave, version 7.1.0T
1 Like

I can confirm this. It works for i and j but not any other variable name, which sounds like a bug. That is probably a side effect of the change requiring the ++ and – operators to be next to their variable with no space in between. But it should not play favorites with variable names like that, allowing i and j but not the other names.

Could you submit a bug report at Savannah please? GNU Octave - Bugs: Browse Items [Savannah]

As a workaround, please use consistent spacing around your operators for now.

EDIT: I submitted it to Savannah, here: GNU Octave - Bugs: bug #62552, Operator spacing works for i and j... [Savannah]

I can reproduce this in a more general case at the command prompt (GUI and CLI):

a = 1
b = 2
a+b
a + b
a +b

results in

ans = 3
ans = 3
error: a used as variable and later as function

Thanks a lot for your considerations, documentation and bug report. As far as I’ve understood, this “strange” behavior results from the desire being compatible with Matlab. Thanks for having that goal, even some things are not optimal, but I was really surprised, how simple it was for me once to run a quite a bit of Matlab code simply on Octave. Thanks again for your outstanding work.

So for me, it’s all ok.

2 Likes