Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C3861: 'roundf': identifier not found

Tags:

c++

I'm normally good at googling stuff like this but I can't seem to find anything this time.

I downloaded some source code from here and it uses a function called roundf.

I already have #include <math.h> and as a first thought added #include <cmath> but still have the problem. I can't seem to find out where the function originates...

Is there an alternative function? Or does anyone know where it comes from so I can include the header file?

like image 332
Reanimation Avatar asked Nov 09 '13 23:11

Reanimation


2 Answers

The roundf() function is defined by C99, but MSVC implements very little of C99, so it is not available with the Microsoft compilers.

You can use this one:

float roundf(float x)
{
   return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
}
like image 160
nos Avatar answered Oct 20 '22 14:10

nos


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 25
vitperov Avatar answered Oct 20 '22 13:10

vitperov