Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why round() is known by mingw but not by visual studio compiler

Tags:

c++

visual-c++

Sample code which is valid and gets compiled by gcc but not by VS compiler:

#include <cmath>

int main()
{
    float x = 1233.23;
    x = round (x * 10) / 10;
    return 0;
}

but for some reason, when I am compiling this in Visual Studio I get an error:

C3861: 'round': identifier not found

I do include even cmath as someone suggested here: http://www.daniweb.com/software-development/cpp/threads/270269/boss_loken.cpp147-error-c3861-round-identifier-not-found

Is this function in gcc only?

like image 796
Petr Avatar asked Mar 10 '14 16:03

Petr


2 Answers

First of all, cmath is not guaranteed to bring round into the global namespace, so your code could fail, even with an up-to-date, standards compliant C or C++ implementation. To be sure, use std::round (or #include <math.h>.)

Note that your C++ compiler must support C++11 for std::round (<cmath>). A C compiler should support C99 for round (from <math.h>.) If your version of MSVC doesn't work after the fix I suggested, it could simply be that that particular version is pre-C++11, or is simply not standards compliant.

like image 194
juanchopanza Avatar answered Oct 14 '22 00:10

juanchopanza


You also can use a boost library:

#include <boost/math/special_functions/round.hpp>

const double a = boost::math::round(3.45); // = 3.0
const int b = boost::math::iround(3.45); // = 3
like image 42
vitperov Avatar answered Oct 13 '22 23:10

vitperov