Hi, how can I write a power? For example, I want to do 2^8, or variable "b"^8.
Thanks,
Printable View
Hi, how can I write a power? For example, I want to do 2^8, or variable "b"^8.
Thanks,
Code:double b=8;
cout<<pow((double)2, 8)<<" and "<<pow(b, 8)<<endl;
In the <math.h>
use it like: pow(2.4 , 3); // power of 3
I found this info on google (www.tutorials4u.com/c/t99.htm), just going to assume its correct :p (you could create your own too, but it'd probably have less flexibility unless you put some time into it).
Thank you, is very useful web!!
I normally use a loop for this kind of thing eg temp=temp*2 and run it 3 times to get 2^3.Probally not the best solution but it works
Yes, that is a good idea when you canīt use <math.h>.
If you are coding in C++, you should include <cmath.h>, I think. <math.h> is for C, while <cmath.h> is a C++ implementation of the same functions.
The difference is in that while C++ supports function overloading, C doesn't. That's why in math.h you will find functions like:
double pow (double base, double exp);
float powf (float base, float exp);
long double powl (long double base, long double exp);
...
While cmath.h has:
double pow(double base, double exp);
float pow (float base, float exp);
long double pow (long double base, long double exp);
...
So, when using math.h, and wanting to do something like 2.15f ^ 2.3f, you must do it with powf() (or perform a cast from float to double and use pow()), while with <cmath.h>, you can still use the overloaded pow() for nearly anything (long double, float, double,etc.)
It's <cmath>, not <cmath.h>. And yes, that's preferable.
Also, in the first answer, there's absolutely no need to cast the arguments. In fact it's a bad idea, as many compilers use integer powers instead of the more complicated and slower floating point power algorithm.
Marlon, I do hope you use full *= temp inside the loop. But it's still unnecessary. For short powers, write it out (temp^2 -> temp*temp, temp^3 -> temp*temp*temp), for larger ones use the cmath function.
Yeah, sorry, you are right. The problem was that I looked it up in "Programmer's Reference" by Herbert Schildt, where he said it must be <cmath.h>. Which I thought was strange, 'cause most of the C++ headers do not have the .h, like <iostream>, <vector> etc. But now I remember that I've heard somewhere that Schildt was a bad author or something?Quote:
It's <cmath>, not <cmath.h>
There is no need to cast the second argument, no. But what about the first one? If I just type pow(2,8); I get following errors:Quote:
Also, in the first answer, there's absolutely no need to cast the arguments. In fact it's a bad idea, as many compilers use integer powers instead of the more complicated and slower floating point power algorithm.
Code:main.cpp:12: error: call of overloaded `pow(int, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
double)
/usr/include/c++/3.3.4/cmath:512: error: long double
std::pow(long double, int)
/usr/include/c++/3.3.4/cmath:508: error: float std::pow(float,
int)
/usr/include/c++/3.3.4/cmath:504: error: double
std::pow(double, int)
/usr/include/c++/3.3.4/cmath:495: error: long double
std::pow(long double, long double)
/usr/include/c++/3.3.4/cmath:486: error: float std::pow(float,
float)
Hmm ... then apparently the cast is indeed necessary in order to disambiguate. (Annoying.)
But a better idea here is to use constant suffixes:
pow(8.0, 3) // calls pow(double, int)
pow(8.0f, 3) // calls pow(float, int)
pow(8.0l, 3) // if compiles, calls pow(long double, int)
Its because they changed the standard (ANSI or something) after that reference was made. It used to require .h but some odd 4 years ago they changed it so it no longer required .h and its now depreciated. (in gcc 4.0+ and .NET 2003+ or something).Quote:
Originally Posted by MathFan
Mhm, will keep that in mind.Quote:
Originally Posted by CornedBee
Oh, didn't know that. Thanx...Quote:
Originally Posted by Dae
What Dae just said is wrong. There never was a cmath.h header in any standard. From the very beginning, the C++ standard contained math.h and cmath.
Hmmm... my edition of the book was printed in 2000, and as far as I know, there were no changes in the standard since then. But then why on earth does Schildt write (even several times!) that I should include <cmath.h> in his book??
Because while Schildt is indeed a good author, in that he writes interesting texts, he very frequently gets his facts wrong. Here's what a page about the C and C++ standards has to say about his book "The Annotated ANSI C Standard":
In fact, the reason this book is recommended is simply that it's the cheapest way of acquiring a copy of the standard.Quote:
Herbert Schildt is not held in wide regard by knowledgeable C and C++programmers, and this is the only book with his name on it most of them would ever recommend. The format of the book places the text of the standard on the left hand pages side-by-side with Schildt's annotations on the right hand side.
While many of Schildt's comments are incorrect and flatly contradict the actual text of the standard on the facing page, the standard itself is complete except for one missing page.
Some more:
http://www.lysator.liu.se/c/schildt.html
http://herd.plethora.net/~seebs/c/c_tcr.html