in reply to Re^31: Why is the execution order of subexpressions undefined? (magic ruts)
in thread Why is the execution order of subexpressions undefined?

Parenthesis are strictly for the parser. Parenthesis do not create lists, and they do not create sequence points. Parenthesis define precedence. In:
(f(a) + g(b)) * h(c)
the run-time is free to calculate g(b), h(c), f(a) in that order.

IMO, it should even be free to not calculate f(a) or g(b) if it finds h(c) to be 0. But that's just me.

Replies are listed 'Best First'.
Re^33: Why is the execution order of subexpressions undefined? (magic ruts)
by TimToady (Parson) on Apr 18, 2005 at 15:43 UTC
    What if f(a) or g(b) returns Inf?
      Then, IMO, the expression should still return 0.
        But of course, your humble opinion is in the miniority. Inf*0 should be NaN.
Re^33: Why is the execution order of subexpressions undefined? (magic ruts)
by BrowserUk (Patriarch) on Apr 19, 2005 at 12:44 UTC

    What if f(), g() and h() get their values from arrays (or external sources) in lockstep? Ie. each call returns the next value from it's respective source.

    The compiler arbitrarially opts to not call some of those functions when one of them returns 0, all subsequent calculations are wrong because the sequences are no longer in step. Is 0 a second class number?

    So now the only way to avoid the compiler subverting what the programmer know he needs is for the programmer to play hostage to the compiler and code:

    my $temp1 = f(a); my $temp2 = g(b); my $temp3 = h(c); my $result = ($temp1 + $temp2) * $temp3;

    You call that an optimisation? That is so ludicrous as to be laughable, except it is where thing stand right now--and, on the basis of majority opinion in this thread, the way it is likely to be in the future. No that is laughable!


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      So now the only way to avoid the compiler subverting what the programmer know he needs is for the programmer to play hostage to the compiler and code
      Well, either that or avoid side-effects in your functions. Is that really so much to ask? You do realize you should try to minimize the use of side-effects in general, right? Just like you should limit your use of global variables and gotos?
        Assignment is a side-effect. And it's hard to write a useful Perl program that doesn't do assignment.

        Any form of IO is a side-effect, regardless of where you do it.

        In an imperative language, 'functions' (procdures that return values if you prefer) are the major mechanism of code structuring. When I do IO to a database, I call a function (or method (same difference). When I read or write to a file, I call a function. When I read or write to a socket udp/tcp/http/ftp/telnet/et al, it is all done with functions and all of those functions that do IO have side-effects. f(a) + g(b) * h(c)

        f(a) retrieves the range from the radar.

        g(b) retrieves a correction factor from the wind speed indicator.

        h(c) gets a scalar from a lookup table in memory. keyed upon the type of shell (HE, tracer, AP) that will load next from the belt.

        The final result is used to adjust the elevation of the barrel in real time.

        How do you avoid the side effects?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?
          A reply falls below the community's threshold of quality. You may see it by logging in.
      Replace + with ||, and * with &&.

      Still ludicrous?

        No. Logical operators short circuit, but you do not do associative and communicative math with logical operators.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?