# nth root function

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

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.$

• Is $y$ an integer? – Zach Effman Dec 11 '15 at 19:02
• Please edit the question to include a link to the other post you mentioned. It might help people better understand what you're doing. – David K Dec 11 '15 at 19:03
• What does $exp_2$ mean? – Aneesh Dec 11 '15 at 19:06
• The formula looks a little bit like something someone might write for a definition of an $n$th root function, but there seem to be several errors in it. – David K Dec 11 '15 at 19:08
• Assuming $exp_2 (x)$ is simply $2$ raised to the power $x$, I invite you to take a look at this link, wolframalpha.com/input/… , which shows that your definition does not capture the underlying mathematical definition of the $n$th root function. Perhaps a good start may be to replace your $x$'s with $\frac{1}{x}$'s – Aneesh Dec 11 '15 at 19:10

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$.
• Don't I have to use $log_2$, since I exponentiate 2? I thought, the formula can only be correct, when I do $log_2$ in case of $exp_2$, $log_3$ in case of $exp_3$ and so on, because they "balance each other". – Zelphir Dec 11 '15 at 19:41
• @Zelphir Yeah. $\lg$ is another way to write $\log_2$ – Trevor Norton Dec 11 '15 at 19:43
• So $2^{\lg x} = x$ – Trevor Norton Dec 11 '15 at 19:44