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: - std::vector<int> increments(static_cast<int>(log(a.size() + 1)/log(2)));
-
for (int i = 0; i < increments.size(); i++)
-
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 - 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?
9 23288
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. - increments[i] = static_cast<int>(pow(static_cast<double>(2), i)+.005);
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.
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.
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: - int pow(int base, int power) {
-
if (power == 0) return 1;
-
if (power == 1) return base;
-
if (power % 2 == 0) return pow(base * base, power / 2);
-
if (power % 2 == 1) return base * pow(base * base, power / 2);
-
}
Annoying, if you ask me.
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.
I had the same result doing this lab, and eventually got it without nasty casting:
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: -
double pow(double, double);
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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)
|
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)?...
|
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...
|
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...
|
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 )
|
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...
|
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;...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |