I’ve found some workaround for floating point problem in PHP:
php.ini setting precision = 14
342349.23 - 341765.07 = 584.15999999992 // floating point problem
php.ini setting, let’s say precision = 8
342349.23 - 341765.07 = 584.16 // voila!
Demo: http://codepad.org/r7o086sS
How bad is that?
1. Can I rely on this solution if I need just precise 2 digits calculations (money)?
2. If not can you provide me a clear example when this solutions fails?
Edit: 3. Which php.ini.precision value suits best two digits, money calculations
- Please mind I can’t use integer calculations (float*100 = cents), it’s far too late for that.
- I am not going to work on numbers higher than 10^6
- I don’t need to compare numbers
UPDATE
@Baba answer is good, but he used precision=20, precision=6 in his tests… So still i am not sure is it gonna work or not.
Please consider following:
Let’s say precision = 8 and only thing I do is addition + and subtraction -
A + B = C
A - B = C
Question 1: Is precision workaround gonna fail for numbers between 0..999999.99, where A and B is a number with decimal places? If so please provide me an example.
Simple test would do the job:
// if it fails what if I use 9,10,11 ???
// **how to find when it fails??? **
ini_set('precision', 8);
for($a=0;$a<999999.99;$a+=0.01) {
for($b=0;$b<999999.99;$b+=0.01) {
// mind I don't need to test comparision (round($a-$b,2) == ($a-$b))
echo ($a + $b).','.($a - $b)." vs ";
echo round($a + $b, 2).','.round($a - $b, 2)."\n";
}
}
but obviously 99999999 * 2 is too big job so I can’t run this test
Question 2: How to estimate/calculate when precision workaround fails? Without such crazy tests? Is there any mathematicial*, straight answer for it? How to calculate is gonna to fail or not?
*i don’t need to know floating point calculations works, but when workaround fails if you know precision, and range of A and B
Please mind I really know cents and bcmath are best solution. But still I am not sure is workaround gonna fails or not for substraction and addition
Introduction
Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Most fractional numbers don’t have an exact representation as a binary fraction, so there is some rounding going on. A good start is What Every Computer Scientist Should Know About Floating-Point Arithmetic
Questions
Question 1
Answer 1
If you need need precise 2 digits then the answer is NO you can not use the php precision settings to ascertain a 2 digit decimal all the time even if you are
not going to work on numbers higher than 10^6.During calculations there is possibility that the precision length can be increased if the length is less than 8
Question 2
Answer 2
Output
Question 3
Answer 3
Precision and Money calculation are 2 different things … it’s not a good idea to use PHP precision for as a base for your financial calculations or floating point length
Simple Test
Lest Run some example together using
bcmath,number_formatand simpleminusBaseExample AOutput
Example BOutput
Example COutput
Example DOutput
Conclusion
Forget about floating point and just calculate in
centsthen later divided by100if that is too late just simply usenumber_formatit looks consistent to me .Update
Form
0to999999.99at increment of of0.01is about99,999,999the combination possibility of your loop is9,999,999,800,000,000I really don’t think anyone would want to run such test for you.Since floating point are binary numbers with finite precision trying to set
precisionwould have limited effect to ensure accuracy Here is a simple test :Output
Try
Output
The fact sill remains Floating Point have Accuracy Problems but for mathematical solutions you can look at
Not sure what that statement means 🙂