-1

Sorry if this seems really obvious-I'm a beginner- but is there any way to find any root of a value? Like the cube root or fourth root? And is there any way t do this with a variable instead of a set value? ex. math.varroot(x) or something like this? Thanks!

wjandrea
  • 23,210
  • 7
  • 49
  • 68
  • 3
    Yes. If you wish to find the cube root of 100, for example, you can do `100**1/3`, essentially raising the number to the reciprocal of the root you are wanting. – JNevill May 23 '22 at 21:15
  • 1
    I think you want `1/3` there, @JNevill – Chris May 23 '22 at 21:17
  • 3
    @JNevill: Cube root would be `100 ** (1 / 3)`; the parentheses are needed for precedence (exponentiation is higher precedence than division, so what you wrote is equivalent to `(100 ** 1) / 4 == 100 / 4 == 25.0`), and a cube root is `1 / 3`, not `1 / 4`. If you hate parens, `100 ** 3 ** -1` works (the associativity of `**` means that's the same as `100 ** (3 ** -1)`). – ShadowRanger May 23 '22 at 21:18
  • Whoops. Sorry about that. I updated my comment. It's been a long day, y'all. – JNevill May 23 '22 at 21:19
  • 1
    @JNevill: Don't forget the parentheses! – ShadowRanger May 23 '22 at 21:20
  • Python 11 is adding `math.cbrt` to find the cube root. – Frank Yellin May 23 '22 at 21:31
  • @Frank You mean 3.11? – wjandrea May 23 '22 at 21:32
  • 1
    In addition to the linked duplicate, you might want to check out [How do I calculate square root in Python?](/q/70793490/4518341) (which I wrote) cause there's a bunch of related info there, like [How to find integer nth roots?](/q/15978781), [calculating n-th roots using Python 3's decimal module](/q/15424123), SymPy, NumPy, and some arbitrary precision solutions for square root that could probably be generalized to nth root. – wjandrea May 23 '22 at 21:36
  • @wjandrea. Yeah. Typed too quickly. – Frank Yellin May 23 '22 at 21:44

0 Answers0