chaining compound assignment results in undefined behavior

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

chaining compound assignment results in undefined behavior

Seyyed Hossein Sajjadi
Hi

There is a problem with chaining compound assignment.
For example the expression a=1;a+=a+=4 will result 10.
I think such a behavior is borrowed from C language but this behavior
is known as "undefined behavior".
 This issue have been resolven in Java.
 Java language guarantees that the operands of operators appear
to be evaluated in a specific evaluation order , namely, from left to
right. §15.7
Other specifications about evaluation of expressions are in
"The Java® Language Specification"

 also this approach is followed by c# language
 so result of above expression in Java is 6

Reply | Threaded
Open this post in threaded view
|

Re: chaining compound assignment results in undefined behavior

Jordi Gutiérrez Hermoso-2
On Fri, 2014-06-06 at 17:20 +0330, Hossein Sajjadi wrote:
> There is a problem with chaining compound assignment.
> For example the expression a=1;a+=a+=4 will result 10.
> I think such a behavior is borrowed from C language but this behavior
> is known as "undefined behavior".

Yes, but this is not undefined behaviour in Octave. "Undefined
behaviour" is a specific term defined by the C and C++ standards.
There is no Matlab or Octave standard, so there is no definition of
"undefined behaviour" either.

>  This issue have been resolven in Java.

I don't think there is any particular reason to do whatever Java does
here. Octave's behaviour here seems to be another sensible thing to
do. Normally = is right-associative, so I see no reason for += to be
left-associative.

- Jordi G. H.



Reply | Threaded
Open this post in threaded view
|

Re: chaining compound assignment results in undefined behavior

Richard Crozier
On 06/06/2014 17:33, Jordi Gutiérrez Hermoso wrote:
> On Fri, 2014-06-06 at 17:20 +0330, Hossein Sajjadi wrote:

>
>>   This issue have been resolven in Java.
>
> I don't think there is any particular reason to do whatever Java does
> here. Octave's behaviour here seems to be another sensible thing to
> do. Normally = is right-associative, so I see no reason for += to be
> left-associative.
>
> - Jordi G. H.
>


I would certainly be a lot more surprised by what Java does than what
Octave does. I would expect assignments to happen right-to-left.

Richard


--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


Reply | Threaded
Open this post in threaded view
|

Re: chaining compound assignment results in undefined behavior

Seyyed Hossein Sajjadi
In reply to this post by Jordi Gutiérrez Hermoso-2
> Yes, but this is not undefined behaviour in Octave. "Undefined
> behaviour" is a specific term defined by the C and C++ standards.
> There is no Matlab or Octave standard, so there is no definition of
> "undefined behaviour" either.

 I don't see source of the interpreter but if the interpreter do
 exactly what C do undefined bahavior  may be transfered from c to
 octave.In the other word the expression a=1;a+=a+=4 may be evaluated 6
 sometimes or may be evaluated 10 in other situations such as change of
 compiler or multi thread execution ...

Reply | Threaded
Open this post in threaded view
|

Re: chaining compound assignment results in undefined behavior

Jordi Gutiérrez Hermoso-2
On Fri, 2014-06-06 at 20:53 +0330, Hossein Sajjadi wrote:
>  the expression a=1;a+=a+=4 may be evaluated 6 sometimes or may be
>  evaluated 10 in other situations such as change of compiler or
>  multi thread execution ...

In C++, yes. Not in Octave. The result is always going to be 10 in
Octave. There is no concept of "undefined behaviour" in the Octave
language.

- Jordi G. H.