<分区>
如果我有两个代表精确整数(在 32 位整数范围内)的随机 float 或 double ,我能否期望它们之间的任何和所有加法、减法和乘法产生一个没有小数的整数 float / double 部分?
float x = randInt();
float y = randInt();
float resultAdd = x + y;
float resultSub = x - y;
float resultMul = x * y;
if(fract(resultAdd) == 0.f && fract(resultSub) == 0.f && fract(resultMul) == 0.f){
// will this section always execute, assuming no overflow occurred?
}
每个人都明白永远不要相信浮点精度,但我想在适当的时候重建信任。鉴于某些解释性语言(不明智地)使用 float / double 作为通用“数字”类型的基础,了解哪些操作可以将 float 的状态保留为整数很重要。