Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

Timeline for answer to How do I calculate square root in Python? by wjandrea

Current License: CC BY-SA 4.0

Post Revisions

25 events
when toggle format what by license comment
Mar 10, 2025 at 14:24 history edited wjandrea CC BY-SA 4.0
typo (repeated word)
Sep 13, 2023 at 0:30 history edited wjandrea CC BY-SA 4.0
Use link with slug. A bunch of other minor fixes, mostly for clarity and brevity.
Jul 12, 2022 at 20:14 history edited wjandrea CC BY-SA 4.0
Use clearer naming.
Feb 4, 2022 at 19:44 history edited wjandrea CC BY-SA 4.0
Mention built-in `pow()` function
Jan 23, 2022 at 23:24 history edited Karl Knechtel CC BY-SA 4.0
fix name for operator
Jan 22, 2022 at 21:34 vote accept wjandrea
Jan 22, 2022 at 20:54 history edited wjandrea CC BY-SA 4.0
Mention nth root precision following Peter Cordes's comment
Jan 22, 2022 at 20:24 history edited wjandrea CC BY-SA 4.0
"pow()" doesn't seem relevant, unless its "mod" parameter might be useful in this context, which I don't know. Use non-inline links.
Jan 22, 2022 at 16:34 history edited Bernhard Barker CC BY-SA 4.0
Added link to operator conversion rules
Jan 22, 2022 at 16:08 history edited Bernhard Barker CC BY-SA 4.0
Added link to pow function and expanded on types a bit.
Jan 22, 2022 at 14:43 history edited Bernhard Barker CC BY-SA 4.0
Added link justifying that a square root equals the number to the power of 1/2
Jan 22, 2022 at 14:34 history edited Bernhard Barker CC BY-SA 4.0
Added explanation for the two options
Jan 22, 2022 at 10:14 history edited Toby Speight CC BY-SA 4.0
More specific wording
Jan 21, 2022 at 20:34 history edited Sören CC BY-SA 4.0
If there is a built-in, that should be the first option.
Jan 21, 2022 at 20:05 comment added wjandrea @Mark Thanks, that makes a lot of sense. I tried cmath.exp(cmath.log(x)/2) and got results that were comparable in accuracy to x ** .5. I updated the answer accordingly. If there's anything you'd like to add, by all means. I was thinking maybe it could use a summary at the bottom saying, like, "x ** .5 works in general," [which, btw, is why I put it first] "but for more specific cases, use the .sqrt() function of the library you're using for optimal results and performance."
Jan 21, 2022 at 20:00 history edited wjandrea CC BY-SA 4.0
Explain complex accuracy based on Mark Dickinson's comment.
Jan 21, 2022 at 9:50 comment added Mark Dickinson @wjandrea: "why does cmath use a different algo?" <- Different from what? Are you asking why cmath.sqrt(z) doesn't just use z ** 0.5? If so, the answer is that general complex power is a more complicated algorithm (take complex log, scale, then take complex exp of the result) than square root, with more opportunities for loss of accuracy, and so cmath.sqrt(z) is likely to be both faster and more accurate than z ** 0.5. My recommendation would be to always use an explicit sqrt function or method rather than a general powering operation.
Jan 21, 2022 at 6:06 comment added wjandrea @Mister Oh wait, if you square the results again, you don't get the input number back. Does that mean the actual problem is some sort of drop of precision?
Jan 21, 2022 at 6:03 comment added wjandrea @Mister Huh, cool! That begs the question though, why does cmath use a different algo?
Jan 21, 2022 at 5:41 comment added MisterMiyagi „Exponentiation works with negative numbers and complex numbers, though the results are very slightly off and I'm not sure why:“ That’s due to both being complex number operations/results and complex numbers not being a number line (from -inf to +inf) but rather a 2D plane (also -inf j and +inf j). Compare to how √x=1 has the solutions +1 and -1 - i.e. both „directions“ of the number line. Since complex numbers represent a plane, sqrt results are a circle on the complex plane. Picking one result on this circle is not numerically stable, hence some algorithms produce inaccurate results.
Jan 20, 2022 at 22:36 history edited wjandrea CC BY-SA 4.0
typo
Jan 20, 2022 at 22:15 history edited John Kugelman CC BY-SA 4.0
It's hard to remember which is option 1 and which is option 2 after scrolling down
Jan 20, 2022 at 21:21 history edited wjandrea CC BY-SA 4.0
added 58 characters in body
S Jan 20, 2022 at 21:16 history answered wjandrea CC BY-SA 4.0
S Jan 20, 2022 at 21:16 history made wiki Post Made Community Wiki by wjandrea