By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

pow(int,int)

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

Thanks,

Chris

Nov 28 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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>.
C++98 adds:

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:

Moreover, there shall be additional overloads sufficient to ensure:

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

P: n/a
"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

P: n/a
>C++98 adds:
>
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.

Bad, bad idea.

Steve
Nov 30 '06 #5

P: n/a
"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ek**********@blue.rahul.net...
C++98 adds:

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.

Bad, bad idea.
So bad, it isn't even the idea.

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

P: n/a
Steve Pope wrote:
>C++98 adds:

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

P: n/a
Jacek Dziedzic <ja************************@gmail.comwrote:
>Steve Pope wrote:
>>C++98 adds:

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

P: n/a

osmium wrote:
No, it has no such function.
Bummer. Thanks for your answer and explanation.

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.