Search code examples
c#exceldouble-precisioninteger-division

Excel Spreadsheet not giving same answer as C# program


I have trouble with the following code. It is not giving the same answer in excel as C# and I'm sure the excel answer is correct. I have already tried to change integer values to double by adding a decimal point. Please advise.

the excel version

=((1+BZ21)*BX21*CG21*(B21*1))-(0.5*BZ21*BX21*CG21*((C21*0)+(0*D21)))

the c# version

voltageVector[rows, 5] = ((1.0 + ki) * Rp * c * (ma * 1.0)) - (0.5 * ki * Rp * c * ((mb * 0.0 )+ (mc * 0.0)));

The constants ki, Rp, c and ma are all double precision values.

ki = 0.69999999999999907;
Rp = 0.0469486290322602;
c = 60.0
ma = 2.0;
mb = 1.0;
mc = 1.0;

The C# answer is

9.5775203225810763

The excel answer is 9.56760000000433


Solution

  • I think something's funny in your Excel spreadsheet. When I tried this, I get the same answer in both C# and Excel. I believe the correct answer is 9.577520323. Here's a screenshot of my Excel spreadsheet:

    enter image description here