Thread: pow() help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    pow() help

    I keep getting '1' as the answer to this. My instructor says to use
    1.0/3.0 instead of 1/3 to get the cuberoot. Then I get an overload error. Help?!


    Code:
    #include <iostream>
    #include <conio.h>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    	float x = 20;
    
    	float cuberoot = pow(x, 1/3);
    		cout << cuberoot;
    
    	getch();
    	return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    1/3 is zero in integer math, thus x to the zero power will always be one. I know theres a power function that deals with floats, let me see if I can find it...

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    For my compiler(GCC 3.4.3) mathcalls.h defines pow to take two doubles for arguments. I tested it and the statement:
    Code:
    pow(20.0, 1.0/3.0);
    works just fine.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by MB1
    I keep getting '1' as the answer to this. My instructor says to use
    1.0/3.0 instead of 1/3 to get the cuberoot. Then I get an overload error. Help?!


    Code:
    #include <iostream>
    #include <conio.h>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    	float x = 20;
    
    	float cuberoot = pow(x, 1/3);
    		cout << cuberoot;
    
    	getch();
    	return 0;
    }
    the overload error is because you're calling pow(float,int) when pow really only accepts (double,double). overloading is when you use the same function name (pow) with different arguments. basically, all you need to do is send two doubles to pow. try this:
    Code:
    float cuberoot = static_cast<float>(pow(static_cast<double>(x), 1.0/3.0));
    the first cast isn't entirely necessary, but it's good practice.
    Last edited by major_small; 04-18-2005 at 10:11 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Thanks for the help. I got the actual program working.

    I was supposed to compute water flow to 1000 feet per second and I couldn't get the cuberoot part towork.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the difference between pow() vs shift?
    By Overworked_PhD in forum C Programming
    Replies: 9
    Last Post: 01-06-2008, 01:04 PM
  2. Help with pow()
    By jedi_jinn in forum C Programming
    Replies: 7
    Last Post: 10-09-2006, 02:17 AM
  3. trying to use pow()
    By Mikecore in forum C Programming
    Replies: 9
    Last Post: 10-09-2005, 06:30 PM
  4. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 09:46 PM
  5. Problem with pow()
    By RMacFly in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 11:54 AM