VB.NET Subtraction Problem

Hi,

I'm currenty using VS.NET 2003 .NET 1.1 and am experiencing a very strange problem.

        Dim x As Double, y As Double, z As Double

        x = 10.2
        y = 3
        z = x - y

At the end of this z is always  7.199999999999993

Where do all of these decimal places come from? I'm sure there's a very simple answer or I haven't got something set right, but I can't find it.

Thanks in advance,
Jon
HBPROCKAsked:
Who is Participating?
 
Bob LearnedCommented:
http://msdn2.microsoft.com/en-us/library/x99xtshc(VS.80).aspx

<Quote>
Double precision numbers store an approximation of a real number.

When you work with floating-point numbers, keep in mind that they do not always have a precise representation in memory. This could lead to unexpected results from certain operations...
</Quote>

You only need as much precision from the result that you have with the input:

z = Math.Round(z + .005, 1)

Bob
0
 
Bob LearnedCommented:
It is based on the precision for double.  If you don't need it, don't use it--use singles instead.

Dim x As Single, y As Single, z As Single

x = 10.2
y = 3
z = x - y

Bob
0
 
HBPROCKAuthor Commented:
I see your point Bob, but in certain circumstances (i.e my calculator) I will require a dynamic range of results.

Many more DP may be required, but smaller sums like this will still need to return an accurate result without seemingly erronious variation. Using rounding is not an option...

Could you explain to me where this variation from the math logic comes from? Being a VB6 developer, I've always been used to receiving the 7.2 expected from this calculation.

Many Thanks,
Jon
0
 
HBPROCKAuthor Commented:
This is the information I needed...

Thanks!
Jon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.