# Question..

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-04-2006
gyferlotus
Question..
How do I use the pow command?

I need to basicly do 10^x

I dont know how to do it with the pow command if any one could help me I would be thankful
• 11-04-2006
Salem
<batman>
pow!!!!
zap!!!!
</batman>

> How do I use the pow command?
n = pow(x, y);
• 11-04-2006
swoopy
>How do I use the pow command?
Code:

`double answer = pow(10., x)`
• 11-04-2006
Mario F.
Code:

`double pow( double base, double exp );`
As such,

Code:

```double result; double exp = 3; // for instance result = pow(10.0, exp);```
Do NOT use integers with this function. The standard states that implementations are free to overload floating-point functions to their float, double and long double types. So, its possible that your implementation has pow defined as:

double pow( float base, float exp );
double pow( double base, double exp );
double pow( long double base, long double exp );

If you use an integer as an argument to one or both parameters, the compiler will complain your attempt is ambiguous. Even if it does not (because some compilers do not overload or provide preprocessor policies), your code may fail to compile on other implementations.
• 11-04-2006
swoopy
>Do NOT use integers with this function.
Doesn't C++ have a:
Code:

`double pow( double base, int exp );`
I have no idea how to check this. Maybe the C++ standard has a list?
• 11-04-2006
manutd
If you need to do ints, just write it all out:
Code:

`x = x*x*x*x*x*x*x*x*x*x      //x^10`
• 11-04-2006
Mario F.
Quote:

Originally Posted by manutd
If you need to do ints, just write it all out:
Code:

`x = x*x*x*x*x*x*x*x*x*x      //x^10`

You are joking right?
• 11-04-2006
manutd
Yes.
• 11-04-2006
swoopy
I checked the library for Dev-C++ and it does have a:
Code:

`double pow( double base, int exp );`
I dunno if it's standard though.
• 11-04-2006
whiteflags
It's not. I don't understand why you can't just use a cast to double for intermediate use of pow() though.
• 11-04-2006
Mario F.
As citizen said. It's not a big deal to use a cast in this situation. Cast to whatever floating-point type is more appropriate, adorn the literal or define floating-point variables, do what you must. But avoid using integers even if your compiler happily says everything is alright :)
• 11-04-2006
swoopy
>It's not.
Do you have a copy of the C++ standard to verify that? Because I'm fairly certain it's in there.
• 11-04-2006
swoopy
>But avoid using integers even if your compiler happily says everything is alright
I see no reason to avoid using int for the exponent. Unless you're compiling C code.
• 11-04-2006
whiteflags
> Do you have a copy of the C++ standard to verify that?
I can find a reference.
http://www.dinkumware.com/manuals/?m...=math.html#pow
Barring you don't trust that, various implementations will let you know what is available by extention if you look it up in the help files.
• 11-04-2006
Mario F.
Well... the standard does in fact state the following signatures for pow (26.5.6):

float pow(float, int)
float pow(float, float)
double pow(double, int)
double pow(double, double)
long double pow(long double, int)
long double pow(long double, long double)

So yes, an int exponent will not generate ambiguity.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last