Thread: Cubed Root

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Cubed Root

    How would you find the cubed root of a number using C++. I know how to find the square root I just can't figure out how to find the cubed root.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    #include <cmath>

    const double ONE_THIRD = 1.0 / 3.0;

    double cuberoot(double num) {
    return pow(num, ONE_THIRD);
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    I made a little calculator where I used the following code to find any root... I also wrote a power function to go along with it, but that's already included in math.h I think.


    double Root(double number, double base)
    {
    double guess = 0;
    double low = 0;
    double high = number;
    double counter = 0;
    double change = 0;

    while( counter < base )
    counter++;

    if( counter != base)
    number = 0; // The point of this was to be sure it was an integer, I believe

    if (number > 0)
    {
    for( guess = (low + high)/2; Power(guess, base) != number; )
    {
    if( Power(guess, base) > number )
    high = guess;
    else
    low = guess;
    guess = (low + high)/2;
    if( high - low == change )
    break;
    change = high - low;
    }
    }
    return guess;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  2. Really basic string operation
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-28-2005, 05:18 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM