Hi, how can I write a power? For example, I want to do 2^8, or variable "b"^8.
Thanks,
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;
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
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 (you could create your own too, but it'd probably have less flexibility unless you put some time into it).
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
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
`Who are YOU?' said the Caterpillar.
This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.
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.)
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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?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: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)
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
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).Originally Posted by MathFan
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Mhm, will keep that in mind.Originally Posted by CornedBee
Oh, didn't know that. Thanx...Originally Posted by Dae
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
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)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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??
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
Its correct for some of the header files. I didnt know if cmath was one of those though (he said iostream). Regardless, they removed the .h and maybe changed the file name a tiny bit.Originally Posted by CornedBee
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101