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?


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


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.


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

  • $\begingroup$ Is $y$ an integer? $\endgroup$ – Zach Effman Dec 11 '15 at 19:02
  • $\begingroup$ Please edit the question to include a link to the other post you mentioned. It might help people better understand what you're doing. $\endgroup$ – David K Dec 11 '15 at 19:03
  • $\begingroup$ What does $exp_2$ mean? $\endgroup$ – Aneesh Dec 11 '15 at 19:06
  • $\begingroup$ 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. $\endgroup$ – David K Dec 11 '15 at 19:08
  • $\begingroup$ 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 $\endgroup$ – 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$.

  • $\begingroup$ Ok maybe a restriction of y being positive would be a good idea, will edit my question. $\endgroup$ – Zelphir Dec 11 '15 at 19:17
  • $\begingroup$ 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". $\endgroup$ – Zelphir Dec 11 '15 at 19:41
  • $\begingroup$ @Zelphir Yeah. $\lg$ is another way to write $\log_2$ $\endgroup$ – Trevor Norton Dec 11 '15 at 19:43
  • $\begingroup$ So $2^{\lg x} = x$ $\endgroup$ – Trevor Norton Dec 11 '15 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.