Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I rely on PHP php.ini precision workaround for floating point issue
    text
    copied!<p>I've found some workaround for <a href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" rel="noreferrer">floating point problem</a> in PHP:</p> <p>php.ini setting <code>precision = 14</code></p> <pre><code>342349.23 - 341765.07 = 584.15999999992 // floating point problem </code></pre> <p>php.ini setting, let's say <code>precision = 8</code> </p> <pre><code>342349.23 - 341765.07 = 584.16 // voila! </code></pre> <p>Demo: <a href="http://codepad.org/r7o086sS" rel="noreferrer">http://codepad.org/r7o086sS</a></p> <p>How bad is that?</p> <p><strong>1. Can I rely on this solution if I need just precise 2 digits calculations (money)?</strong></p> <p><strong>2. If not can you provide me a clear example when this solutions fails?</strong> </p> <p><strong>Edit: 3. Which php.ini.precision value suits best two digits, money calculations</strong> </p> <hr> <ul> <li>Please mind I can't use integer calculations (float*100 = cents), it's far too late for that.</li> <li>I am not going to work on numbers higher than 10^6</li> <li>I don't need to compare numbers</li> </ul> <h1>UPDATE</h1> <p>@Baba answer is good, but he used <code>precision=20</code>, <code>precision=6</code> in his tests... So still i am not sure is it gonna work or not. </p> <p>Please consider following:</p> <p>Let's say <code>precision = 8</code> and only thing I do is addition <code>+</code> and subtraction <code>-</code></p> <p><code>A + B = C</code></p> <p><code>A - B = C</code></p> <p><strong>Question 1:</strong> 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. </p> <p>Simple test would do the job:</p> <pre><code>// if it fails what if I use 9,10,11 ??? // **how to find when it fails??? ** ini_set('precision', 8); for($a=0;$a&lt;999999.99;$a+=0.01) { for($b=0;$b&lt;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"; } } </code></pre> <p><a href="http://codepad.viper-7.com/wra15R" rel="noreferrer">but obviously <code>99999999 * 2</code> is too big job so I can't run this test</a></p> <p><strong>Question 2:</strong> How to estimate/calculate when precision workaround fails? Without such crazy tests? <strong>Is there any mathematicial*, straight answer for it?</strong> How to calculate is gonna to fail or not?</p> <p>*i don't need to know floating point calculations works, but when workaround fails if you know precision, and range of A and B</p> <hr> <p>Please mind <strong>I really know</strong> cents and bcmath are best solution. But still I am not sure is workaround gonna fails or not for substraction and addition</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload