pow(int,int)

L

Larry Smith

Chris said:
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.h> or <cmath>.
 
H

Howard Hinnant

Larry Smith said:
The ISO/ANSI Std 'C' function is:

double pow(double x, double y);

This is supported by C++; just include
<math.h> or <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
 
O

osmium

Chris Stankevitz said:
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.
 
S

Steve Pope

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
 
J

Jacek Dziedzic

Steve said:
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.
 
C

Chris Stankevitz

osmium said:
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 ~
$
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top