Assignment and the history operator

Feb 3, 2012 at 6:04pm

Assignment and the history operator

I encountered this and found it a bit weird:

reset = in3;
History the_phase(0);

if (reset>0) {
// This doesn’t work:
// the_phase = 0;
// This does
the_phase = the_phase*(reset==0);
}

It seems that for the history operator to do assignment it has to involve itself. (even though these statements are logically equal) It was really confusing the first several times I tried to use codebox.

The alternative, I’m guessing, is to have a temporary variable do all the heavy lifting and then do the assignment at the end.

Is there a better way of doing this/reason for why it is the way it is? (I’m fairly sure there’s a good reason, and I’d like to follow best/better practices…)

#61601
Feb 3, 2012 at 6:12pm

As a follow-up, I tried the local variable approach and encountered the following problem. These two statements should be logically equivalent, but the first one doesn’t work, and the second one does. I’m guessing because it has the history value earlier?

// This doesn’t work:
if (reset!=0) {
tmp_phase = 0;
} else {
tmp_phase = the_phase;
}

// This DOES work:
if (reset==0) {
tmp_phase = the_phase;
} else {
tmp_phase = 0;
}

I know that I could write it as:
tmp_phase = the_phase*(reset==0);

and this always works, but I’d like to use the conditionals sometimes where I have more things happening during the boolean.

#222358
Feb 3, 2012 at 6:48pm

Conditional statements (if/else) and iteration statements (for/while) are in the GenExpr grammar but not implemented yet. Use the ? operator for this kind of behavior for now.

#222359

You must be logged in to reply to this topic.