问题描述
我在我的项目中使用第三方库获取BigFloat值(我使用了这两个库https://github.com/Osinko/BigFloat和https://www.nuget.org/packages/BigFloat)。我在机器上得到的结果就是想要的结果;但是,当我在其他计算机上运行该应用程序(在3台不同的计算机中测试)时,该应用程序给出的结果却有所不同(所有三个给出的数字都相同,这与我在计算机中获得的值有所不同,对于我的应用程序)。 我在有关浮点计算的不同机器上研究了关于不同结果的各种文章,但是在这三台机器上产生的结果都是相同的。所以,我想知道我是否缺少什么。或者,如何解决这个问题?
执行计算的函数部分是(thisItemCount,count_system,userDuration,repairTime,MTBFValue和workingRate值是我通过表单从用户输入中获得的值的两倍):
// Calculate the effective total failure rate
double FailureRate = (thisItemCount * Count_system * useDuration * repairTime) /
(MTBFvalue / workingRate);
//FailureRate = Math.Round(FailureRate,8,MidpointRounding.AwayFromZero);
// Calculate the required number of spares
int n = 0;
BigFloat failureProbability = 0;
BigFloat adequacy = 0;
for (int j = 0; j < 150; j++)
{
try
{
BigFloat power = BigFloat.Pow(FailureRate,n);
failureProbability = power / Factorial(n);
adequacy += failureProbability;
BigFloat Exp = (BigFloat)(Math.Exp(-FailureRate));
BigFloat FA = Exp * adequacy;
double Level = _spareAvailability / 100;
BigFloat AvailabilityLevel = new BigFloat(Level);
if (FA >= AvailabilityLevel)
{
Console.WriteLine("Break");
break;
}
n++;
}
catch (Exception ex)
{
Console.WriteLine("Didn't calculate this row");
Console.WriteLine(ex);
}
}
result.Text = n.ToString();
解决方法
这可能是这台机器的文化。
在源代码中显示区域性,然后重试。
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); // your culture