I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
get GPL

nroot

A function that calculates the nth root of an object
Controller: samtheman

Interface

C++

Nroot

 
template<class T> Tnrootconst T& x
const T& n )
This function calculates the nth root of a number x, as in \inline  \sqrt[n]{x}, using the iterative formula This algorithm converges quadratically, meaning that the bits of precision double per iteration, resulting in 64 bits after 6 iterations, which is the number of iterations used in the function. pow(const T&, const E&) must be defined and the normal arithmetic operators must accept different combinations of T and E.

Example 1

#include <stdio.h>
using namespace Maths::Arithmetic;
int main()
{
  for(double x = 10; x <= 100; x += 10) //to resolve pow ambiguities
  {
    for(int y = 2; y < 5; y++)
      printf("The %d root of %.0f is %lf\n", y, x, nroot(x,y));
  }
  return 0;
}

Output

The 2 root of 10 is 3.162278
The 3 root of 10 is 2.154435
The 4 root of 10 is 2.632547
The 2 root of 20 is 4.472136
The 3 root of 20 is 2.714418
The 4 root of 20 is 2.114743
The 2 root of 30 is 5.477226
The 3 root of 30 is 3.107233
The 4 root of 30 is 2.340347
The 2 root of 40 is 6.324555
The 3 root of 40 is 3.419952
The 4 root of 40 is 2.514867
The 2 root of 50 is 7.071068
The 3 root of 50 is 3.684031
The 4 root of 50 is 2.659148
The 2 root of 60 is 7.745967
The 3 root of 60 is 3.914868
The 4 root of 60 is 2.783158
The 2 root of 70 is 8.366600
The 3 root of 70 is 4.121285
The 4 root of 70 is 2.892508
The 2 root of 80 is 8.944272
The 3 root of 80 is 4.308869
The 4 root of 80 is 2.990698
The 2 root of 90 is 9.486833
The 3 root of 90 is 4.481405
The 4 root of 90 is 3.080070
The 2 root of 100 is 10.000000
The 3 root of 100 is 4.641589
The 4 root of 100 is 3.162278

Note

This function does not check for what would be normally incorrect input, as some objects are meant to act differently than normal numbers, such as complex numbers. If incorrect input values are passed to the function, the return values will be junk.

References

"Nth root algorithm." Wikipedia, The Free Encyclopedia. 8 Feb 2007, 14:48 UTC. Wikimedia Foundation, Inc. 3 Jun 2007 http://en.wikipedia.org/w/index.php?title=Nth_root_algorithm&oldid=106578515

Parameters

xa number
nthe root required

Returns

to nth root of inval

Authors

Sam Schetterer (June 2007)
Source Code

Source code is available when you agree to a GP Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.