Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Confusion on ** operator

by velusamy (Monk)
on Jan 10, 2009 at 06:57 UTC ( [id://735360]=perlquestion: print w/replies, xml ) Need Help??

velusamy has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
use strict; use warnings; print -2**4+1;
The above code printing -15. Can any one explain how it is printing -15?

Replies are listed 'Best First'.
Re: Confusion on ** operator
by davido (Cardinal) on Jan 10, 2009 at 07:02 UTC

    Operator precedence is at work:

    The '-' operator as a unary operator binds to "2" with the second highest precedence. First comes the ** binary operator, which binds more closely than the unary '-'. Last in your formula comes addition.

    It really is spelled out like this:

    (-(2**4))+1

    Or in other words: Two to the exponent of four equals sixteen, and then made negative. Negative sixteen plus one equals negative fifteen.

    perlop is your friend.

    It's the same for common algebraic expressions, hand written or in computer form:

    1. Exponent: (**)
    2. Sign: (unary -)
    3. Division and Multiplication: (/ and *)
    4. Subtraction and addition: (- and +)

    This is not a complete list of operators, of course. For a better list, again, take a look at perlop.

    Edit: fixed parens to clarify.


    Dave

      And, indeed, the Exponentiation section of perlop discusses this very example (well, the -2**4 part anyway).
Re: Confusion on ** operator (poly)
by tye (Sage) on Jan 11, 2009 at 02:51 UTC

    And how does one know this without "memorizing the precedence table"?

    The precedence of simple arithmatic operators was set so that one can write polynomials without using parentheses. So just consider the right polynomial and it is easy to derive what the precedence rules are:

    -x3+5x2-6x+2
    -(x3)+(5(x2))-(6x)+2
    
    -$x**3+5*$x**2-6*$x+2 -($x**3)+(5*($x**2))-(6*$x)+2

    So, using (( to indicate "binds tighter than" such that the tightest-binding operators are to the left below, the above tells us:

    (( * (( + - ** (( (( -u

    (Where -u fits in relative to * nearly doesn't matter, but unary operators tend to bind tighter in general and that holds true here so that the actual precedence is ** (( -u +u (( * / (( + - as can be verified at perlop.)

    - tye        

Re: Confusion on ** operator
by zentara (Archbishop) on Jan 10, 2009 at 14:35 UTC
    There was an interesting post on the comp.lang.perl.misc newsgroup regarding this, answered by jwkrahn, the negation has low precedence and needs to be in ()
    #!/usr/bin/perl # http://mathforum.org/library/drmath/view/55721.html # gives bad output my $x = ( -1 ** 0 ); print "$x\n"; # correct my $y = ((-1)**0); print "$y\n";

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://735360]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found