Algorithm to calculate root (or power) in computer

Click For Summary

Discussion Overview

The discussion centers around finding an algorithm to calculate the nth root or power of a given real number, where "n" can be either an integer or a fractional value. Participants explore various methods and express their preferences for mathematical approaches over built-in functions in programming languages.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests an algorithm for calculating nth roots or powers without relying on existing functions like pow().
  • Another participant suggests using the pow() function from math libraries in C++ and other languages, but acknowledges the request for a lower-level approach.
  • A participant expresses a desire to focus on the mathematical aspect of the problem rather than programming, indicating a preference for implementing a custom power() method.
  • A later reply introduces the relationship between powers and logarithms, suggesting that using exponential and logarithmic functions could be a viable method for calculating powers and roots.
  • Concerns are raised about handling cases where the base is negative, particularly when dealing with real and complex results.
  • Some participants clarify that the original question may be more focused on integer cases, while others reference a Wikipedia algorithm for fractional cases.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a specific algorithm. There are multiple competing views regarding the best approach to implement the calculation, with some preferring mathematical methods and others suggesting built-in functions.

Contextual Notes

Participants express uncertainty about the handling of negative bases and the implications for complex results. There is also a distinction made between integer and fractional powers, which may affect the choice of algorithm.

hkBattousai
Messages
64
Reaction score
0
I need an algorithm to calculate nth root or power of any given real number. "n" can be either integer or fractional, and is real.

I found http://en.wikipedia.org/wiki/N-th_root_algorithm" , but it requires to calculate power in it, therefore I can't use it.

Newton's method:
[itex]x_{k+1} = \frac{1}{n} \left[{(n-1)x_k +\frac{A}{x_k^{n-1}}}\right][/itex]


So, I need another algorithm.
Can you please suggest one?
 
Last edited by a moderator:
Physics news on Phys.org
If you are writing it in c++, you can include math or math.h depending on your compiler and use the pow() function. Some other languages have similar math libraries.

I suspect your looking for something lower level without functions other than base instructions. This may be of interest to you: http://www.syntax-example.com/Code/calculate-powerab-ie-ab-1670.aspx
 
Last edited by a moderator:
fedaykin said:
If you are writing it in c++, you can include math or math.h depending on your compiler and use the pow() function. Some other languages have similar math libraries.

I suspect your looking for something lower level without functions other than base instructions. This may be of interest to you: http://www.syntax-example.com/Code/calculate-powerab-ie-ab-1670.aspx

Yes, I will write in C++ without using any external libraries like pow() or sqrt() of math.h. Your example code is in Assembly language. Rather than a pseudo code, I'm looking for a mathematical method for this calculation.
 
Last edited by a moderator:
hkBattousai said:
Yes, I will write in C++ without using any external libraries like pow() or sqrt() of math.h. Your example code is in Assembly language. Rather than a pseudo code, I'm looking for a mathematical method for this calculation.

Can't you just implement pow(x,n) ?
 
Jarle said:
Can't you just implement pow(x,n) ?

Actually, I'm more concerned about the mathematical aspect of the problem more than computer programming. I want to implement my own power() method.
 
hkBattousai said:
I want to implement my own power() method.

That's what I meant. for example to define x^k = pow(x,k) (excuse my java syntax)

double a = x;
for(int i = 0; i < k; i++) {
a = x * a;
}
return a;
 
Recall:

[tex] x^\alpha = e^{\ln x^\alpha} = e^{\alpha \ln x}[/tex]

now you just need an exponential and a log. Special cases like square roots can have nice iterations that converge quick (learned in real analysis of all places!), but I'm guessing this is the route you must take for general roots/powers.

Note that even though [tex]x[/tex] and [tex]\alpha[/tex] are real, when [tex]x<0[/tex] the result is complex. Are you including this case?

Good luck,

jason

edit: just realized that your question is for for the less general case of either [tex]\alpha[/tex] or [tex]1/\alpha[/tex] an integer. Jarle has the answer for the integer case. Once you have that, the 1/integer case can be done via the algorithm in your wikipedia page. Sorry if my generalization caused any confusion!
 
Last edited:

Similar threads

  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
45
Views
7K