0

I am doing calculation by C# on Win7.

I got a double number precision problem.

 double x = 3389774.583;

 string ss = "x is " + x + " x square is " + x * x + " , 11490571723552.8 - x*x is " + (11490571723552.8 - x * x) + "\n";

 Console.WriteLine(ss);

you may find the result is not 0 even tough 11490571723552.8 is the x*x.

I know it is the precision problem of display and calculation.

I can set up a threshold to make the result 0.

Are there other better ways ?

Thanks

user2420472
  • 1,127
  • 1
  • 12
  • 20
  • 1
    You can use `decimal` instead of `double`. It has smaller range, but the precision is much better. – MarcinJuraszek Feb 13 '14 at 20:14
  • Have a look at the many questions about floating point precision in the "Related" bar at the lower-right side of this question. – Eric J. Feb 13 '14 at 20:14
  • 2
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – p.s.w.g Feb 13 '14 at 20:14

2 Answers2

1

it's because square of 3389774.583 is not 11490571723552.8. If you will try windows calculator you will get value around 11490571723552.823889.

So there is no chance to get result 0 for (11490571723552.8 - x * x) where x is 3389774.583

Try

double x = 3389774.583;
double xx = x*x;
Console.WriteLine(xx); 

string ss = "x is " + x + " x square is " + x * x + " , 11490571723552.8 - x*x is " + (xx - x * x) + "\n";
Console.WriteLine(ss); 
Jaroslav Kubacek
  • 1,387
  • 18
  • 26
0

Try rounding the output to a number of decimals with a format string:

Console.WriteLine(ss.ToString("F2"));
Gusdor
  • 14,001
  • 2
  • 52
  • 64