0

Result of calculation shown below should be equal to 0,007306897 (I referred from 2 books) . But when i check result from Watch 1, i see that result is equal to 0,007306882. I split the process into some parts. And the problem is occurring when c is calculating.

///Declarations
double sigma = 1.00000000;
double a,b,e,c;
a = (1 / Math.Sqrt(2 * Math.PI)); //calculated properly
c = -(i * i + j * j) / 2.00000000 * (sigma * sigma); //i and j are equal to -2
e = Math.E; //calculated properly
b = Math.Pow(e, c); 
result=a * b;
  • [The answer should round to `0.007306883`](https://www.wolframalpha.com/input/?i=1%2Fsqrt(2pi)*e%5E(-(-2+*+-2+%2B+-2+*+-2)+%2F+2+*+1+*+1)). – Sweeper Jul 27 '19 at 08:52
  • You need to learn about the basics of floating point types and their [limitations](https://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise)! – TaW Jul 27 '19 at 08:54

1 Answers1

0

Unfortunately the double type is not highly accurate. The link below shows that some numbers like 1.05 can not be stored accurately by the double type.

http://www.binaryconvert.com/convert_double.html

For normal usage it is usually accurate enough, if you need accuracy to the level you describe you probably need to use something like binary coded decimal. That will mean the normal math library won't work. You could try asking on some physics sites to see what they use when modelling complex systems to maintain accuracy.

Paul McCarthy
  • 818
  • 9
  • 24