469,922 Members | 2,037 Online

# pow(int,int)

Q1: Does c++ provide pow(int,int)?
Q2: If not, why not?

Thanks,

Chris

Nov 28 '06 #1
8 58217
Chris Stankevitz wrote:
Q1: Does c++ provide pow(int,int)?
Q2: If not, why not?

Thanks,

Chris
The ISO/ANSI Std 'C' function is:

double pow(double x, double y);

This is supported by C++; just include
<math.hor <cmath>.

Nov 29 '06 #2
In article <1c5bh.20014\$yV.6551@trnddc07>,
Larry Smith <ls****@nospam.comwrote:
Chris Stankevitz wrote:
Q1: Does c++ provide pow(int,int)?
Q2: If not, why not?

Thanks,

Chris

The ISO/ANSI Std 'C' function is:

double pow(double x, double y);

This is supported by C++; just include
<math.hor <cmath>.

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

The current C++0X working draft adds:

1. If any argument corresponding to a double parameter has type long
double, then all arguments corresponding
to double parameters are effectively cast to long double.

2. Otherwise, if any argument corresponding to a double parameter has
type double or an integer type, then all
arguments corresponding to double parameters are effectively cast to
double.

3. Otherwise, all arguments corresponding to double parameters are
effectively cast to float.

-Howard
Nov 29 '06 #3
"Chris Stankevitz" writes:
Q1: Does c++ provide pow(int,int)?
Q2: If not, why not?
No, it has no such function. The reason is that the committee that decides
such things decided that such a function was not a good idea.

ISTM that since the range of results is much too large to be contained in an
int, a conversion to a floating point type will be required eventually.
Since the result can not be expressed precisely with the usual data types,
why not do the conversion when the function is called? BTW, it is not
obvious that there is any loss of precision in the cases where the result
can be expressed as an int.

Nov 29 '06 #4
>
float pow(float, float);
Ouch. So an expression like pow(2.54,2.0) is now only
single-precision. This will cause subtle roundoff errors
in a lot of pre-existing code.

Steve
Nov 30 '06 #5
"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ek**********@blue.rahul.net...

float pow(float, float);

Ouch. So an expression like pow(2.54,2.0) is now only
single-precision.
No. Both operands are double, so it calls pow(double, double).
This will cause subtle roundoff errors
in a lot of pre-existing code.

So bad, it isn't even the idea.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 30 '06 #6
Steve Pope wrote:

float pow(float, float);

Ouch. So an expression like pow(2.54,2.0) is now only
single-precision. This will cause subtle roundoff errors
in a lot of pre-existing code.
Wouldn't you need pow(2.54f,2.0f) for pow to resolve
to pow(float, float)?

- J.
Nov 30 '06 #7
Jacek Dziedzic <ja************************@gmail.comwrote:
>Steve Pope wrote:

float pow(float, float);

Ouch. So an expression like pow(2.54,2.0) is now only
single-precision. This will cause subtle roundoff errors
in a lot of pre-existing code.

Wouldn't you need pow(2.54f,2.0f) for pow to resolve
to pow(float, float)?
You're right, I think. Retract the above.

Steve
Nov 30 '06 #8

osmium wrote:
No, it has no such function.

chris@organiza-2euba2 ~
\$ cat test.cpp
#include <cmath>
using namespace std;

void f() { pow(2,2); }

chris@organiza-2euba2 ~
\$ g++ -c test.cpp
test.cpp: In function `void f()':
test.cpp:4: error: call of overloaded `pow(int, int)' is ambiguous
/usr/include/math.h:99: note: candidates are: double pow(double,
double)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cmath:361: note: long
double std:
:pow(long double, int)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cmath:357: note: float
std::pow(f
loat, int)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cmath:353: note: double
std::pow(
double, int)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cmath:349: note: long
double std:
:pow(long double, long double)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cmath:345: note: float
std::pow(f
loat, float)

chris@organiza-2euba2 ~
\$

Nov 30 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.