differnece between cmath and math.h

with math.h this n = n + pow(2, i) * atoi(x.c_str()); works, but with cmath it doesent. Why is that?
Last edited on
math.h is the deprecated C header. cmath is the C++ header. The difference is that cmath puts all the names in the std namespace.

n = n + std::pow(2, i) * std::atoi(x.c_str());
Peter87 is correct, although <cmath> is of course a whole lot more than just math.h in namespace std. But to answer the question in a way that's useful to you, define "doesn't work". If you're getting a compilation error, post the error message.

I am going to guess that your C++ compiler is pre-C++11 and that your i has type int.

<math.h> has only one overload of pow(), for double arguments. The compiler builds an implicit conversion from int to double and compiles the call.

<cmath> before C++11 had six overloads of std::pow(), none of which accepted int first argument. The compiler would not be able to decide whether to convert 2 to double or to float or to long double, and fail.

If that is your problem, use pow(2.0, i)
Last edited on
i have code::block, i dont know if it supports c++11 or not.
Topic archived. No new replies allowed.