[RESOLVED] Single Variable Type - odd amount?
Dim Bal As Single = SumOfCash - (CashSales + StartAmt)
SumOfCash = 272.85
CashSales = 122.85
StartAmt = 150.00
Bal = .00000725 ???
so.. Bal <> 0 when it should be...
Any ideas?
I solved the issue by using Math.round but i am curious as to what could cause it?
Dim Bal As Single = Math.Round(SumOfCash - (CashSales + StartAmt), 2)
Re: Single Variable Type - odd amount?
Binary fractions are always likely to be inaccurate especially when dealing with decimals. Single and Double take no account of the inherent contradictions so if you're dealing exclusively with currency you should always declare all relevant variables as Decimal (remember that the default for literal values is always Double!)
Re: Single Variable Type - odd amount?
ahh.. ok cool!
Thanks Dun!
Re: [RESOLVED] Single Variable Type - odd amount?
There are a couple of reasons that you can get these floating point errors. The first and most obvious is that there is a limited number of bits allocated to represent the number and since there are an unlimited number of numbers between 0 and 1 not everyone of these can be represented. But there is another reason, which is more common and that is that you simply can't exactly represent certain numbers in the binary system. Just like in the decimal system you can't accurately write the number 1/3 (one third) since it would be 0.33333333...... in the same manner you can't write 1/10 (one tenth) in binary format (using the IEEE 754 standard used for storing the Single data type).
The System.Single data type is an approximation of a floating point number so you must always treat its value as an approximation.