错误 C3861:"roundf":找不到标识符

error C3861: 'roundf': identifier not found

本文关键字:标识符 找不到 roundf 错误 C3861      更新时间:2023-10-16

我通常擅长谷歌搜索这样的东西,但这次我似乎找不到任何东西。

我从这里下载了一些源代码,它使用一个名为 roundf 的函数。

我已经有#include <math.h>,作为第一个想法添加了#include <cmath>但仍然有问题。我似乎找不到函数的来源...

有替代功能吗?或者有谁知道它来自哪里,以便我可以包含头文件?

roundf()函数由 C99 定义,但 MSVC 实现的 C99 很少,因此它不适用于Microsoft编译器。

您可以使用这个:

float roundf(float x)
{
   return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
}

您还可以使用提升库:

#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