操作系统之间的Exp()和Log()实现不同

时间:2017-10-16 15:29:40

标签: c# python c++ mono operating-system

我是开发科学软件的团队的一员,因此我们最优先考虑结果的可重复性。我们注意到,取决于使用的操作系统,软件会产生略微不同的结果。我发现在极少数情况下,exp和log函数给出的结果与double的最后一位有差异。

这里我有一个关于c#的例子,但至少可以在c++ or python上重现。所有测试都在同一台机器上完成。

using System;

namespace Test {
    public class Program {
        public static void Main() {
            byte[] input = { 14, 243, 143, 0, 124, 41, 85, 64 };
            double inputD = BitConverter.ToDouble(input, 0);
            double outputD = Math.Exp(inputD);
            byte[] output = BitConverter.GetBytes(outputD);

            Console.WriteLine("Math.Exp(" + inputD + "\thex: " + BitConverter.ToString(input).Replace("-", " ") + ")\t=\t" +
                              outputD + "\thex: " + BitConverter.ToString(output).Replace("-", " "));

            input = new byte[] { 198, 77, 75, 30, 56, 151, 18, 65 };
            inputD = BitConverter.ToDouble(input, 0);
            outputD = Math.Log(inputD);
            output = BitConverter.GetBytes(outputD);

            Console.WriteLine("Math.Log(" + inputD + "\thex: " + BitConverter.ToString(input).Replace("-", " ") + ")\t=\t" +
                              outputD + "\thex: " + BitConverter.ToString(output).Replace("-", " "));
        }
    }
}

Windows 10.0.15063,mono 5.2.0:

Math.Exp(84.6481934934384 hex: 0E F3 8F 00 7C 29 55 40) = 5.7842004815199E+36 hex: 9A 64 2E 68 FC 67 91 47
Math.Log(304590.029584136 hex: C6 4D 4B 1E 38 97 12 41) = 12.6267219860911 hex: 14 E4 43 B4 E1 40 29 40

Ubuntu 16.04,mono 5.2.0.224:

Math.Exp(84.6481934934384 hex: 0E F3 8F 00 7C 29 55 40) = 5.7842004815199E+36 hex: 99 64 2E 68 FC 67 91 47
Math.Log(304590.029584136 hex: C6 4D 4B 1E 38 97 12 41) = 12.6267219860911 hex: 15 E4 43 B4 E1 40 29 40

你能否提出任何想法如何应对?如何使这些基础在不同的操作系统上以相同的方式运行?

0 个答案:

没有答案