round function cannot be found in Win32 C++ project even with cmath include

2.8k views Asked by At

I created a blank Win32 C++ project. The compiler keeps on giving C3861 undefined error for the round function even if I include the math.h or cmath library.

I have tried the following

1. adding the /TC compile as C++ and using cmath
2. adding the include _MATH_DEFINES_DEFINED 
2

There are 2 answers

1
BeyelerStudios On BEST ANSWER

Visual Studio 2012 (MSVC 11.0) doesn't strictly follow C++11, so it may be that it doesn't have std::round.

Use

inline double round(double value) { return value < 0 ? -std::floor(0.5 - value) : std::floor(0.5 + value); }
// analogously for float
1
THellier On

Are you trying to round to an int?

std::round() does not return an int value,

instead try:

int a = int(std::floor(var + 0.5));

For more explanation: http://en.cppreference.com/w/cpp/numeric/math/round