Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: ** operator in perl

by swampyankee (Parson)
on Feb 03, 2009 at 12:59 UTC ( #740978=note: print w/replies, xml ) Need Help??

in reply to ** operator in perl

I'm going to be more verbose.

You seem to be thinking that unary minus binds more tightly than exponentiation under normal arithmetic rules. It doesn't. You're expecting it to parse as

(-2)4 - 1

this is a misconception I've had to deal with often when tutoring community college students. By the normal precedence rules of arithmetic, it will actually be evaluated

-(24) - 1.

Perl (and most other programming languages) try to replicate the normal arithmetic precedence rules as closely as possible.

Remember, under normal arithmetic rules, exponentiation (the ** operator in Perl) binds more closely than multiplication (there's an implied multiplication in -2; it's shorthand for (-1) * 2), which binds more tightly than addition.

Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://740978]
[marto]: hopefully everyone had a good weekend

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2018-02-19 10:18 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (260 votes). Check out past polls.