somerandomusername
somerandomusername

Reputation: 2023

Weird PHP floating point behavior

Having weird problem:

$testTotal = 0;
foreach($completeBankArray as $bank){
   var_dump($testTotal);
   echo  " + ";
   var_dump(floatval($bank["amount"]));
   echo " = ".(floatval($testTotal) + floatval($bank["amount"]))."</br>";
   $testTotal = floatval(floatval($testTotal) + floatval($bank["amount"]));

And this is output I get:

------------------//--------------------
float(282486.09) + float(15) = 282501.09
float(282501.09) + float(3.49) = 282504.58
float(282504.58) + float(22.98) = 282527.55999999
float(282527.55999999) + float(5.2) = 282532.76
float(282532.76) + float(39.98) = 282572.73999999
float(282572.73999999) + float(2.6) = 282575.33999999
float(282575.33999999) + float(2.99) = 282578.32999999 
------------------//----------------------- 

How is this possible, what am I doing wring ?

Upvotes: 1

Views: 1561

Answers (4)

Luke Mills
Luke Mills

Reputation: 1606

The other answers have given some insight into why you get this behaviour with floats.

If you are dealing with money, one solution to your problem would be to use integers instead of floats, and deal with cents instead of dollars. Then all you need to do is format your output to include the decimal.

Upvotes: 0

Daniel
Daniel

Reputation: 3806

Floats are never exact and will diff quite a bit in the long run. If you are working with precision math, please read about the bc library.

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33502

You aren't doing anything wrong. Floats are notoriously innaccurate. From the docs (In the huge red warning box):

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Upvotes: 7

Kevin
Kevin

Reputation: 1706

Classic numeric precision example. Computers store floating point numbers in binary.

The short answer is that the computer cannot accurately represent some floating point numbers in binary.

The long answer involves moving between numerical bases. If you have a float, you cannot represent it completely in binary unless the denominator contains can be broken into factors that are powers of 2.

Upvotes: 0

Related Questions