0

I received an error in a condition where I need a substraction result to be less or equal to zero. The specific values are (obtained via var_dump):

$arr_charges[total] = float(25.63)
$arr_credits[total] = float(25.63)
($arr_charges[total] - $arr_credits[total]) = float(3.5527136788005E-15)

I would expect ($arr_charges[total] - $arr_credits[total]) = float(0), because both values are the same number. I understand this issue can happen with different numbers, due to the precision problem with float numbers, but I think this is strange when the same number is used as minuend and subtrahend.

I'm using PHP:

> php --version
PHP 5.4.6-1ubuntu1.8 (cli) (built: Apr  4 2014 01:28:36)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v2.0.0, Copyright (c) 2005-2012, by mOo

Is this normal? or is this a PHP bug?

There seems to be a way to fix the result, and that is to apply a cast to int. In my case it works, but that might be not enough in some cases.

UPDATE 05/01/2014 - 1

Thanks for your kind answers. Here is a piece of code related:

$arr_charges['total'] = 25.63;
$arr_credits['total'] = 25.63;

return (($arr_charges['total'] - $arr_credits['total']) <= 0 );

This code is returning false, because the substraction operation returns 3.5527136788005E-15, as explained above in the var_dump results.

Yes, I know there is an issue with floating point precision, but I wouldn't expect that issue to happen in a case like the exposed above. Even so, I ask because I would like to know if there is serious a way to use this kind of operations with floating point values.

Thanks.

David
  • 1,282
  • 3
  • 18
  • 40
  • 3
    If you understand why it is so (precision) - then why ask? __Yes__ this is because of frloating point precision issues. __No__ - you should not compare floats on equality – Alma Do May 01 '14 at 17:50
  • [Unable to reproduce this issue](http://3v4l.org/m2ngD). – Amal Murali May 01 '14 at 17:50
  • @AlmaDo: I don't think it is related. And where is the comparison being done in this question? – Amal Murali May 01 '14 at 17:51
  • You don't show any valid code. http://sandbox.onlinephpfunctions.com/code/0af961a6b81979e2c0c815688f694d1d0a34e993 – AbraCadaver May 01 '14 at 17:52
  • @AmalMurali it __is related__. Once the data isn't precise, it affects __all__ related stuff. As, for example, equality checks (and there __is__ a comparison in OP's question) – Alma Do May 01 '14 at 18:23

1 Answers1

0

You can test the equality by approximating wich should avoid the floating point error

function isEqual($a, $b)
{
    return abs($a-$b) <= 0.00001;
}
Tofandel
  • 3,006
  • 1
  • 29
  • 48
  • It may work in some cases but it's not a good way to compare floating-point values. What if those values need to be more precise than 0.00001? [Most effective way for float and double comparison](http://stackoverflow.com/q/17333/995714), [How to correctly and standardly compare floats?](http://stackoverflow.com/q/4548004/995714) https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ – phuclv Oct 10 '15 at 13:07