DescriptionThe sqrt() method returns the square root of x for x > 0. Show
SyntaxFollowing is the syntax for sqrt() method − import math math.sqrt( x ) Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object. Parametersx − This is a numeric expression. Return ValueThis method returns square root of x for x > 0. ExampleThe following example shows the usage of sqrt() method. #!/usr/bin/python3 import math # This will import math module print ("math.sqrt(100) : ", math.sqrt(100)) print ("math.sqrt(7) : ", math.sqrt(7)) print ("math.sqrt(math.pi) : ", math.sqrt(math.pi)) OutputWhen we run above program, it produces the following result: math.sqrt(100) : 10.0 math.sqrt(7) : 2.6457513110645907 math.sqrt(math.pi) : 1.7724538509055159 python_numbers.htm Option 1: math.sqrt()The
Option 2: Fractional exponentThe power operator
( The power operator requires numeric types and matches
the conversion rules for binary arithmetic operators, so in this case it will return either a
(Note: in Python 2, This method can be generalized to nth root, though fractions that can't be exactly represented as a
Edge casesNegative and complexExponentiation works with negative numbers and complex numbers, though the results have some slight inaccuracy:
Note the parentheses on Meanwhile,
PrecisionBoth options involve an implicit conversion to
Very large numbers might not even fit in a float and you'll get Other typesLet's look at
Exponentiation fails unless the exponent is also
Meanwhile,
5 SymPyDepending on your goal, it might be a good idea to delay the calculation of square roots for as long as possible. SymPy might help.
This doesn't seem very useful at first. But sympy can give more information than floats or Decimals:
Also, no precision is lost. (√2)² is still an integer:
In comparison, floats and Decimals would return a number which is very close to 2 but not equal to 2:
Sympy also understands more complex examples like the Gaussian integral:
Finally, if a decimal representation is desired, it's possible to ask for more digits than will ever be needed:
wjandrea 24.8k8 gold badges53 silver badges73 bronze badges answered Jan 21 at 14:38
Eric DuminilEric Duminil 51.4k8 gold badges66 silver badges118 bronze badges NumPy
docs NegativeFor negative reals, it'll return
Another option, of course, is to convert to complex first:
0 Newton's methodMost simple and accurate way to compute square root is Newton's method. You have a number which you want to compute its square root (
This line computes a more
accurate estimate with those 2 parameters. You can pass
For example we need to find 30's square root. We know that the result is between 5 and 6.
number is 30 and estimate is 5. The result from each recursive calls are:
The last result is the most accurate computation of the square root of
number. It is the same value as the built-in function This answer was originally posted by gunesevitan, but is now deleted. 6 Python's The following method takes a positive number
See the reference below for details on this operation's implementation. It also shows how to implement other operations with upper and lower bounds (although there is apparently at least one error
with the
Alternatively, using Python's
In the foregoing, answered Jan 21 at 19:45
Peter O.Peter O. 31.3k14 gold badges77 silver badges92 bronze badges 1
Binary searchDisclaimer: this is for a more specialised use-case. This method might not be practical in all circumstances. Benefits:
I personally implemented this one for a crypto CTF challenge (RSA cube root attack),where I needed a precise integer value. The general idea can be extended to any other root.
EDIT:As @wjandrea have also pointed out, **this example code can NOT compute **. This is a side-effect of the fact that it does not convert anything into floats, so no precision is lost. If the root is an integer, you get that back. If it's not, you get the biggest number whose square is smaller than your number. I updated the code so that it also returns a bool indicating if the value is correct or not, and also fixed an issue causing it to loop infinitely (also pointed out by @wjandrea). This implementation of the general method still works kindof weird for smaller numbers, but above 10 I had no problems with. Overcoming the issues and limits of this method/implementation:For smaller numbers, you can just use all the other methods from other answers. They generally use floats, which might be a loss of precision, but for small integers that should mean no problem at all. All of those methods that use floats have the same (or nearly the same) limit from this. If you still want to use this method and get float results, it should be trivial to convert this to use floats too. Note that that will reintroduce precision loss, this method's unique benefit over the others, and in that case you can also just use any of the other answers. I think the newton's method version converges a bit faster, but I'm not sure. For larger numbers, where loss of precision with floats come into play, this method can give results closer to the actual answer (depending on how big is the input). If you want to work with non-integers in this range, you can use other types, for example fixed precision numbers in this method too. Edit 2, on other answers:Currently, and afaik, the only other answer that has similar or better precision for large numbers than this implementation is the one that suggest SymPy, by Eric Duminil. That version is also easier to use, and work for any kind of number, the only downside is that it requires SymPy. My implementation is free from any huge dependencies if that is what you are looking for. answered Jan 21 at 17:12
Uncle DinoUncle Dino 7621 gold badge6 silver badges23 bronze badges 9 Arbitrary precision square rootThis variation uses string manipulations to convert a string which represents a decimal floating-point number to an The input string, The
Test
Output
For small numbers of digits, it's faster to use Here's a live version running on the SageMathCell server. answered Jan 25 at 14:39
PM 2RingPM 2Ring 53k5 gold badges77 silver badges167 bronze badges 1 find square-root of a number
OUTPUT:-
✔ Output 💡 CLICK BELOW & SEE ✔
answered May 7 at 17:21
8 How do you write 2 squared in python?How to Square a Number in Python. By multiplying numbers two times: (number*number). By using Exponent Operator (**): (number**2). Using math.pow() method: (math.pow(number, 2)). How does square root work in Python?sqrt() has a straightforward interface. It takes one parameter, x , which (as you saw before) stands for the square for which you are trying to calculate the square root. In the example from earlier, this would be 25 . The return value of sqrt() is the square root of x , as a floating point number.
|