Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nth Root of a number in C++ [duplicate]

Tags:

c++

math

I'm trying to make a maths library and one of the functions finds a nth root of a float.

My current expression is -

value = value ^ 1/rootValue

but I'm getting an error because I'm using a float. Is there another method of solving this?

like image 733
user3198803 Avatar asked Jan 15 '14 15:01

user3198803


2 Answers

There is no "power" operator in C++; ^ is the bitwise exclusive-or operator, which is only applicable to integers.

Instead, there is a function in the standard library:

#include <cmath>

value = std::pow(value, 1.0/root);
like image 185
Mike Seymour Avatar answered Nov 13 '22 15:11

Mike Seymour


^ is not what you want here. It is a bitwise exclusive-OR operator.

Use

#include <math.h>

and then

value = pow(value, 1/rootvalue)

Reference for pow: http://www.cplusplus.com/reference/cmath/pow/

like image 2
starsplusplus Avatar answered Nov 13 '22 16:11

starsplusplus