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 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
doublethat Excel is using, and (if your PHP code is using one) afloatin PHP code. Ensure you aren’t mismatching these asfloats 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.