cmath vs math.h

I just want to use power function of math library. It do not work with #include <cmath> but it works with #include <math.h>.

please explain!. I am using Dev-C++ 4992. As i've read some books, they said that .h is replace by c-prefix. That is why i wonder.

I've found some solutions:

#include <cmath>
cout << pow(3,2); // this line error

cout << pow(3, 2.0); // ok

cout << pow(3.0, 2) ; // ok

but, however all of above code is ok with math.h

This is actually quite interesting and works differently on Microsoft Visual Studio 2008 and Dev C++(using mingw);

1. Microsoft Visual Studio 2008 cmath is basically a wrapper that calls math.h.
In math.h if running in C mode you only get one power function pow(double, double).
In C++ mode (which we are using) you get the c++ overloaded functions:
long double pow(long double,int), float pow(float,int), double pow(double,int) and a few others.
So calling pow(int, int) for example pow(3,2) will always fail due to ambiguity whether you include cmath or math.h

2. DEV C++ with MINGW
With this set up, math.h just contains the the usual C function
pow(double, double) - so all the functions work because with pow(int, int) both ints get promoted to double by compiler and all is OK
cmath in more than a wrapper for math.h. First it includes math.h and then undefines a whole lot of stuff that math.h defined, and substitutes the c++ versions.
This includes the pow function declaration.
As the c++ overloaded functions (same as any other c++ compiler), you will get the ambiguity problem - when using pow(int, int).

P.S The ambiguity occurs with pow(int, int) because integers can be promoted to floats or doubles, which means that pow(int, int) can fit any of the 6 or so overloaded c++ pow function - so the compiler gets confused.
Topic archived. No new replies allowed.