-1

I'm wondering how does calculation made in the python using the power (**) with 0.5 calculate the square root value.

So while doing 2**0.5 in python we are getting 1.4142135.

Does anyone knows how does calculation has been made in python. how can we do that math in written.

Alec
  • 8,529
  • 8
  • 37
  • 63
  • Are you asking for the mathematical or pythonic explanation for why 2^(1/2) is 1.4? – Alec Oct 23 '19 at 05:08
  • If any of the answers solved your question, it's good practice to upvote them and accept the best one. The latter also grants you a small rep bonus :) – Alec Oct 24 '19 at 03:12
  • You want to know how `a**b`, or in mathematical notation `a^b`, is computed for non-integer exponents `b` like `0.5`? The basic idea (for all programming languages) is that `a^b = e^(b ln a)` where `e^x` is the exponential function with Euler number `e=2.71828...`, which has a simple Taylor series expansion: `e^x = 1 + x + x^2/2! + x^3/3! + ...`. Based on that sophisticated optimizations are applied. See also these questions: https://stackoverflow.com/questions/50724862, https://stackoverflow.com/questions/49960020 – coproc Oct 24 '19 at 08:31

3 Answers3

3

Mathematically, x1/2 is √x (√2 is approximately 1.41).

Python calculates square roots using 2^(y*log2(x)).

y*log2(x) and 2^x are both instructions on the compiler for CPython.

Alec
  • 8,529
  • 8
  • 37
  • 63
0

The reason this happens is that x to the power of (1/2) is mathematically equivalent to the square root of x

Michael H
  • 77
  • 5
  • Hi Michael M, Thanks for your valuable timing. I know that its x(1/2). But like to know the math calculation behind python how its calculating. :) – Swaminathan Oct 23 '19 at 05:03
-1

The ** operator in Python is equivalent to raising one value to the value of another.

So in your case. 2**0.5 is equivalent to 2^(1/2) which is in turn, equivalent to the square root of 2.

Caladin00
  • 356
  • 1
  • 3
  • 11