Search code examples
c#doublepointfloatingbigfloat

BigFloat calculations produce different results in various machines


I am using a third-party library in my project for BigFloat values (I used these two libraries https://github.com/Osinko/BigFloat and https://www.nuget.org/packages/BigFloat). The results I get in my machine is the wanted results; however, when I run the application in other machines (I test in 3 different machines) the application gave a different result (all three gave the same number which is different from the value I got in my machine and isn't an acceptable value for my application). I study various posts regarding different results in different machines regarding floating-point calculation, but the result produces in all the three machines were the same. So, I wonder if I am missing something. Or, how can I solve this problem?

The part of the function that does the calculation is (thisItemCount, count_system, userDuration, repairTime, MTBFValue, and workingRate values are double which I get from the user's entry via form):

// 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();

Solution

  • It's maybe could be the culture of this machines.

    Explicit the culture in your source code and try again.

    CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); // your culture