Reputation: 29
This situation really confused me: I have one .NET application doing some floating number calculation. It seems having problem to do the division, Math.Pow, and Math.Exp. For example:
double _E1 = 20616579.5;
double sub = 19000;
double total = 19623;
double percent = sub / total; //0.96825152635574341
double _result1 = Math.Pow(_E1, percent); //12078177.0
double _result2 = Math.Exp(percent * Math.Log(_E1)); //12078184.730266357
all three results, percent, _result1, and _result2 are incorrect (you can use calculator to verify).
I have another .NET program running the same code, on the same machine, gives the correct results:
By just looking at the result for percent, the precision only goes to 7 decimal digits. The Double usually goes to 16 decimal digits.
I have another even simpler example as follows:
_outcome equals to some ridiculous number. but it shows correct result when I put cursor on top of "*".
Please help, drove me crazy in the last few days.
UPDATE: the problem solved. DirectX was the culprit. see: Can floating-point precision be thread-dependent?
Upvotes: 1
Views: 241
Reputation: 152491
Your exact code doesn't compile (you have percent
declared twice). I suspect that there's a conversion somewhere else that you haven't posted (perhaps a conversion to float
somewhere that will reduce precision to 6-7 digits).
When I run your code (minus the extra declaration of percent
) in LinqPad I get the correct result:
void Main()
{
double _E1 = 20616579.5;
double sub = 19000;
double total = 19623;
double percent = sub / total;
double _result1 = Math.Pow(_E1, percent);
double _result2 = Math.Exp(percent * Math.Log(_E1));
percent.Dump(); // 0.968251541558375
_result1.Dump(); // 12078180.3702605
_result2.Dump(); // 12078180.3702605
}
Upvotes: 1
Reputation: 956
Double only has the precision of 15-16 digits. I think this is where you get your error.
Try using Decimal (http://msdn.microsoft.com/en-us/library/364x0z75.aspx) instead. Can handle 28-29 digits.
Upvotes: 0