How to represent interger powers in C++?

J

Joseph Paterson

I believe there to be no such representation.
Try:

#include <cmath>
float result = pow(3.0, 4.0)

Joseph.
 
D

Daniel T.

heng said:
such as 3^4 ?

/* pow example */
#include <cstdio>
#include <cmath>

using namespace std;

int main ()
{
cout << "7 ^ 3 = " << pow( 7, 3 ) << '\n';
cout << "4.73 ^ 12 = " << pow( 4.73, 12 ) << '\n';
cout << "32.01 ^ 1.54 = " << pow( 32.01, 1.54 ) << '\n';
}
 
S

Steven T. Hatton

heng said:
such as 3^4 ?
Option 1) write your own function. (but overloading won't work for
builtins.)
Option 2) Post to comp.std.c++ and suggest it be added.
Option 3) Use the morally unsound conversion to and from floating point
functionality provided with the Standard Library.

There are two other options, but they amount to impractical academic
exercises. These are to use Cpp meta-programming or template
meta-programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top