Oliver Kucharzewski
Oliver Kucharzewski

Reputation: 2655

Translating VB to PHP - Floating point issues

I'm attempting to translate my VB code into PHP, and am having issues with floating point numbers. VB seems to round a particular number while PHP doesn't.

I have multiple equations like these so need a solution which will adequately calculate them all as the exact number as VB calculates.

Is there any reason why this is occurring?

PHP

483.737 + 105678 / 293.6700134 = 843.58987968799

VBA (Excel):

483.737 + 105678 / 293.6700134 = 843.5898797

With ini_set('precision', 10)

PHP

enter image description here

VBA

enter image description here

Upvotes: 0

Views: 68

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

When dealing with floating point numbers, whenever you inspect them you ultimately deal with a “to string” somewhere, and that conversion, from a well-defined mathematical concept or international standard, tends to break things more often than not. If the math checks out, but things look weird while debugging, that is probably the case. I don't specifically know if VBA and PHP store their floating point numbers in the same way, however, so it is possible that some math could be off.

Your numbers look identical to me, although one goes to greater precision, but once again, that is a display thing.

Upvotes: 1

Related Questions