Skip to content Skip to sidebar Skip to footer

How To Calculate Python Float-number-th Root Of Float Number

I found the following answer here on Stackoverflow: https://stackoverflow.com/a/356187/1829329 But it only works for integers as n in nth root: import gmpy2 as gmpy result = gmpy.

Solution 1:

see Power by squaring for negative exponents

anyway as I do not code in python or gmpy here some definitions first:

  • pow(x,y) means x powered by y
  • root(x,y) means x-th root of y

As these are inverse functions we can rewrite:

  • pow(root(x,y),x)=y

equation

You can use this to check for correctness. As the functions are inverse you can write also this:

  • pow(x,1/y)=root(y,x)
  • root(1/x,y)=pow(y,x)

So if you got fractional (rational) root or power you can compute it as integer counterpart with inverse function.

Also if you got for example something like root(2/3,5) then you need to separate to integer operands first:

root(2/3,5)=pow(root(2,5),3)
 ~11.18034 = ~2.236068   ^3
 ~11.18034 = ~11.18034

For irational roots and powers you can not obtain precise result. Instead you round the root or power to nearest possible representation you can to minimize the error or use pow(x,y) = exp2(y*log2(x)) approach. If you use any floating point or fixed point decimal numbers then you can forget about precise results and go for pow(x,y) = exp2(y*log2(x)) from the start ...

[Notes]

I assumed only positive operand ... if you got negative number powered or rooted then you need to handle the sign for integer roots and powers (odd/even). For irational roots and powers have the sign no meaning (or at least we do not understand any yet).


Solution 2:

If you are willing to use Python 3.x, the native pow() will do exactly what you want by just using root(x,y) = pow(x,1/y). It will automatically return a complex result if that is appropriate.

Python 3.4.3 (default, Sep 27 2015, 20:37:11)
[GCC 5.2.1 20150922] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> pow(1/0.213, 1/31.5)
1.0503191465568489
>>> pow(1/0.213, -1/31.5)
0.952091565004975
>>> pow(-1/0.213, -1/31.5)
(0.9473604081457588-0.09479770688958634j)
>>> pow(-1/0.213, 1/31.5)
(1.045099874779588+0.10457801566102139j)
>>>

Returning a complex result instead of raising a ValueError is one of changes in Python 3. If you want the same behavior with Python 2, you can use gmpy2 and enable complex results.

>>> import gmpy2
>>> gmpy2.version()
'2.0.5'
>>> gmpy2.get_context().allow_complex=True
>>> pow(1/gmpy2.mpfr("0.213"), 1/gmpy2.mpfr("31.5"))
mpfr('1.0503191465568489')
>>> pow(-1/gmpy2.mpfr("0.213"), 1/gmpy2.mpfr("31.5"))
mpc('1.0450998747795881+0.1045780156610214j')
>>> pow(-1/gmpy2.mpfr("0.213"), -1/gmpy2.mpfr("31.5"))
mpc('0.94736040814575884-0.094797706889586358j')
>>> pow(1/gmpy2.mpfr("0.213"), -1/gmpy2.mpfr("31.5"))
mpfr('0.95209156500497505')
>>> 

Solution 3:

Here is something I use that seems to work with any number just fine:

root = number**(1/nthroot)
print(root)

It works with any number data type.


Post a Comment for "How To Calculate Python Float-number-th Root Of Float Number"