nth root function

598 Views Asked by At

I want to write code for a nth root function, so I need to be sure, that the underlying mathematical function is correct. From another post over at SO, I wrote the following definition:

$ \sqrt[x]{y} = y^{\frac{1}{x}} = \left\{ {\begin{array}{rl} \exp_{2}\left(x \cdot \log_2 \left(\frac{1}{y}\right)\right) & \text{if } y > 0\\ -\exp_{2}\left(x \cdot \log_2 \left(\frac{1}{\left\lvert{y}\right\rvert}\right)\right) & \text{if } (y < 0) \land \left((y \equiv 1 \mod 2) \lor \left(\frac{1}{y} \equiv 1 \mod 2\right)\right)\\ \exp_{2}\left(x \cdot \log_2 \left(\frac{1}{\left\lvert{y}\right\rvert}\right)\right) & \text{if } (y < 0) \land \left((y \equiv 0 \mod 2) \lor \left(\frac{1}{y} \equiv 0 \mod 2\right)\right) \end{array}} \right. $

Is this function definition correct?

EDIT#1

Link to the other question: https://stackoverflow.com/questions/34221713/how-to-calculate-python-float-number-th-root-of-float-number/34223324#34223324

EDIT#2

What I want to achieve is a function, that can calculate the nth-root of any positive rational number, where n can be rational as well.

EDIT#3

I've not checked the variable positions again and this is what I have now:

$ \sqrt[\frac{n}{m}]{a} = a^{\frac{m}{n}} = \left\{ {\begin{array}{rl} \exp_{2}\left(\frac{m}{n} \cdot \log_2 \left(a\right)\right) & \text{if } a > 0\\ -\exp_{2}\left(\frac{m}{n} \cdot \log_2 \left(a\right)\right) & \text{if } (a < 0) \land \left((x \equiv 1 \mod 2) \lor \left(\frac{1}{x} \equiv 1 \mod 2\right)\right)\\ \exp_{2}\left(\frac{m}{n} \cdot \log_2 \left(a\right)\right) & \text{if } (a < 0) \land \left((x \equiv 0 \mod 2) \lor \left(\frac{1}{x} \equiv 0 \mod 2\right)\right) \end{array}} \right. $

1

There are 1 best solutions below

4
On BEST ANSWER

Your formula should be $y^{1/x}=2^{\frac{1}{x}\lg y}.$ And you should probably restrict your domain to just positive $y$ values unless you want to start getting imaginary values (e.g. $(-1)^{1/2} = \pm i)$.

Edit: If you want $a<0$ and want to find the $n^{th}$ (real) root, then you should make sure that $n$ is odd (otherwise you will get only imaginary roots); in this case $a^{1/n} = -(-a)^{1/n}.$ For $a^{m/n}$, we just do $(a^{1/n})^m$.