Easy: 20.45 cannot be represented as a finite power of 2. Therefore the result can only be approximate. I am surprised it's the first time you see this in all your long years of computing...
Cool! Thanks fot the quick reply.
As for your surprise - You shouldn't be. There are lots of people with years of experience in software development(much more than me) that have never crossed path with this thing. It's all about the projects you did. There's always a little tidbit that someone would know that another person wouldn't.
It's also worthwhile to note that the expressions "21 - 5.45" and "21 - 20.45" are evaluated at compile-time and emitted into the IL as constants. The constants look like the following:
15.550000000000001
0.55000000000000071
More fun with floats:
dim i as single
dim counter as integer
for counter = 1 to 10
i += .1
Console.WriteLine(i)
next
good times.
I *learnt* this a couple of years ago when I was messing around with the following snippet of Javascript:
var openingBalance = parseFloat(1234.00)
var closingBalance = parseFloat(openingBalance - 1)
// now add 10 lots of .1 to bring our closing
// balance back up to our opening balance
for(var i = 0; i < 10; i++)
{
closingBalance += 0.1
}
// What will it be?
// Are they equivalent or not??
alert(openingBalance == closingBalance) ;
return true ;
It is just not right to use any comparison operation between float type figures the mentioned way.
In computer world it has to be
(floatValue1 - floatValue2)< Epsilon
where Epsilon is some small quantity, for ex. 0.00000001 - it will be your calculation accuracy.
The very first "21 - 20.45" problem is the typical in computer mathematics.
For the similar reason it is sometimes not possible, believe me or not, to calculate a determinant of a 3d matrix on a computer, but it would take just 3 minutes to find it by a human brain.
That problems in math is called "error of a method".
well i walked in to the town center and i sliped on a banana skin