views:

352

answers:

7

When I moved a program from a Mac to this Windows PC, the VC++ 2008 compiler is giving me errors for passing unsigned ints to the cmath pow() function. As I understand, this function is not overloaded to accept anything but floating-point numbers.

Is there some compiler flag/setting that will ignore these errors? Also does anyone know how to find the documentation on the VC++ compiler?

Thanks!

EDIT: This isn't a warning, it's an error. However, for me it's not an issue since my program is only dealing with numbers that come out as integers, so I don't care that they aren't floats. If it was just warnings I would move on with my life, but it's not letting me compile. Can I suppress errors somehow? Like I said, the errors aren't coming up on my Mac and the program is fine.

A: 

Don't ignore warnings, fix them. Cast your number to a float with static_cast before you call pow().

GMan
A: 

I don't know of a flag, but getting rid of the warnings was easy enough for me. Just double click on each of the warnings in the "Task List" and add the appropriate casting, whether you prefer

(double) my_variable

or

static_cast<double>(my_variable)

I'm guessing if you're getting the ambiguous warning, there are multiple pow functions defined somewhere. It's better to be explicit in my opinion anyway. For what it's worth, my vote goes with the static_cast option.

Jeffrey Martinez
A: 

As Mehrdad mentioned, use the #pragma warning syntax to disable a warning. Documentation is here - http://msdn.microsoft.com/en-us/library/2c8f766e.aspx

I would be inclined to fix the warnings rather than hide them though!

Mark Ingram
+4  A: 

Jesus Christ - why are you all telling him how to turn off this warning!??

His code is broken - he's passing an unsigned int instead of a float!

You should be telling him to fix his code!

Additional:

EDIT: This isn't a warning, it's an error. However, for me it's not an issue since my program is only dealing with numbers that come out as integers, so I don't care that they aren't floats. If it was just warnings I would move on with my life, but it's not letting me compile. Can I suppress errors somehow? Like I said, the errors aren't coming up on my Mac and the program is fine.

Integers and floats use different representations internally. If you have the same number in an int and a float, the bit pattern inside the storage for them is completely different. You cannot under any circumstances whatsoever expect your code to work if you are passing an integer when you should be passing a float.

Furthermore, I assert your Mac code either is silently using an overloaded version of that function (e.g. you are on that platform compiling with C++) or you believe it works when in fact it is working by chance or is not actually working.

Additional additional:

No compilers ever written has the ability to turn off errors.

A warning means the compiler thinks you're making a mistake.

An error means the compiler doesn't know what to do.

Blank Xavier
Exactly. This is why software sucks.
John Dibling
Read my edit. The result of pow in my program will always be an integer, I don't care that it's wrong and it's not "broken". My issue is that VC++ gives me errors where Eclipse on my Mac doesn't. Also my pow doesn't seem to be overloaded for uints, which is also annoying.
Tony R
@Tony: Your code is broken if you pass an int to a function expecting a double. Not all compilers enforce this rule, but according to the C++ language, yes, your code is broken. Luckily, it is also easy to fix
jalf
@Tony: You have an unsigned int. You are sending it to a function that expects a float. The compiler complains. Your code is broken. It's really just that simple.
John Dibling
@Tony: the results of pow are not always going to be an integer, due to rounding issues with larger/smaller floating point numbers. You will always need to cast.
Blank Xavier
Thanks everyone. I guess my frustration was in the fact that it worked on Mac but not PC. I've since been informed that often the libraries on different systems are often very different, so pow may infact have been overloaded where on my PC it isn't. Same thing came up when I tried to use log2(), which exists on Mac but not on my PC.
Tony R
A: 

C++ has overloads for pow/powf for int exponents. Heed the warning.

MSN
Where? I don't see them anywhere; not on my computer in my math.h nor on the internet in any reference to pow(). Do you have a link?
Tony R
Check my answer. Include cmath instead of math.h.
jalf
Well, you can look in math.h. The documentation for pow and powf that accompanies Visual C++ also has documentation for it. But here's a link anyway: http://msdn.microsoft.com/en-us/library/dt5dakze.aspx
MSN
math.h won't help. On any compliant compiler, the overloads are only defined in cmath, not math.h
jalf
A: 

Don't ignore this or any warnings. Fix them. The compiler is your friend, trying to get you to write good code. It's a friend that believes in tough love, but it is your friend.

If you have an unsigned int and need a float, convert your unsigned in to a float.

And the MSDN Library is the documentation for both the VC++ implementation of the language and the IDE itself.

John Dibling
A: 

There are a couple of options:

In C, the solution is simply to cast the ints to doubles:

pow((double)i, (double)j)

In C++, you can do the same, although you should use a C++-style cast:

pow(static_cast<double>(i), static_cast<double>(j))

But a better idea is to use the overload C++ provides:

std::pow(static_cast<double>(i), j);

The base still has to be a floating-point value, but the exponent can be an int at least

The std:: prefix probably isn't necessary (most compilers make the function available in the global namespace as well).

Of course, to access the C++ versions of the function, you have to include the C++ version of the header.

So instead of #include <math.h> you need to #include <cmath>

C++ provides C++ versions of every C header, using this naming convention. If the C header is called foo.h, the C++ version will be cfoo. When you're writing in C++, you should always prefer these versions.

jalf