当我执行以下操作时:
long x1 = LONG_MAX;
float x2 = x1;
long x3 = x2;
我得到以下信息:
x1 = 2147483647
x2 = 2147483648
x3 = -2147483648
有什么方法可以从 x1 到 x2 向 0 舍入(甚至简单地向负无穷大舍入)?
最佳答案
也许您对标准 IEEE 754 rounding modes 感兴趣?
该链接指向 libc;我不确定这是否已经标准化为语言本身的一部分。
我想这会起作用(未经测试):
#include <math.h>
int save_round_mode = fegetround();
fesetround (FE_TOWARDZERO);
/* do stuff here */
fesetround (save_round_mode);
关于c++ - 将整数舍入为浮点型并向 0 舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741584/