相关疑难解决方法(0)

round()for C++中的float

我需要一个简单的浮点舍入函数,因此:

double round(double);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1
Run Code Online (Sandbox Code Playgroud)

我能找到ceil()floor()在math.h中-但不是round().

它是以另一个名称存在于标准C++库中,还是缺少?

c++ floating-point rounding

227
推荐指数
11
解决办法
36万
查看次数

为什么GCC允许在C++中使用round(),即使使用ansi和pedantic标志?

有没有一个很好的理由为什么这个程序在GCC下编译,即使使用-ansi-pedantic标志?

#include <cmath>

int main (int argc, char *argv [])
{
     double x = 0.5;

     return static_cast<int>(round(x));
}
Run Code Online (Sandbox Code Playgroud)

用它来编译干净(没有警告,甚至)g++ -ansi -pedantic -Wall test.cpp -o test.

我看到两个问题:

  1. round() 在ISO-conformant模式下不应该对C++可用(因为它来自C99)
  2. 即使round()在这种情况下可用,它也应该只是std命名空间

我错了吗?

c++ gcc rounding c++98

6
推荐指数
1
解决办法
1704
查看次数

标签 统计

c++ ×2

rounding ×2

c++98 ×1

floating-point ×1

gcc ×1