473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ambiguous call to pow()

Ganon11
3,652 Expert 2GB
I was working on a program this morning that required me to use the pow function (to compute 2^i at one point, and 3^i at another). I used the following code:

Expand|Select|Wrap|Line Numbers
  1. std::vector<int> increments(static_cast<int>(log(a.size() + 1)/log(2)));
  2. for (int i = 0; i < increments.size(); i++)
  3.     increments[i] = pow(2, i);
and got an error message saying "The call to pow(int, int&) is ambiguous. Candidates are:" and proceeded to list all sorts of pow functions using doubles, long doubles, floats, etc., but no int or long int versions!

Eventually I had to change that last line to

Expand|Select|Wrap|Line Numbers
  1. increments[i] = static_cast<int>(pow(static_cast<double>(2), i));
in order to get the dang thing to compile. It produced expected results, but still frustrated me.

Why is there no pow(int, int) function available in the cmath header file?
Oct 24 '07 #1
9 23288
RRick
463 Expert 256MB
For most of the functions in cmath, it doesn't make sense to have an integer version. Functions like sine and sqrt don't transfer well to integer.

They did make a version of abs for integers and longs, but pow has been left behind.

Since you are dealing with floating point values for pow, you might run into round off errors when converting to ints. It is advisable to add a small delta to pow before converting to an int.
Expand|Select|Wrap|Line Numbers
  1. increments[i] = static_cast<int>(pow(static_cast<double>(2), i)+.005);
Oct 24 '07 #2
Ganon11
3,652 Expert 2GB
I agree that sine, sqrt, and exp don't need int versions, but it makes perfect sense to raise an integer to an integer power and expect an integer to result.
Oct 24 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I recommend that you not cast in C++ unless you are on fire.

This is not a fire.

Just write a

int pow(int, int);

as suggested and off you go.

You have to remember that pow() is an old C function and C does not allow for function overloading so everyone had to type cast. Unfortunately, that is an extremely bad habit in C++.

The C++ pow() requires you use a valarray rather than an int. Personally, I think you will have your function working before you understand how to use valarray.
Oct 25 '07 #4
Ganon11
3,652 Expert 2GB
All right, I'll keep that in mind for future projects.

Will simply putting a declaration of int pow(int, int) at the top of my program work? Or will I have to provide a definition for the function?

EDIT: Wrote a simple test program to find out that I did need to provide a declaration, so I used the fast exponentiation method we learned in class:

Expand|Select|Wrap|Line Numbers
  1. int pow(int base, int power) {
  2.     if (power == 0) return 1;
  3.     if (power == 1) return base;
  4.     if (power % 2 == 0) return pow(base * base, power / 2);
  5.     if (power % 2 == 1) return base * pow(base * base, power / 2);
  6. }
Annoying, if you ask me.
Oct 25 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Will simply putting a declaration of int pow(int, int) at the top of my program work? Or will I have to provide a definition for the function?
The declaration will be enough for the compile step. But you will need the definiton to be compiled for the link step.

Your pow() should allow for negative exponents.
Oct 25 '07 #6
Laharl
849 Expert 512MB
I had the same result doing this lab, and eventually got it without nasty casting:

Expand|Select|Wrap|Line Numbers
  1. pow(2.0, i);
Oct 25 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
I had the same result doing this lab, and eventually got it without nasty casting:


Code: ( text )
pow(2.0, i);
It still is a cast. This calls:
Expand|Select|Wrap|Line Numbers
  1. double pow(double, double);
  2.  
The int i is automatically cast as a double using the compiler's C++ promotion rules. The probem is that the result is a double when an int is desired.
Oct 28 '07 #8
Laharl
849 Expert 512MB
If you know that only whole number values will be returned or that a truncated value will be sufficient, you can use floor() to take care of that.
Oct 29 '07 #9
http://www.cplusplus.com/reference/clibrary/cmath/pow/

Look at the definition for ::pow. I don't see int pow(int, int). Where are you getting this from?

Why not cast in c++? If you know what you're doing - it's no problem.

In what documentation is advising you not to cast? or is this just your personal opinion?


@weaknessforcats
Jan 16 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

52
by: Michel Rouzic | last post by:
I obtain an unwanted behavior from the pow() function : when performing pow(2, 0.5), i obtain 1.414214 when performing pow(2, 1/2), i obtain 1.000000 when performing a=0.5; pow(2, a), i obtain...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
0
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
42
by: John Smith | last post by:
In a C program I need to do exponentiation where the base is negative and the exponent is a fraction. In standard C this would be something like t = pow(-11.5, .333), but with this combination of...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
7
by: Ioannis Vranos | last post by:
In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome. Ambiguous...
32
by: Anna Smidt | last post by:
I am having an "ambiguous call to overloaded function" error again. This is the function: int nGetProfWidth (int ncols, unsigned ProfSpec) { if ((ProfSpec & PROF_2d) == 0) return ncols;...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.