Go Down

Topic: Calculating roots of a number (Read 5309 times) previous topic - next topic

lohitpm

Good day everyone.

How do we calculate the square root, cube root and so on?

I know there is the sqrt() function which allows me to find the square root. But what about the further roots? How can we calculate them? If i want the fourth or fifth root how do i do it?

It would be great if someone tells me what actually we are doing in finding roots.

Thank you.

dannable


lohitpm

#2
Jan 20, 2016, 11:40 am Last Edit: Jan 20, 2016, 11:51 am by lohitpm
What did Google tell you?
The root of a number x is another number, which when multiplied by itself a given number of times, equals x.

So from what i understand a root of a number x can mathematically expressed as 'x to the power of one over the number of times to be multiplied'.

For example:
Fourth root of x is equal to x to the power of one-fourth.

How to multiply x one by four times?

Correct me if i'm wrong.


dannable

You are correct. A quick test on a scientific calculator proves this.

3 ^ 4 = 81

81 ^ 0.25 (1/4) = 3


westfw

Roots can be done with fractional powers.  Ie: pow(x, 0.333333333)  will give you about the cube root of x.

robtillaart

#5
Jan 20, 2016, 07:21 pm Last Edit: Jan 20, 2016, 07:21 pm by robtillaart
1) ??
2) squareroot ==> sqrt(number);
3) cuberoot ==> cbrt(number);
4) quadroot ==> sqrt(sqrt(number));
5) pentaroot ==> pow(number, 0.2);
6) sixtroot ==> cbrt(sqrt(number));
...

in general

Code: (not tested extensively) [Select]
double nthroot(double number, double n)
{
  if (n == 0) return NAN;
  if (number > 0) return pow(number, 1.0 / n);
  if (number == 0) return 0;
  if (number < 0 && int(n) == n && (int(n) & 1)) return -pow(-number, 1.0 / n);
  return NAN;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

lohitpm

This link gives information regarding finding the nth root of a number. It also shows Newton's method for calculation.

What remains now is converting maths into a function.

Thanks for your replies.

westfw

Quote
4) quadroot ==> sqrt(sqrt(number));
An interesting thought!   It turns out that pow() is the slowest of the AVR math functions; it's about 10x slower than the sqrt() function;   cbrt() is about twice as fast as pow()...
http://www.nongnu.org/avr-libc/user-manual/benchmarks.html


dannable

I was broadly thinking of using log() for the calculation. But I don't have the IDE or an Arduino at work so couldn't develop the idea.

robtillaart

#9
Jan 21, 2016, 09:06 pm Last Edit: Jan 21, 2016, 09:27 pm by robtillaart
This link gives information regarding finding the nth root of a number. It also shows Newton's method for calculation.

What remains now is converting maths into a function.
So give it a try ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up