If I have two random floats or doubles that represent exact integers (within the range of a 32-bit integer), can I expect any and all addition, subtraction, and multiplication between them yield an integer float/double with no fractional part?
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?
}
Everyone understands to never trust floating-point precision, but I would like to rebuild trust where appropriate. Given that some interpreted languages (unwisely) use floats/doubles as the basis of a generic "number" type, it's important to know what operations can preserve a float's status as an integer.