views:

108

answers:

4

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?

+1  A: 

Most of the systems out there implement IEEE 754, which is not terribly great but is good enough for most purposes. If you want better precision then look at something like GMP.

Ignacio Vazquez-Abrams
+3  A: 

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.

Billy ONeal
@duffymo: Thanks for fixing that :) Perhaps I shouldn't answer SO questions while working on other things.
Billy ONeal
Actually, I would think it more likely that PHP uses SSE2 for double arithmetic. I expect that Excel has a horrible legacy burden of mandated bug-for-bug compatibility with older versions, whereas PHP most likely does not.
Stephen Canon
@Stephen Canon: Gains from SSE are only possible if calculation can be parallelized 4 ways. Since PHP has no parallelization primitives, I don't think implementing SSE would even be a gain there. Of course I could be 110% wrong.
Billy ONeal
@Billy ONeal: That's not true; SSE has scalar operations as well as vector, and they are often faster than using the x87 FPU even without vectorization.
Stephen Canon
@Stephen Canon: When you've got lots of data to process, yes. But there are costs to switching things around into SSE mode. Anyway, this entire discussion isn't relevant. It doesn't matter which program implements what -- so long as they do things differently there will be differences in observed behavior. That is the point.
Billy ONeal
A: 

Be very careful when dealing with PHP float calculations. PHP is a flexible scripting language, but its flexibility and loose typing can cause hazards when casting between types or reading in data.

I highly suggest you read the pages on floats in the PHP manual. There is a module called BC Math that is provided for arbitrary precision calculations, and depending on what you are doing, you may want to take advantage of it.

zombat
A: 

For longer but not arbitrary precision, try qd.

lhf
Why the down vote? My answer was a reply to the answer by Ignacio Vazquez-Abrams; perhaps it should have been a comment there.
lhf