c# - Casting Results of Float Multiplication Produces Differing Results if the Float is First Saved to a Variable? -


this question has answer here:

the code here straight forward don't understand results:

float percent = 0.69f; int firstint = (int)(percent*100f);  float tempfloat = percent*100f; int secondint = (int)tempfloat;  debug.log(firstint + " " + secondint); 

why firstint 68 secondint 69?

it looks compiler has figured out value of the

percent*100f 

expression using double math, , optimized away computation. not allowed when intermediate results saved in float variable.

since .69 not have anexact representation in float or in double, 2 representations "land" on different sides of int: double above, while float below actual value of .69.


Comments