This is a very noob question, but I am curious to know the reason behind this: -If I debug the following C++ code:
void floatreturn(float i){
//nothing
}
int main(){
float a = 23.976;
floatreturn(a);
return 0;
}
Monitoring the passed value of a, it appears to be 23.9759998 when entering floatreturn. As a result, any processing of the value in the function would require to manually tweak the precision. Is there a reason for this, and any way to avoid it?
23.976, but I don't notice any change even when switching todouble. ideone.com/7NgWb023.976is a double, not float. Usefloat a = 23.976f;instead. But you'll never get the exactly result in binary floating point0.976as a binary fraction, and you'll see the representation is infinite (the same reason that the fraction one-third in decimal has an infinite representation of0.3with the3repeating forever). So neither0.976nor23.976can be exactly represented in any floating point type (with a base-2 mantissa) and a nearest approximation is stored. On your platform, the nearest approximation represented by afloatis apparently what you are seeing. (There is also a minor wrinkle that23.976has typedouble, and more precision may be lost on converting tofloat)