bomortensen
bomortensen

Reputation: 3396

Excel calculation in C#

I'm struggling a bit with an excel calculation that needs to be written in C#.

The calculation in excel looks like this:

=(-0,7333 * (1) ^ 2 + 3,3167 * 1 + 1,625) * 1.821 * 16/1000 * 1,125

In C# I have this:

Math.Pow(-0.7333 * (1.0), 2) + 3.3167 * 1.0 + 1.625) * 1821.3125 * 16.0 / 1000 * 1.125

The excel calculation gives me the result of 138

The C# calculation gives me the result of 179.63554194392623

Of course, the C# calculation should match the result of the Excel calculation and while I'm certainly not a genious when it comes to math, I fail to spot the difference in this calculation :-/

Can anyone point this math-blind being here in the right direction? :-)

Any help/input is greatly appreciated!

Thanks in advance.

All the best,

Bo

Edit: Thanks for all your answers! :-) I just learned something new right there.

Upvotes: 1

Views: 540

Answers (3)

Alberto De Caro
Alberto De Caro

Reputation: 5213

There is a missing parenthesis in the excel expression:

=((-0,7333 * 1) ^ 2 + 3,3167 * 1 + 1,625) * 1.821 * 16/1000 * 1,125

That returns:

179.60472015642

Otherwise the power will be applied only to "1". Moreover, in the excel formula, the term 1821 is missing decimal digits. So:

=((-0,7333 * (1) )^ 2 + 3,3167 * 1 + 1,625) * 1821,3125 * 16/1000 * 1,125

That returns:

179,635541943926

Much better!!

Upvotes: 0

Bas
Bas

Reputation: 27105

Your error was that excel will do the power before muliplication:

double q = (-0.7333 * Math.Pow((1.0), 2) + 3.3167 * 1.0 + 1.625) * 1821.3125 * 16.0 / 1000 * 1.125;

Upvotes: 1

kirilloid
kirilloid

Reputation: 14304

Change format of the cell in EXcel and it will show you the exact result.

Also -0,7333 * (1) ^ 2 in Excel is -0.7333 * Math.Pow((1.0), 2) and not as you wrote.

Upvotes: 6

Related Questions