0
Dim totinr As Double
totinr = 325.0 * 80.6

When i run above code (programatically using immediate window etc.) i got output like this 26194.999999 but manually or using calculator result come like this whole value 26195. How this happen? how calculation perform in .NET for the above two value multiplication? only particular values multiplication is different in .NET otherwise 99% multiplication is correct

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Ravi
  • 105
  • 2
  • 3
  • 9
  • 3
    There are loads of questions like this, but they're not always easy to find. See http://csharpindepth.com/Articles/General/FloatingPoint.aspx and http://csharpindepth.com/Articles/General/Decimal.aspx - they're from a C# perspective, but the basis is the same. – Jon Skeet Dec 13 '11 at 05:27
  • [`Double.Epsilon`](http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx) – Uwe Keim Dec 13 '11 at 05:31
  • 1
    @UweKeim: That's almost certainly *not* a useful link, to be honest - especially with absolutely zero explanation. – Jon Skeet Dec 13 '11 at 05:50
  • 1
    @UweKeim: you're correct - but Ravi needs more background information (like "what is a 'Double', and how does it work?") before he can make use of "Double.Epsilon". The C# In Depth articles cited by Jon Skeet and me are much more appropriate. IMHO... – paulsm4 Dec 13 '11 at 06:38
  • Thanks to all i learned lot of things using above links – Ravi Dec 13 '11 at 06:55

2 Answers2

1

One of the most important things you need to learn in programming - any language - is that floating point arithmetic is an approximation!

This is not a "bad thing", and it certainly shouldn't be a "surprising" thing.

Please read this link:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Fun fact: Running this in Mathematica (which supports arbitrary precision) gives the value `26194.999999999996`. If you do this anywhere you'll get more or less the same thing. – Mike Bailey Dec 13 '11 at 05:28
0

Great answer here: https://stackoverflow.com/a/618596/540339

It goes through the accuracy and when to use Float, Double or Decimal.

Community
  • 1
  • 1
Adam
  • 16,089
  • 6
  • 66
  • 109