Basic maths - completely wrong

tomhosking

Well-known member
Joined
Aug 4, 2005
Messages
49
Programming Experience
1-3
My program calculates the difference between two measured values, to see what the error is. When I ask VB to do this, it often comes out with absurd answers.

eg:
1921.25618 - 1921.25625

gives

-6.99999999

The code is:
facediff = targetSet(q).myMeasXYZFace1.X - targetSet(q).myMeasXYZFace2.X

Where facediff is a string, and the other two are double.

 
Worked it out:

If you do not explicitly convert a double to string, it doesnt give an outright error, it just gives wrong values!
 
Doubles are not an exact number. They are an approximated value data type. This stems from the fact there is no efficient way to store the number in memory accurately. This offen leads to the type of phenomenon you are seeing. We see it all the time here too. If you don't need more than 4 decimal places, the a currency data type is the best thing to to use.

Tg
 
Back
Top