所有系统的浮点运算都是一样的吗?(Are all the system's floating points operations the same?)

我们正在用PHP创建这个Web应用程序,当我们在报告中工作时,我们有Excel文件来比较我们的结果,以确保我们的编码正在进行正确的操作。

现在我们遇到了浮点算术的一些差异。 我们正在进行相同的划分和乘法,并且会遇到略有不同的数字,这会产生明显的差异。

我的问题是,Excel是否将它的浮点运算委托给CPU,PHP也依赖于CPU进行操作。 或者每个应用程序是否实现了自己的数学算法集?

We're making this web app in PHP and when working in the reports we have Excel files to compare our results to make sure our coding is doing the right operations.

Now we're running into some differences due floating point arithmetics. We're doing the same divisions and multiplications and running into slightly different numbers, that add up to a notable difference.

My question is if Excel is delegating it's floating point arithmetic to the CPU and PHP is also relying in the CPU for it's operations. Or does each application implements its own set of math algorithms?

最满意答案

Microsoft Excel使用特定计算机上的本机Double类型来执行其计算。 我不确定PHP正在使用什么。

但是,应该注意的是,即使在基于x86的机器上复制浮点数也可能会改变它的值。 浮点内部存储在80位宽的寄存器中。 它们以64位宽的块存储在存储器中。 因此,假设Excel和PHP都在基于x86的计算机上运行,​​即使使用类似的计算,也可以获得不同的值。 特别是在浮点类型支持的范围的极端情况下。

最后,Excel使用的double float和(如果您的PHP代码使用一个)PHP代码中的float之间存在明显差异。 确保你没有不匹配,因为float的精度要低得多。

编辑:忘了这一点 - 如果PHP或Excel(更可能是Excel)采用SSE扩展,输出会有差异,因为它们的浮点运算操作64位,而不是80位,双倍。

Microsoft Excel uses the native Double type on a particular machine to perform its calculations. I am unsure exactly what PHP is using.

However, it should be noted that even copying a floating point number on x86 based machines may change it's value. Floating points are stored internally in registers that are 80 bits wide. They are stored in memory in blocks 64 bits wide. Therefore, assuming both Excel and PHP are running on x86 based machines, you can get different values even with similar calculations. Particularly at the extremes of the ranges supported by floating point types.

Lastly, there's an obvious difference between the double that Excel is using, and (if your PHP code is using one) a float in PHP code. Ensure you aren't mismatching these as floats have much less precision.

EDIT: Forgot about this too -- there will be differences in output if either PHP or Excel (more likely Excel) is/are employing SSE extensions, as their floating point operations operate on 64 bit, not 80 bit, doubles.

所有系统的浮点运算都是一样的吗?(Are all the system's floating points operations the same?)

我们正在用PHP创建这个Web应用程序,当我们在报告中工作时,我们有Excel文件来比较我们的结果,以确保我们的编码正在进行正确的操作。

现在我们遇到了浮点算术的一些差异。 我们正在进行相同的划分和乘法,并且会遇到略有不同的数字,这会产生明显的差异。

我的问题是,Excel是否将它的浮点运算委托给CPU,PHP也依赖于CPU进行操作。 或者每个应用程序是否实现了自己的数学算法集?

We're making this web app in PHP and when working in the reports we have Excel files to compare our results to make sure our coding is doing the right operations.

Now we're running into some differences due floating point arithmetics. We're doing the same divisions and multiplications and running into slightly different numbers, that add up to a notable difference.

My question is if Excel is delegating it's floating point arithmetic to the CPU and PHP is also relying in the CPU for it's operations. Or does each application implements its own set of math algorithms?

最满意答案

Microsoft Excel使用特定计算机上的本机Double类型来执行其计算。 我不确定PHP正在使用什么。

但是,应该注意的是,即使在基于x86的机器上复制浮点数也可能会改变它的值。 浮点内部存储在80位宽的寄存器中。 它们以64位宽的块存储在存储器中。 因此,假设Excel和PHP都在基于x86的计算机上运行,​​即使使用类似的计算,也可以获得不同的值。 特别是在浮点类型支持的范围的极端情况下。

最后,Excel使用的double float和(如果您的PHP代码使用一个)PHP代码中的float之间存在明显差异。 确保你没有不匹配,因为float的精度要低得多。

编辑:忘了这一点 - 如果PHP或Excel(更可能是Excel)采用SSE扩展,输出会有差异,因为它们的浮点运算操作64位,而不是80位,双倍。

Microsoft Excel uses the native Double type on a particular machine to perform its calculations. I am unsure exactly what PHP is using.

However, it should be noted that even copying a floating point number on x86 based machines may change it's value. Floating points are stored internally in registers that are 80 bits wide. They are stored in memory in blocks 64 bits wide. Therefore, assuming both Excel and PHP are running on x86 based machines, you can get different values even with similar calculations. Particularly at the extremes of the ranges supported by floating point types.

Lastly, there's an obvious difference between the double that Excel is using, and (if your PHP code is using one) a float in PHP code. Ensure you aren't mismatching these as floats have much less precision.

EDIT: Forgot about this too -- there will be differences in output if either PHP or Excel (more likely Excel) is/are employing SSE extensions, as their floating point operations operate on 64 bit, not 80 bit, doubles.