-
November 8th, 2009, 06:26 PM
#1
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:iameter()
{
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
-
November 8th, 2009, 08:34 PM
#2
Re: Power Funtion problem C++
We don't know what Volume or B are, and apparently the compiler's not sure either.
-
November 8th, 2009, 08:45 PM
#3
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
-
November 8th, 2009, 09:40 PM
#4
Re: Power Funtion problem C++
Originally Posted by punjboy
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
-
November 8th, 2009, 09:50 PM
#5
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 ..
-
November 8th, 2009, 10:01 PM
#6
Re: Power Funtion problem C++
Originally Posted by punjboy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|