我从math.h库中的round()和roundf()函数得到了意想不到的结果.以下是示例代码:
#include
#include
int main(void)
{
float f;
double d;
/* Note man page says that roundf() returns a float
and round() returns a double */
f = roundf(1.2);
d = round(1.2);
printf("%f\n",f);
printf("%lf\n",d);
return 0;
}
当我编译并运行程序时,我得到:
gcc -lm round.c
./a.out
288.000000
524288.000000
哇哇哇?
更新:除了答案,我已经确认代码在较新的编译器上正常工作. (我认为%lf不是正确的printf说明符,但在这种情况下不会影响最终结果.)我需要弄清楚为什么编译器会以这种方式运行,因为我运行的代码使用round()和已在同一台机器上编译.当我弄明白时,我会更新帖子.
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-slackware-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)