PHP math gets crazy, need explanation, my brain is melting

Posted by derei on Stack Overflow See other posts from Stack Overflow or by derei
Published on 2013-11-05T21:46:51Z Indexed on 2013/11/05 21:53 UTC
Read the original article Hit count: 235

Filed under:
|
|

I know that playing with php float can give strange results if you try to add "goats + apples", but please take a look to the following case:

$val = 1232.81;
$p1  = 1217.16;
$p2  = 15.65;

$sum = $p1 + $p2;
$dif = $val - $sum;

echo $dif;

It will give you -2.2737367544323E-13 ... yeah, ALMOST zero, but then why it doesn't say 0 ? This freaks me out big time. Please, I need some valid explanation.

© Stack Overflow or respective owner

Related posts about php

Related posts about math