1

Possible Duplicate:
round() for float in C++

I used mingw and eclipse with c++98 on my laptop. But on my pc I have vs2012.

Now I have a small problem, the math library seems to be completely different.

in c++98 I had access to round and M_PI with math.h. But in vs2012 it isn't there anymore. I made a google search and found the following reference http://en.cppreference.com/w/cpp/numeric/math/round

But round is not in cmath and it is not in math.h either. Also I am unable to find M_PI

Is there another math library?

Community
  • 1
  • 1
Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • For `round()`, see the x-ref. C99 has `round()` in both `` and ``, but MSVC still implements the archaic C89 standard which did not have `round()`. `M_PI` is a POSIX-ish extension. It is not surprising to me that the function and macro are missing. – Jonathan Leffler Feb 02 '13 at 17:27
  • 1
    If you define `_USE_MATH_DEFINES` before including `` (or ``) then `M_PI` will be defined. They are not defined by default because they are not part of the C or C++ language standards. Visual C++ 2012 does not define the `round` function, which is not part of C90 or C++03 (it is part of C++11 by way of the C99 Standard Library, which Visual C++ does not yet implement). – James McNellis Feb 02 '13 at 17:29

3 Answers3

3

round() is indeed in <cmath>: http://en.cppreference.com/w/cpp/numeric/math/round

M_PI is not part of C or C++ standard, but something that's provided on POSIX-complient systems. MinGW tries to mimic a POSIX environment, so it provides this.

You can get a very good approximation with double pi = 4 * std::atan(1);.

Martin Green
  • 1,056
  • 8
  • 15
2

round should be in the <cmath> header (see § 26.8 of the C++11 Standard, Table 119), and if it is not, it is an implementation bug. However, the M_PI constant is not part of the C++11 Standard (see the same Table 119), although some implementations do provide it.

For instance, with GCC 4.7.2 and the implementation of libstdc++ that comes with it, this code compiles:

#include <cmath>

int main()
{
    int x = std::round(M_PI);
}

Make sure you are using the std:: namespace qualification for round().

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2

These are new in C++11. MinGW had it before because of C compat, but VS2012 is not a C99 compiler. M_PI is probably the same story. IOW, you were relying on implementation-specific behaviour with MinGW, now it's Standard behaviour but VS hasn't upgraded quite yet.

Puppy
  • 144,682
  • 38
  • 256
  • 465