Power Funtion problem C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Power Funtion problem C++

  1. #1
    Join Date
    Oct 2009
    Posts
    9

    Power Funtion problem C++

    Hi,

    I am using a power funtion in my program and i am getting errors and apparently my program is not accepting the changes.

    I am using the 'pow' function. the formulas i am writing are - Diameter = B Volume 1/3 and Pressure = Volume 1/ B

    and i am using #include <cmath>


    /*************************************…
    ** Diameter
    **************************************…

    float balloon::diameter()
    {
    return (B * (float) pow(Volume,(1/3))); // Calculating Diameter
    }


    /*************************************…
    ** Pressure
    **************************************…

    float balloon:ressure()
    {
    return ((float) pow(Volume,(1/B))); // Caluclating Pressure
    }



    the errors i am getting with this is -

    \visual studio 2008\projects\prog8\code_2.cpp(71) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
    \visual studio 2008\projects\prog8\code_2.cpp(81) : error C2666: 'pow' : 6 overloads have similar conversions
    c:\program files\microsoft visual studio 9.0\vc\include\math.h(575): could be 'long double pow(long double,int)'
    c:\program files\microsoft visual studio 9.0\vc\include\math.h(573): or 'long double pow(long double,long double)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(527): or 'float pow(float,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(525): or 'float pow(float,float)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(489): or 'double pow(double,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(123): or 'double pow(double,double)'
    1> while trying to match the argument list '(float, double)'
    1>Build log was saved at "file://c:\Users\Aman\Documents\Visual Studio 2008\Projects\Prog8\Debug\BuildLog.htm"
    1>Prog8 - 1 error(s), 1 warning(s)


    Would appreciate any help...

    Thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: Power Funtion problem C++

    We don't know what Volume or B are, and apparently the compiler's not sure either.

  3. #3
    Join Date
    Oct 2009
    Posts
    9

    Re: Power Funtion problem C++

    I guess i should rephrase the question -

    how would u enter this in a c++ format - ( using power function POW)

    B *air 1/3

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Power Funtion problem C++

    Quote Originally Posted by punjboy View Post
    I guess i should rephrase the question -

    how would u enter this in a c++ format - ( using power function POW)

    B *air 1/3
    This will answer your own question.

    What does this program output:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << 1/3;
    }
    Now what does this output?
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << 1.0/3.0;
    }
    See the difference? What is the difference between 1, 1.0, and 3, 3.0?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2009
    Posts
    9

    Re: Power Funtion problem C++

    Well, i did try that

    B * float pow (float Volume,float (1.0/3.0));


    but it doesn't gives me the right answer ..

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Power Funtion problem C++

    Quote Originally Posted by punjboy View Post
    Well, i did try that

    B * float pow (float Volume,float (1.0/3.0));


    but it doesn't gives me the right answer ..
    It shouldn't have even compiled. Do you have a C++ book, or are you just trying to make things up and seeing what works? Basically, that line of code doesn't make any sense,

    It's very simple:
    Code:
    #include <cmath>
    #include <iostream>
    
    int main()
    {
       float Volume = 45.0F;
       float Value = pow(Volume, float(1.0/3.0));
       std::cout << Value;
    }
    If you're using float, then the pow() function requires two float arguments. If you're using double, then the pow() function requires two double arguments (or a double and an int as the power).

    Note that I stated 1.0/3.0 and not 1/3. Do you know the difference? It is very important to this entire discussion.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center