Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raising numbers to power **
#1
Hi there!
I've noticed something that I can't quite wrap my head around:
if I raise 2**2 I get 4
and if I raise 4**3 I get 64
but if I do something like 2**2**3 I get 256 ???

Why am I getting this result rather than 64 ?
Thanks !
Reply
#2
Precedence rules. Is evaluated as 2**(2**3). Always use parenthesis when precedence is unclear, even if they aren't needed. Clarity in code is as important as correctness.
GJG likes this post
Reply
#3
Another thing to keep in mind that numeric literals in Python are without sign and ** binds more tightly than unary sign. Therefore you must not be surprised by this:

>>> -2**2
-4
>>> (-2)**2
4
GJG likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
From the docs
Quote:6.5. The power operator
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is:

power ::= (await_expr | primary) ["**" u_expr]
Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2 results in -1.

The power operator has the same semantics as the built-in pow() function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type, and the result is of that type.

For int operands, the result has the same type as the operands unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 10**2 returns 100, but 10**-2 returns 0.01.

Raising 0.0 to a negative power results in a ZeroDivisionError. Raising a negative number to a fractional power results in a complex number. (In earlier versions it raised a ValueError.)
GJG likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Coding in Power BI Visual vjrans 0 276 Jan-31-2024, 07:54 AM
Last Post: vjrans
  Power Shells vs Compile and Run programs? RockBlok 2 381 Jan-13-2024, 09:08 PM
Last Post: RockBlok
Question Python and Power BI Desktop dangermaus33 1 1,319 Jan-19-2023, 06:54 AM
Last Post: GetOnData
  SystemError: initialization of integer failed without raising an exception Anldra12 2 4,391 Apr-19-2022, 10:50 AM
Last Post: Anldra12
  Detecting power plug Narayan 2 2,725 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  NameError: name 'display' is not defined when running code on power bi beginner1 2 19,203 Jul-24-2019, 11:03 AM
Last Post: beginner1
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,741 May-09-2019, 12:19 PM
Last Post: Pleiades
  power spectrum from 2Dfft sdipti596 1 2,325 Mar-30-2019, 12:34 AM
Last Post: scidam
  python result problem of an iteration algorithm for power allocation Jessica 1 2,638 Sep-07-2018, 08:08 PM
Last Post: micseydel
  raising multiple exceptions Skaperen 10 9,299 Mar-19-2017, 06:32 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020