bugGNU Octave - Bugs: bug #46397, eval: The evaluated code should...

 
 

bug #46397: eval: The evaluated code should not affect last result

Submitter:  Oliver Heimlich <oheim>
Submitted:  Sat 07 Nov 2015 12:14:23 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Wont Fix Assigned to:  None
Originator Name:  Oliver Heimlich Open/Closed:  * Closed
Release:  * 4.0.0 Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 20 Mar 2018 02:05:54 PM UTC, comment #4: 

Just for some clarity on eval behaviour differences.

MATLAB

>> clear; eval('12;'); % result: ans=12
>> clear; eval('x=12;'); % result: x=12 ans=not exist
>> clear; y=eval('12;'); % result: y=12 ans=not exist
>> clear; y=eval('x=12;'); % result: Error: The expression to the left of the equals sign is not a valid target for an assignment.


Octave

>> clear; eval('12;'); % result: ans=12
>> clear; eval('x=12;'); % result: x=12 ans=not exist
>> clear; y=eval('12;'); % result: y=12 ans=12
>> clear; y=eval('x=12;'); % result: y=12 x=12 ans=not exist


It's not clear to me why Octave's behaviour is superior enough that is worth making an incompatibility.

Ceral Paquet <octavebugs>
Sat 02 Sep 2017 08:28:56 PM UTC, comment #3: 

Agree with "Won't Fix"
Someone might file a bug report to Mathworks on their
behavior.

Michael Godfrey <godfrey>
Group Member
Sat 02 Sep 2017 12:54:55 PM UTC, comment #2: 

I'm marking this as "Won't Fix".  It is a Matlab incompatibility, but it also makes more sense to me to kee it this way.  'eval' runs code that can have side effects.  For example, you can bring new variables into existence with


eval ('x = 2;')


It makes sense to me that a successful evaluation of code is the same as if the code had been typed, or run from a script, in which case the ans variable would be updated.



Rik <rik5>
Group administrator
Sat 07 Nov 2015 03:07:20 PM UTC, comment #1: 

The following algorithm should do the trick:

1. Store the state of `ans` in the target context (caller or base).

2. Evaluate the script.

3. Use the current state of `ans` in the target context as the return value for eval (or evalin).

4. Reset the state of `ans` in the target context to the value stored in step 1.

Oliver Heimlich <oheim>
Sat 07 Nov 2015 12:14:23 PM UTC, original submission:  

When evaluating code with `eval`, this also affects what will be returned by the function `ans` afterwards. In Matlab this is not the case.

The function `evalin` is also affected.


11
x = eval ('12')

`ans` returns 12 in Octave and 11 in Matlab.

13
x = eval ('14;')

`ans` returns 14 in Octave and 13 in Matlab.

15
x = eval ('16');

`ans` returns 16 in Octave and 15 in Matlab.

17
x = eval ('18;');

`ans` returns 18 in Octave and 17 in Matlab.


Oliver Heimlich <oheim>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by octavebugs (Posted a comment)
  • -email is unavailable- added by godfrey (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by oheim (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-09-02 rik5 StatusNone Wont Fix
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code