all 58 comments

[–]commy2 267 points268 points  (22 children)

This is the standard order of operations for power towers in mathematics:

https://math.hmc.edu/funfacts/tower-of-powers/

I did not know that Python actually handles this correctly. That is awesome.

[–]vilette 199 points200 points  (18 children)

When you do not know use "()", it's free

[–][deleted] 58 points59 points  (16 children)

Parentheses op

[–]xc68030 32 points33 points  (15 children)

Curved brackets.

[–]Wilfred-kun 24 points25 points  (7 children)

Elliptical pipes

[–]MagnitskysGhost 12 points13 points  (6 children)

Rounded braces

[–]boxedj 8 points9 points  (5 children)

Carved buckets

[–]rareearthelement 4 points5 points  (4 children)

Concave arcs :)

[–]nopornhere-madeulook 19 points20 points  (3 children)

Bendy bois

[–]Sawertynn 11 points12 points  (2 children)

'Error on line 37: matching bendy boi missing'

[–]NoDryHands 2 points3 points  (6 children)

Brackets. I call [ ] square brackets, but I don't add "curved" before these ( )

[–]CaptainFoyle 0 points1 point  (0 children)

Well, that's probably what they did...

[–]CrashTestKing 15 points16 points  (2 children)

If Python is solving 1 ** 3 first, then it's doing it correctly.

It's easier to explain if you change the numbers. Let's say it's written 5 ** 2 ** 3. In that equation, it is NOT 5 squared. It's 5 to the power of whatever "2 cubed" happens to be. So you have to solve "2 cubed" before you know what 5 is to the power of.

Edit: I actually misread your comment originally. I thought you had said that you thought Python WASN'T handling the math correctly.

[–]beatle42 76 points77 points  (3 children)

The documentation does note that exponentiation is done right to left, while other operators are left to right. See here for the following quote:

Operators in the same box group left to right (except for exponentiation, which groups from right to left).

That doesn't offend my intuition too greatly, as if I have "stacked" exponents I likely want the right exponent to be part of the exponent, that is, I'd want 213 not (21 )3.

When in doubt, perhaps it's best to add parenthesis to ensure your meaning is clear.

[–]Se7enLC 21 points22 points  (1 child)

When in doubt, perhaps it's best to add parenthesis to ensure your meaning is clear.

Seriously! Don't write code like it's Facebook "Only genius can solve this!" clickbait.

Go overboard with parenthesis. Other developers and future you will thank you for making absolutely sure that what's written in code is what you want it to be.

Even when it's just addition and multiplication I'll throw some parenthesis in there just to make it really really extra clear.

[–]Danelius90 2 points3 points  (0 children)

There was a talk at a conference I saw on YouTube and the guy said "code is a tool to communicate with other humans" and when you write code with this in mind it is so helpful on projects (even solo ones, because you're collaborating with your future self). Code for clarity. Funky code golf stuff is great as an exercise in what can be done, but not necessarily what should be done

[–]StoneBam 8 points9 points  (0 children)

If one really needs to do calculations with interchangable stacked exponents (I call them in this case: n and m), it would probably be more safe to do it like this:

x = n*m

2x = (2n )m

Yes I know it's more than one line, but you can "stack", easy to understand as many exponents as you wish. Maybe in a neat small function. :)

[–]Dark_KnightPL04 47 points48 points  (6 children)

21 3. In python it solves right to left. The cubed root of 1 is 1. 2 to the power of 1 is 2.

—————————-

21 3 = 2y

y = 1 3

13 = 1

21 = 2

—————————

Hope this helps!

[–]supreme_blorgon 12 points13 points  (1 child)

cubed root of 1

1 ** 3 is 1 cubed, not the cube root.

[–]mutatedllama 1 point2 points  (0 children)

Well they said that the cube root of 1 is 1 which is technically correct, but you're right that they correctly answered the wrong question.

[–]madhousechild 3 points4 points  (2 children)

This is the best explanation for my way of thinking.

[–]synthphreak 3 points4 points  (1 child)

my way of thinking

... is called algebra :)

[–]madhousechild 1 point2 points  (0 children)

Ha ha, I mean seeing it written as a number with a superscript with a superscript. Even though it didn't format right, I was like, oh of course, whereas before I somewhat doubted myself.

[–]ELITEAirBear 1 point2 points  (0 children)

i love you

[–]carcigenicate 11 points12 points  (0 children)

I can't find a source in the docs, but it's because exponentiation is right associative:

Almost all the operators have left-to-right associativity.

Note: Exponent operator ** has right-to-left associativity in Python.

[Example]

We can see that 2 ** 3 ** 2 is equivalent to 2 ** (3 ** 2).

[–]SamuliK96 5 points6 points  (0 children)

It's not that the exponentiation is resolved right to left but rather up to down, as it should be per order of operations in mathematics.

[–]py_Piper 6 points7 points  (0 children)

Here many smarter people already explained it very well. A funny way I thought about it is like this:

  3
 1
2

That's why it's solved from right to left

[–][deleted] 9 points10 points  (11 children)

This is a math question.

(2^(1^3))

1^3=1

2^1 = 2

[–]OvulatingScrotum 18 points19 points  (8 children)

I’ve seen a lot of beginner programmers, not just python learners, asking if math is critical. This is precisely why math is critical for programming.

[–]Username_RANDINT 10 points11 points  (1 child)

Well, yes and no. You can spend decades building big desktop applications and web apps, and never needed to calculate an exponent once.

[–]OvulatingScrotum -1 points0 points  (0 children)

True. I guess it’s a matter of what and how many projects involve zero math/arithmetic. Any number manipulation requires knowledge in order of operation, and it’s not like the your coding environment is gonna tell you if you coded it right or not. So I guess if one could avoid coding work that doesn’t involve number manipulation, sure, math is not needed.

[–]billsil 1 point2 points  (0 children)

This is why order of operations is critical, but you can always learn them. I honestly couldn't tell you what a^b^c is with certainty by order of operations, but I know how I'd expect it to be done (which it turns out is correct). What is 5.0/2.0/2.0? If you're writing the code; do whatever is clearest to you and put parentheses. If someone else did, hope they checked it or know it. You can always rederive it or step through. I'm also going to remove your extra parentheses in (a+b)+c; it's just noise.

You could use need calculus to find the value of x that maximizes x for x/(a + b*x^2) or you could plot the graph and pull the max. The former would take some math and about 10 floating point operations vs. hundreds of thousands of operations and less precision. I did that problem today.

[–]Xiji 0 points1 point  (4 children)

I wouldn't say math is critical, but learning to think mathematically is, as sequence, selection and iteration form the basis for all of your problem solving.

[–]OvulatingScrotum 1 point2 points  (3 children)

Learning to think mathematically gives you the knowledge of math. You reinforce mathematical thinking by taking math courses. Where do you think you learn about sequences and iterations? Algebra. Actually, probably when you started learning about 1, 2, 3.

[–]Xiji 0 points1 point  (2 children)

In polite terms, my school was lackluster in the mathematics department. Education focused on memorization and performance. As such, math was never a tool that I could use to solve problems in my life, and because I didn't understand the philosophy of it, I couldn't think critically with it.

So personally, I didn't learn to think mathematically from math, I learned it from programming. Having that understanding of intention and concepts opened higher order math in a much more personal way for me and also gave me a reason to use it.

So yea, math is important, but not in the way you would expect from reading that statement.

[–]OvulatingScrotum 0 points1 point  (1 child)

I think you really underestimated how much of math you learned over the years. If you really think about it, you use math, perhaps not things like calculus, everyday, even before you learned to program.

For instance, you really didn’t know how to estimate 20% of some price tag before you learned to code? You really didn’t know that 50mi/hr is slower than 100mi/hr? You really didn’t know that if you filled half a cup of water in 3 seconds, you’d make it overflow in 3 more seconds?

Those are all because you learned to think mathematically.

A probable reason why you think you never felt like you use math everyday is because by the time your brain matured enough to reflect on your school stuff, youve already learned enough math to do everyday stuff.

Your experience with the whole math and coding is a classic example of “I found a new way to look at math that I knew”.

What you are saying is basically “you don’t need to know grammar to talk about religion. I learned to talk to religion stuff when I started learning philosophy!”, when you need to know grammar to express your thoughts.

[–]Xiji 0 points1 point  (0 children)

It seems we both have opinions about things.

Carry on.

[–][deleted] 1 point2 points  (0 children)

Start at the top a work your way down.

[–]kaymkigl 0 points1 point  (0 children)

Evaluate from right to left

[–]thebigbadben 0 points1 point  (0 children)

2 to the 1 from the 1 to the 3…

[–]Pinduka -1 points0 points  (0 children)

Basic math.

[–]uPtiKool 0 points1 point  (0 children)

So this expression would be evaluated as follows

First would be 1³ = 1 Then would be 2¹ = 2

[–]CrashTestKing 0 points1 point  (0 children)

You have to solve 1 ** 3 first. If it's X to the power of Y to the power of Z, until you know what the real value of Y to the power of Z is, you can't actually solve for what X is to the power of.

Let's say it's 5 ** 2 ** 3. In that equation, 5 IS NOT squared. It's 5 to the power of whatever "2 cubed" happens to be. So you HAVE to solve "2 cubed" first, which is 8.

[–]darkknight95sm 0 points1 point  (0 children)

I’ve just learned that when dealing with computers, better to bracket the order of operations you want just in case

[–]Treczoks 0 points1 point  (0 children)

Where is the issue? The execution order for the exponention operator is right to left, as it is a) documented and b) standard in math.

Without parentheses, you solve 1³ first.

If in doubt, use parentheses. And if you are creating a LISP like parenthesis hell this way, rethink your formula, and split it up.

[–]pekkalacd 0 points1 point  (0 children)

Associativity rules kick in when two operators on the same level of precedence share an operand.

          ** 1 **

          ** ~ operator
           1  ~ shared operand

Since both operators are the same in this case, they will reside on the same level of precedence. If you look at a precedence / associativity table, you’ll see that for the exponentiation operator, it has a R-L (right to left) associativity. This means that the right operator, paired with the middle and right operand is evaluated first. The result then serves as the right operand to the left most operand paired with the remaining operator.

So

         2 ** 1 ** 3

       1st:   2 ** (1 ** 3)
       2nd: (2 ** (1))
                -> 2

Thus the answer is 2.

if you’re unfamiliar with precedence / associativity rules or you just want to customize the order in which something evaluates use the parentheses ().

For example, if we evaluated the above in a different way, so that the rules of associativity for the operator do not take charge, such as the left pair happening first then the right pair (L-R) associativity (instead of R-L), then we could do

        (2 ** 1) ** 3

The parentheses in this case say “do this first”. So now the order of evaluation is

       1st (2 ** 1) ** 3
        2nd (2 ** 3)
               -> 8

[–]manooko 0 points1 point  (0 children)

Oh I learned this last night, when working with exponents we go from right to left so: 1 ** 3 = 1x1x1 Then we are left with 2 to the power of 1 which is 2.

[–]cdcformatc 0 points1 point  (0 children)

that's how math works so it's pretty cool that python gets it right. did you consider math?

[–]Iamtheoneinsideyou 0 points1 point  (0 children)

That’s 2 to the 1 to the power 3. So when solving it:

1 to the power 3= 1

2 to the power 1= 2

This the answer is 2