简体   繁体   中英

Translating excel function to c# Math.pow not resulting in same values

I'm trying to translate this function from excel to C#:

=10^(0.1*(-44.1224+3.32*(-97.060520104064452*LOG(0.89*(200/160)))))

excel gives me: 1.2467E-06

var value = Math.Pow(10, 0.1 * (-44.1224 + 3.32 * (-97.060520104064452 * Math.Log10(0.89 * (200 / 160)))));

C# gives me c#value = 0.0016543260792358536

Anybody has any ideas why I don't get the same value in excel and c#?

Because C# is rounding for you.

Change this:

200 / 160

To

200.0 / 160.0

Output locally:

1.24676291550131E-06

It's because 200/160 is being cast to an integer, which truncates your result. To fix the issue, you can do as Rob suggested and write the numerator or demoniator as a floating point number. See the / operator reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM