Go Down

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

#### lohitpm

##### Jan 20, 2016, 11:26 am
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.

#1

#### lohitpm

#2
##### Jan 20, 2016, 11:40 amLast Edit: Jan 20, 2016, 11:51 am by lohitpm
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

#3
##### Jan 20, 2016, 12:51 pm
You are correct. A quick test on a scientific calculator proves this.

3 ^ 4 = 81

81 ^ 0.25 (1/4) = 3

#### westfw

#4
##### Jan 20, 2016, 12:52 pm
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 pmLast Edit: Jan 20, 2016, 07:21 pm by robtillaart
1) ??
2) squareroot ==> sqrt(number);
3) cuberoot ==> cbrt(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

#6
##### Jan 21, 2016, 06:31 am
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.

#### westfw

#7
##### Jan 21, 2016, 08:02 am
Quote
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

#8
##### Jan 21, 2016, 09:07 am
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 pmLast 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