C# is there a problem with division?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-06-06T22:43:59Z Indexed on 2010/06/06 22:52 UTC
Read the original article Hit count: 303

Filed under:

This is a piece of my code, it is called every second, after about 10 seconds the values start to become weird (see below):

double a;
double b;

for (int i = 0; i < currAC.Length; i++ )
{                 
    a = currAC[i];
    b = aveACValues[i];
    divisor = (a/b);
    Console.WriteLine("a = " + a.ToString("N2") + "\t" + "b = " + b.ToString("N2"));
    Console.WriteLine("divisor = " + divisor);
    Console.WriteLine("a+b = " + (a+b));
}

and the output:

a = -0.05 b = 0.00

divisor = 41

a+b = -0.0524010372273268

currAC and aveACValues are double[]

what on earth is going on???? The addition result is correct every time, but the division value is wrong, yet it is reading a and b correctly??

EDIT: '41' is the value of the first calculation, ie when a = currAC[0], but this should not remain???

© Stack Overflow or respective owner

Related posts about c#