0

I have made a simple code in C++

#include<math.h>

void main()
{
float a,x;

    cout<<"Enter value of a"<<endl;
    cin>>a;

    x = pow(a,0.5);
    cout<<x;
}

But it's giving me error:

Error for Pow() use with float in C++

When I press F12 and go to definition of pow(), these 6 overloads are found:

math.h pow() overloads

As we can see, it clearly has one overload for (double, double) and one for (float, float), so why does it give error when I declare a as float and works perfectly when I change its data-type to double?

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
  • 3
    The problem is that you pass a `float` as the first argument and a `double` as second argument, and no overload matches that exactly so one of the arguments have to be converted, but the compiler can't decide which conversion to make for you. – Some programmer dude Mar 03 '15 at 13:56
  • 0.5 is probably ambiguous. Modify it to 0.5f so it will be interpreted as a float literal. – Leandro Vaz Mar 03 '15 at 13:57
  • 2
    @LeandroVaz : 0.5 is not ambiguous - it's a literal of type `double`. – Bob Jarvis - Слава Україні Mar 03 '15 at 14:02
  • @Bob Jarvis: Yes I know, that's why I told him to change to 0.5f, so it would match the (float, float) overload. I didn't actually mean to say that it was ambiguous. – Leandro Vaz Mar 03 '15 at 14:16
  • 1
    @JoachimPileborg Unless I'm mistaken (on the overloads of `pow`), I'm surprised the standard doesn't just state to upcast the float to a double. Why would one want to downcast a double to a float without a cast? – Cole Tobin Mar 03 '15 at 16:28

4 Answers4

8

The issue here is that the literal 0.5 is a double, so you are calling pow with a set of arguments that do not unambiguously match an overload, and for which type conversions cannot be applied to unambiguously match an existing overload. You should use the float literal 0.5f instead.

x = pow(a, 0.5f);

Also note that you need to #include <iostream> for cout, cin and endl, and you have to either refer to them by their full names (std::cout, std::cin, std::endl), or use using declarations

using std::cout;
using std::cin;
using std::endl;

Finally, void main() is not one of the valid signatures for the main function. It must return int, so

int main()

or

auto main()->int
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    **auto main()->int** wow, never though about declaring entry point like so. it makes code look more like C++ – codekiddy Mar 03 '15 at 14:00
  • I have done all #include and using namespace std thing obviously – Failed Scientist Mar 03 '15 at 14:01
  • Since `int main()` is the most familiar entry point in C++, I don't recommend using this form `auto main()->int`, it's obfuscated. – masoud Mar 03 '15 at 14:11
  • 1
    @deepmax It won't be obfuscated when people get used to it. But I wouldn't use it either, at least not at this point in time. But it is a valid form of the parameterless signature. – juanchopanza Mar 03 '15 at 14:16
  • 2
    @TalhaIrfan Do not use `using namespace std;`! http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Baum mit Augen Mar 03 '15 at 14:34
3

You're calling it with a float and a double. There isn't an overload for that combination, and several that are close enough to be ambiguous.

0.5f would have type float, if you want to use the overload with two float parameters.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

You need to explicitly call an overloaded function of pow()

x = pow(a, 0.5f);

or

double y = pow(static_cast<double>(a), 0.5);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
codekiddy
  • 5,897
  • 9
  • 50
  • 80
2

Your call doesn't exactly match any of the various definitions of pow, so the compiler is trying to see if it can automatically cast some of your arguments so it will exactly match, but here there are 6 possible definitions of pow that could match. You need to tell the compiler exactly which one you want.

What you tried to call is pow(float, double), because 0.5 is a double literal (not a float!).

If you do for example pow(a, 0.5f) then both arguments are float and it will compile. Of course you could also pick any of the other 5 overloads.

tux3
  • 7,171
  • 6
  • 39
  • 51