tags:

views:

51

answers:

1

Try running this in a .VBS file

MsgBox(545.14-544.94)

You get a neat little answer of 0.199999999999932!

This rounding issue also occurs unfortunately in Sin(2 * pi) since VB can only ever see the (user defined) variable pi as accurate as 3.14159265358979.

Is rounding it manually (and loosing accuracy) the only way to improve the result? What is the most effective way of dealing with this kind of problem?

+2  A: 

That's the way floating point numbers work, small inaccuracies are to be expected. For nicer output it's easiest to just display the results rounded to fewer digits.

sth
+1 It's explained very nicely here (slightly tongue in cheek but very nice explanation) http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx
MarkJ