Nth root pow(base,1/root) question - solved

I need to take the 12th root of(2^x) where x is changing in a project
i tried doing pow(pow(2^x),1/12) and it returns 1 every time.
I did a sample quick program, and printed pow(4,1/2); that should return 2, but it returns 1.
What should I do?


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("START: ");
}

double num;
void loop() {
  // put your main code here, to run repeatedly:
  num = pow(4,2);
  Serial.println("4,2: ");Serial.println(num);
  num = pow(4,1/2);
  Serial.println("4,1/2: ");Serial.println(num);
  delay(2000);

}

output:
4,2:
16.00

4,1/2:
1.00

Try using 0.5 instead of 1/2. 1/2 is zero, it's integer arithmetic at this point.

Or use 1.0/2

And later, for real 1/12.0 will help with that twelfth root of 2.

See what I did there? :wink:

a7

perfect.
For people of the future:
There are two methods, but alto777's is the best.

  1. use pow() correctly and have .0 at the end of whatever operation is inside. This just requires one line, ie pow(2.0,1/n.0) for the nth root (also works with 2/1.0 or 1.0/1)

or, second method which is long but also makes sense if you want to be crazy, but it's also pretty beautiful. Still uses pow, but its nice. Newtons method of root estimation:


double nthRoot(int A, int N)
{
    // initially guessing a random number between
    // 0 and 9
    double xPre = rand() % 10;
    //  smaller eps, denotes more accuracy
    double eps = 1e-3;
 
    // initializing difference between two
    // roots by INT_MAX
    double delX = 5;
    //  xK denotes current value of x
    double xK;
    //  loop until we reach desired accuracy
    while (delX > eps)
    {
        //  calculating current value from previous
        // value by newton's method
        xK = ((N - 1.0) * xPre +
              (double)A/pow(xPre, N-1)) / (double)N;
        delX = abs(xK - xPre);
        xPre = xK;
    }
    return xK;
}

But but you have this line of code in your function...

xK = ((N - 1.0) * xPre +
              (double)A/pow(xPre, N-1)) / (double)N;

Besides dredging up nghtmares about Newton's method.

a7

3 Likes

Do you mean, for those who don't know about integer arithmetic, or bother with consulting the C/C++ reference to learn how to properly call a function?

1 Like

I'm pretty new to c++, so I didn't realizing that doing 1.0 would be different then putting 1. I posted for anyone else who, like me, was having some trouble with their code. It's a stupid error, so if anyone in the future had the same one I put the message to help them.
This forum space isn't a place for being toxic, its a place to help each other. Thank you for your response.

thanks alto777, your cool.

Also, I copied the newtons method from online code, looked through to understand the idea behind it, then copied into my test code and it worked. I was trying to be funny about not being able to use pow() or some other function, because I can't see a scenario where they wouldn't be available.

I'm honestly really new to arduino/circuitry; I'm doing a lot of projects to learn more but I still have a lot of small mistakes like this since I'm trying to teach myself (like most people here I would assume). It's been really fun so far, and its people like you who respond quickly with answers that make it possible for learners like me!

A more helpful suggestion to others is to first check the language reference, to see if one is using function X correctly, before posting with the embarrassing title "function X not working".

The programming language has been around for about 50 years.

1 Like

It's interesting to think of a language that could propagate type dependencies down that far. But C/C++ isn't one.

It was originally intended to be fast and efficient on what are slow machines by today's standards. So integer calculations are given priority. What a great fit for today's small MCU's!

1 Like

I strongly agree.
Not to be negative toward the Op, but "the Forum" should never be the 1st choice for answers. It is not so much an issue of members answering questions, it is more (in my mind) the fact that beginners are not taking the time to research reference materials and to avail themselves of structured tutorials on building structured applications.

I applause anyone jumping into deep water to learn (to swim,) but statistics will show that bodies will accumulate from drownings. An introductory swimming class would drop fatalities significantly. There are every year an increasing number of newbies attempting to replicate or develop projects and the goal is not to be a programmer but to complete a project; piecing together this 'n that into a Frankenstein project that they do not fully understand.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.