Binary Operator Rant


  • ♿ (Parody)

    @too_many_usernames said:

    NEGATION is not the same as SUBTRACTION

    I'm not sure what you mean.  Negation of a number can be defined by the equation

    -x = 0-x

    which is inherently subtraction (it's also inherently multiplication by negative one).

    Sure, but there's no negation involved when I write "-5". The minus sign is part of the number as expressed in text, just like the digit is (or a decimal point is). The context matters here, is the point. Like wise, when writing "-5^2", there is no negation. There are two numbers and an exponentiation operator. Negation requires some other operation (which could simply be taking the value of an expression inside of parentheses), which you'd get if you wrote:

    -(5^2) or -(5)

    But you didn't.



  • @Zecc said:


    var Module = {};
    (function(Module){
    // Set stuff into Module..
    }).call(Module, Module);

     

    For those of you not following, what this does (inline) is apply an anonymous function to an object, Module, which will be 'this' inside the anonymous function. While we're at it, the same object is also passed as the first argument to the function, so it can also be known as "Module" inside it.

    And just to state it explicitly for those wanting to know, the upshot is that you can use temporary variables or whatever else you need and not worry about polluting to parent's namespace; everything stays anonymous except for what you explicitly want named. I use that construct "all the time" as well for the same purpose.

  • ♿ (Parody)

    @Zecc said:

    I usually write it like this though:

     


    var Module = {};
    (function(Module){
    // Set stuff into Module..
    }).call(Module, Module);

     

    For those of you not following, what this does (inline) is apply an anonymous function to an object, Module, which will be 'this' inside the anonymous function. While we're at it, the same object is also passed as the first argument to the function, so it can also be known as "Module" inside it.

    Ah. I guess tosh's args in invocation was confusing me, since he didn't have anything in the function prototype. And I'm not that familiar with javascript.


  • @Zecc said:

    @dhromed said:

    @Zecc said:

    with( {blah1: 1} ){
    alert( blah1 );
    };

    alert( blah1 );

     

    Wow.

    Yeah, it was a real eye-opener for me too. But apparently with is deprecated. I won't miss it.

     

    I didn't read all the posts, sorry. But I want to chime in to say "with" is deprecated in the same way that "<img>" XHTML2 Strict is deprecated... only in the super-strict mode nobody uses, in the super-new version nobody uses, and probably nobody will ever implement because it breaks compatibility. For all practical purposes, it's not deprecated at all.

    The real shame is that JavaScript could easily fix the scope problems with "with" by requiring the leading period when referring to object members:

    Current JS:

    var i = 1;
    var blah = new Object();
    blah.j = 1;
    
    with( blah )
    {
     i = 5; // Oops! blah doesn't contain an i, so this modifies the function-scope i!
    }

    Easily-fixed JS:

    var i = 1;
    var blah = new Object();
    blah.j = 1;
    
    with( blah )
    {
     .j = 5; // Period indicates j is an object member, no scope confusion!
     i = 5; // i has no period, indicating function scope-- still no scope confusion!
    }

    I always get annoyed at people who prefer removing something handy to fixing it. "with" is handy; removing it is a mistake. Fixing it would be a boon.



  • Ok people, I have realized that I have not been communicating well. Here's an attempt to resolve that.  Forget about the fact that the single minus sign is in front of a string literal and put in a variable instead:

    In cell a1 type = 3+2

    In cell a2 type = -a1^2.

    Excel spits out 25.

    In this case, it's not a minus sign in front of a number which may be confused with just treating it as a negative value.

    I don't know what math you folks learn, but the expression

    =-x^2

    should be a negative number for all real nonzero values of x, and Excel doesn't do this.

     



  • @too_many_usernames said:

    Ok people, I have realized that I have not been communicating well. Here's an attempt to resolve that.  Forget about the fact that the single minus sign is in front of a string literal and put in a variable instead:

    In cell a1 type = 3+2

    In cell a2 type = -a1^2.

    Excel spits out 25.

    In this case, it's not a minus sign in front of a number which may be confused with just treating it as a negative value.

    I don't know what math you folks learn, but the expression

    =-x^2

    should be a negative number for all real nonzero values of x, and Excel doesn't do this.

     

    All that means is that Excel's OOO does exponents "^" before negation "-"... doesn't sound too shocking or weird to me, frankly.



  • @too_many_usernames said:

    I don't know what math you folks learn, but the expression

    =-x^2

    should be a negative number for all real nonzero values of x, and Excel doesn't do this.

     

    I don't know what math(s) you learnt, but that means nothing mathematically. It's not a mathematical expression because it doesn't have anything on the other side of the equality.

    You still seem to be having trouble with the simple concept that - is a negation operator as well as a minus sign. Excel defines their order of precedence (correctly) so that negation comes before anything else. What you gave means (to Excel): negate a1, then square it. If you use =0-a1^2, you get the answer you're expecting.



  • @blakeyrat said:

    All that means is that Excel's OOO does exponents "^" before negation "-"... doesn't sound too shocking or weird to me, frankly.
    Other way around. Basically, there're two perfectly reasonable options for what -a1^2 means - either -(a1a1) or (-a1)(-a1). Neither is 'better' than the other.



  • =-(A1)^2
    =-A1^2
    =-(A1^2)
    =0-A1^2

    This result in 25, 25, -25, -25. The first one does in fact confuse me, the other 3 pretty much give what i had expected.

    And i'm not even sure if the first one is that unexpected. Basicly Excell sees a - as a minus, unless the - is written in a position where it would otherwise be a syntax error (try to replace it with a * or /). In that case it takes it as a unary function.



  • @Dorus said:

    =-(A1)^2
    =-A1^2
    =-(A1^2)
    =0-A1^2

    This result in 25, 25, -25, -25. The first one does in fact confuse me, the other 3 pretty much give what i had expected.

    And i'm not even sure if the first one is that unexpected. Basicly Excell sees a - as a minus, unless the - is written in a position where it would otherwise be a syntax error (try to replace it with a * or /).In that case it takes it as a unary function.

    Of course, in practice this is all moot. Using brackets which are strictly superfluous is a good thing because readability. You should (almost) always use the form (-a1)^2 purely because that makes it obvious at first glance what you're doing.



  • @intertravel said:

    You still seem to be having trouble with the simple concept that - is a negation operator as well as a minus sign. Excel defines their order of precedence (correctly) so that negation comes before anything else. What you gave means (to Excel): negate a1, then square it. If you use =0-a1^2, you get the answer you're expecting.
     

    Since when is "negation highest precendence" correct? I can't find any reference that claims that - only the contrary in fact.

    It seems, though, there is surprisingly much debate on this matter: see http://mathforum.org/library/drmath/view/61523.html for a good description which says why you always expand variables with parentheses to avoid ambiguity (this should sound familiar to anyone familiar with macro expansion, by the way).  They give a good explanation as to how "-x^2" where x=3 expands to "-(3)^2" not "-3^2".  To make it even more clear, consider if x = a+b; you would expand to "-(a+b)^2, not "-a+b^2".

    Also: both Wolfram Alpha and Google both also think that the string "-5^2" evaluates to -25.  Google even goes so far as to show you it turns that into "-(5^2)" to make the precedence conversion clear: that the minus sign, in fact, is a multiplication.

  • ♿ (Parody)

    @intertravel said:

    Of course, in practice this is all moot. Using brackets which are strictly superfluous is a good thing because readability. You should (almost) always use the form (-a1)^2 purely because that makes it obvious at first glance what you're doing.

    Of course, you're correct in the general sense. But if someone did that specifically, then he is TRWTF.



  • I did some digging on the "-a1^2" (stepping through Excel disasembly is always fun)...I have about an 80% certainty level that the internals are even stranger than the visibile. The expression analyzer actually appears to be considering it

     (-1)^2 * a1^2

    Which is mathematically equivilant to (-a1)^2, but reveals that negation is considered (-1*....) raher than (0-....) [both being qually valid]

     Of course, this has nothing to do with  considering it as -(a1^2)


  • Discourse touched me in a no-no place

    @too_many_usernames said:

    Since when is "negation highest precendence" correct? I can't find any reference that claims that - only the contrary in fact.
    http://support.microsoft.com/kb/q132686/

    Last Review: August 15, 2005

    The order of evaluation of operators dictates that a minus sign (-) used as a negation operator (such as -1) is evaluated before all other operators. Because of this order, the formula
    =-1^2
    represents the value -1 squared, and returns the value 1, a positive value.
    [...]
    That has been the standard method for evaluating formulas since the first version of Microsoft Excel.

    NOTE: This order of operations is different from the order of operations in Lotus 1-2-3.


  • Curiously, Wikipedia only lists two instances that interpret "-a^b" as the negative sign having higher precedence than exponentiation: Excel and some language called bc.  And that's in the section "Gaps in the standard."

    Anyone know of any other places that do this, for posterity's sake?



  • @too_many_usernames said:

    Curiously, Wikipedia only lists two instances that interpret "-a^b" as the negative sign having higher precedence than exponentiation: Excel and some language called bc.  And that's in the section "Gaps in the standard."

    Anyone know of any other places that do this, for posterity's sake?

    Fortran.  Assuming ** is merely local dialect for ^.

     



  • @da Doctah said:

    @too_many_usernames said:

    Curiously, Wikipedia only lists two instances that interpret "-a^b" as the negative sign having higher precedence than exponentiation: Excel and some language called bc.  And that's in the section "Gaps in the standard."

    Anyone know of any other places that do this, for posterity's sake?

    Fortran.  Assuming ** is merely local dialect for ^.

     Too add to the confusion, consider all of the languages where caret ^ means something other then exponentiation (xor in some languages, pointer dereferencing in others, .....)



  • @PJH said:

    @too_many_usernames said:
    Since when is "negation highest precendence" correct? I can't find any reference that claims that - only the contrary in fact.
    http://support.microsoft.com/kb/q132686/

    Last Review: August 15, 2005

    The order of evaluation of operators dictates that a minus sign (-) used as a negation operator (such as -1) is evaluated before all other operators. Because of this order, the formula
    =-1^2
    represents the value -1 squared, and returns the value 1, a positive value.
    [...]
    That has been the standard method for evaluating formulas since the first version of Microsoft Excel.

    NOTE: This order of operations is different from the order of operations in Lotus 1-2-3.

    Microsoft also specifies that 2000 is not a leap year, to stay bug-compatible with old versions of their products. Plus probably a bunch of other stupid things I don't remember right now. That page is simply documenting what Excel does, and does not qualify as a normative specification, which is what I believe too_many_usernames was looking for.



  • @da Doctah said:

    @too_many_usernames said:

    Curiously, Wikipedia only lists two instances that interpret "-a^b" as the negative sign having higher precedence than exponentiation: Excel and some language called bc.  And that's in the section "Gaps in the standard."

    Anyone know of any other places that do this, for posterity's sake?

    Fortran.  Assuming ** is merely local dialect for ^.

     

    Umm - From the Fortran 77 standard, exponentiation is much higher in precedence than unary + or -

    For example, in the expression

    • A ** 2

    The exponentiation operator (**) has precedence over the negation operator (-); therefore, the operands of the exponentiation operator are combined to form an expression that is used as the operand of the negation operator. The interpretation of the above expression is the same as the interpretation of the expression

    • (A ** 2)

    Looking at Lahey's Fortran 95 documentation, thiis hasn't changed. Excel is on it's own with this one



  • @TheCPUWizard said:

    Too add to the confusion, consider all of the languages where caret ^ means something other then exponentiation (xor in some languages, pointer dereferencing in others, .....)
     

    ...logical "and" in APL, logical "not" in PL/1 (in addition to its semantically similar use in the compound operators ^=, ^< and ^>).



  • @TheCPUWizard said:

    @da Doctah said:

    @too_many_usernames said:

    Curiously, Wikipedia only lists two instances that interpret "-a^b" as the negative sign having higher precedence than exponentiation: Excel and some language called bc.  And that's in the section "Gaps in the standard."

    Anyone know of any other places that do this, for posterity's sake?

    Fortran.  Assuming ** is merely local dialect for ^.

     Too add to the confusion, consider all of the languages where caret ^ means something other then exponentiation (xor in some languages, pointer dereferencing in others, .....)

    Trying again, with more tags

    From the Fortran 77 standard - http://www.fortran.com/F77_std/f77_std.html exponentiation is much higher in precedence than unary plus or minus. It even gives this example:

    For example, in the expression


    - A ** 2

    The exponentiation operator (**) has precedence over the negation operator (-); therefore, the operands of the exponentiation operator are combined to form an expression that is used as the operand of the negation operator. The interpretation of the above expression is the same as the interpretation of the expression

    The Fortan 90 standard and the Fortan 95 standard (or at least the final draft of the F95 standartd) can be found on http://gcc.gnu.org/wiki/GFortranStandards and contains the same or similar text


Log in to reply