Thread: Roots??

  1. #1
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    Roots??

    Yeah, I'm a pest.

    Why do I get such bizarre results with

    Code:
    std::cout<<pow(16, -2);
    ??

    If I want the 12th root of 2 for musical applications (I already know it but that's beside the point) how do I calculate it in C++??

    Thanks again, I'll stop now, I'm over my quota of questions for the day!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Bizarre results?

    16^-2 = 1/(16*16) = 1/256

    Comes out to about .0039, which is what I get with that code (after resolving the ambiguity error)

    And for the 12th root, I think this works:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
      float result = pow(2,1.0f/12);
       cout<<"12th root of 2: "<<result<<endl;
      cout<<pow(result,12);
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Yeah, that does work, thanks -

    I don't get this, though:

    Code:
    float result = pow(2,1.0f/12);
    What's with 1.0f/12? I thought that raising to the power of a negative number would yield the root. I thought wrong.

    Where could I find out more about using pow() correctly?

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    power (type a, type b);

    a is the base number
    b is the power

    1.0f = 1.0(float)

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    So if you want the 12th root, its just plain math from here.

    x^(1/12) = 12root(x^1)

    ---->

    12root(x)

    = 12th root of x

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by -JM
    Yeah, that does work, thanks -

    I don't get this, though:

    Code:
    float result = pow(2,1.0f/12);
    What's with 1.0f/12? I thought that raising to the power of a negative number would yield the root. I thought wrong.

    Where could I find out more about using pow() correctly?
    Rasing a number to x to a power -y is equivalent to (1/x)^y which is not the yth root.

    The yth root of x is defined to be x^(1/y).

    You can see why if you consider for example the square of the square root of a number (which is the number again). If the root was a negative power you would get: (x^-2)^2 = x^(-2*2) = x^-4 != x (chaining powers is equivalent to multypling the exponents).
    Since the root is a fractional power though you get: (x^1/2)^2 = x^(1/2*2) = x

    If you want more information about powers and roots any high school level math textbook should have everything you need. From the looks of it pow(x,y) just returns x^y.

  7. #7
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Thanks guys, I get it now!

    So, I should have paid attention in high school math?

    -JM

  8. #8
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I didn't

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simplifing Square Roots
    By LiNeAr in forum C++ Programming
    Replies: 11
    Last Post: 10-01-2005, 08:56 PM
  2. Calculating polynomial roots
    By gambit in forum C++ Programming
    Replies: 7
    Last Post: 08-23-2004, 03:38 PM
  3. Homemade Square Roots
    By MisterWonderful in forum C++ Programming
    Replies: 13
    Last Post: 03-03-2004, 09:21 PM
  4. Square Roots
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 09:45 PM
  5. Square roots / Powers etc.
    By Robert602 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2001, 03:26 AM