I am seeing some strange behaviour when rounding floating point numbers in my Ada code.
I understand that floats are not reliable, and that sometimes they aren't represented exactly.
My question is around why it would matter if you store the value in a variable first.
float'rounding(8.9 / 0.2)
the above comes out as 45, which seems to make sense, 8.9 / 0.2 = 44.5 then round away from zero. (I also tried casting each number to a float to make sure there wasnt some type difference)
but then
a : float : 8.9;
b : float : 0.2;
float'rounding(a / b)
this time it comes out as 44. which means that a / b = 44.4999999 then round down.
Why does saving the values in a variable change the outcome of the calculation when its all the same types?!
Any insight would be amazing!
8.9, 0.2, 8.9 / 0.2aredoubleprecision anda / bis single and so slightly different quotients,. Not sure if that applies to Ada. - chux - Reinstate Monicafloat(8.9) / float(0.2)and still got 44.5 - The Iron Ducklingfloat(8.9)/float(0.2)involves double rounding: 8.9 to double precision then single, so the result might be different from8.9f/0.2fwhere the literals are rounded to single precision directly - phuclv