VB.NET Single data type calculation issue

Albert Tobing picture Albert Tobing · Nov 15, 2012 · Viewed 7.7k times · Source

I want to perform a basic calculation with fractional numbers using vb.net.

Dim a As Single= 7200.5
Dim b As Single= 7150.3
Dim c As Single= a - b

'Expected result = 50.2
MsgBox(a.ToString + " - " + b.ToString + " = " + c.ToString.Trim)
'Produced result is: 50.2002

Dim single1 As Single
Dim single2 As Single
Dim single3 As Single

single1 = 425000
single2 = 352922.2
single3 = single1 - single2

'Expected result is: 72077.8
MsgBox(single3.ToString)
'Produced result is: 72077.81

How can the results be so inaccurate for such a simple calculation? The problem is solved when I change the data type to Decimal, but Decimal objects consume more memory (16 bytes). Is there any alternative data type that i can use to perform simple fractional calculations with accurate results?

Answer

Sean Airey picture Sean Airey · Nov 15, 2012

This is to do with the way floating point numbers are stored in memory, and a Single in .Net is a single precision floating point number, which is much less accurate than a Decimal or a Double for storing decimal numbers.

When the computer calculates your number, it only has binary fractions to use and in a single precision floating point number, they're not very accurate.

See http://en.wikipedia.org/wiki/Single-precision_floating-point_format for more information.

EDIT: There's some more information specific to VB.Net here: http://msdn.microsoft.com/en-us/library/ae382yt8(v=vs.110).aspx