2

I am trying to rewrite some C code from another author into Igor Pro (similarish notation to C). The code is available here.

I don't know how to deal with the lines if ((umin+=input[k+1]-vmin)<minlambda) and else if ((umax+=input[k+1]-vmax)>lambda) with regards to the order of how they go about updating umin and umax, and how the if/elseif statements evaluating to true or false affect the update..

Specifically:

On lines 99-107 there is:

        if ((umin+=input[k+1]-vmin)<minlambda) {        
            do output[k0++]=vmin; while (k0<=kminus);
            vmax=(vmin=input[kplus=kminus=k=k0])+twolambda;
            umin=lambda; umax=minlambda;
        } else if ((umax+=input[k+1]-vmax)>lambda) {    
            do output[k0++]=vmax; while (k0<=kplus);
            vmin=(vmax=input[kplus=kminus=k=k0])-twolambda;
            umin=lambda; umax=minlambda;
        } else { /*blah blah */ }

I have refactored this to read:

    if ((umin+=input[k+1]-vmin) < minlambda) //Todo
        do
            output[k0] = vmin
            k0+=1
        while(k0 <= kminus)

        k=k0
        kminus=k
        kplus=kminus
        vmin=input[kplus]
        vmax = (vmin) + twolambda
        umin = lambda
        umax = minlambda

    elseif ((umax+=input[k+1]-vmax) > lambda) //Todo
        do
            output[k0]=vmax
            k0+=1
        while(k0 <= kplus)

        k=k0
        kminus=k
        kplus=kminus
        vmax=input[kplus]
        vmin = (vmax) - twolambda
        umin = lambda
        umax = minlambda        
    else //blah blah

Do umin and umax only get updated if their if statements evaluate to true? Or does it cascade? IF(umin) -> false, umin updated, ELSEIF(umax) -> true, umax updated, but IF(umin) -> true, umin updated, umax not updated? Or some other variant?

Another question about the same code.

Edit: fixed title. Added igor tag

Community
  • 1
  • 1
masher
  • 3,814
  • 4
  • 31
  • 35
  • 3
    Note that the expression isn't `a += b > c`, it's `(a += b) > c`, quite a different thing. – Some programmer dude Sep 22 '15 at 09:12
  • @joachim I fixed the title. – masher Sep 22 '15 at 09:17
  • in C and many C-like languages, [assignment operators return the value that is assigned to the variable](http://stackoverflow.com/q/3807192/995714). [What is the benefit of having the assignment operator return a value?](http://programmers.stackexchange.com/q/228851/98103) – phuclv Sep 22 '15 at 09:28
  • 1
    This is no valid C. Semicolons missing, no braces, C does not have `elseif`. Please post a [mcve] – too honest for this site Sep 22 '15 at 09:52
  • @olaf what isn't valid C? The first lot of code is a straight copy/paste. The second lot of code is in Igor as explained in my first paragraph. – masher Sep 22 '15 at 09:56
  • That's a terrible codding , please don't code like that. – Michi Sep 22 '15 at 09:58
  • @Michi it's not the OPs fault. The original code is downright disgusting and he's trying to recode it in another language – Tom Tanner Sep 22 '15 at 10:00
  • @michi what is terrible? The C code or the Igor code? – masher Sep 22 '15 at 10:00
  • It was just a suggestion to never write a code like that. – Michi Sep 22 '15 at 10:01
  • @masher: "similar to C" would be C++, or Java. This is far from "similar. Anyway: did I remove the C tag? Not yet! You are apparently asking about the behaviour of a **different** language and only use the C code as a starting point, So the C tag is irrelevant and you should add a tag for the target language. – too honest for this site Sep 22 '15 at 10:07
  • @olaf I am asking about the behaviour of the C code and how the if statements expand. I've already got one response that looks like it is written in VBA, but it is easy enough to understand. – masher Sep 22 '15 at 10:13
  • 1
    @masher Hard to say, because you said that you try to recode it. My question is why do someone try to recode something if he doesn't understand **if((a+=b) > c)**. This sounds strange to me. – Michi Sep 22 '15 at 10:24

4 Answers4

3
if( a += b > c)

In this first b>c is evaluated as > has higher precedence than += .

Then += will be evaluated. Now , if b>c is true then a will be a+=1 and if it is false then a+=0 will be evaluated .

Now this (as you updated your title)-

 if ((umin+=input[k+1]-vmin)<minlambda)  

In this first (umin+=input[k+1]-vmin) will be evaluated .Why ? due to brackets () having higher precedence than <.

In (umin+=input[k+1]-vmin) , due to precedence of - is higher than +=. input[k+1]-vmin is evaluated and then its result is added to umin and stored in umin.

After this evaluation it is compared with minlamda.

Similarly you can understand how this will work (ofcourse if condition in if is false) -

else if ((umax+=input[k+1]-vmax)>lambda) 

here also umax will be updated and then it will be compared with lambda.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

umin will be updated every time you enter there.
umax will be updated by (umax+=input[k+1]-vmax) > lambda if and only if (umin+=input[k+1]-vmin) < minlambda is false because it is in else if

a+=b > c works as if(b>c)a+=1; else a+=0;

(a+=b)>c works as (a+=b),a>c, which returns a>c after adding b to a.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

This:

for (;;) {
....
  if ((umin+=input[k+1]-vmin)<minlambda) {        
        do output[k0++]=vmin; while (k0<=kminus);
        vmax=(vmin=input[kplus=kminus=k=k0])+twolambda;
        umin=lambda; umax=minlambda;
    } else if ((umax+=input[k+1]-vmax)>lambda) {    
        do output[k0++]=vmax; while (k0<=kplus);
        vmin=(vmax=input[kplus=kminus=k=k0])-twolambda;
        umin=lambda; umax=minlambda;
    } else { /*blah blah */ }
}

(from the original source) is more or less equivalent to this

for (;;)
{
    ...
    umin += input[k + 1] - vmin;
    if (umin < minlambda) 
    {
        ...
        continue;
    }
    umax += input[k + 1] - vmax;
    if (umax > lambda)
    {
         ....
        continue;
    }
    /* blah blah */
}

You can do this because the if block is at the end of a loop, otherwise you'd need some else's and extra indenting which would be moderately less pretty (but probably still easier to understand).

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
  • OK. I might want to put a second conditional on the second if statement to ensure that the first evaluated to false. But I do get your reasoning. – masher Sep 22 '15 at 10:15
  • @masher you don't need to because of the continue at the end of the previous if block – Tom Tanner Sep 22 '15 at 10:22
0

You can look at it this way:

((umin+=input[k+1]-vmin)<minlambda)

is essentially

var umin += input[k+1] - vmin;
if (umin < minlambda) { }
else if ((umax += input[k+1] - vmax)> lambda) { }

umin is just calculated inside the if statement and then compared to minlambda

li0n_za
  • 455
  • 2
  • 15
  • So does that mean that umin is updated on every iteration and umax is only updated when if(umin) evaluates to false? – masher Sep 22 '15 at 09:20
  • Yes, exactly, else it would enter the if and skip the `else if umax` section. – li0n_za Sep 22 '15 at 09:26
  • In your example, umin and umax are updated regardless of the truthness of either if statement. I thought that the update cascaded, and umax was updated iff umin evaluated to false. – masher Sep 22 '15 at 09:41
  • I'm currently on the train, and such thinking is beyond me!@! – masher Sep 22 '15 at 09:50