0

I am just doing some simple arithmetic calculations in PHP and getting very strange results. Below, I have mentioned what I am doing and what result I am getting:

1. $a = 191.39 - 147;
2. echo $a; //Print 44.39
3. echo (191.39 - 147); //Print 44.39
4. echo -44.39 + 44.39; //Print 0
5. echo -44.39 + $a; //Print -1.4210854715202E-14
6. echo -44.39 + (191.39 - 147); // Print -1.4210854715202E-14  

Can someone please tell me, why I am getting different value in Line 5 and 6 as compared to Line 4? Because I am doing same thing in Line 4,5 and 6 but results are different.

Thanks in advance

Dhirender
  • 604
  • 8
  • 23
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Jeto Oct 31 '20 at 07:51
  • [Also relevant.](https://stackoverflow.com/questions/3726721/php-floating-number-precision) – Jeto Oct 31 '20 at 07:52

1 Answers1

1

I believe this is to do with floating point precision as the 'weird' answer you are getting is just an incredibly small decimal.

See the PHP manual on floating point numbers

Specifically, the big red warning box about precision.

jacc_01
  • 310
  • 1
  • 2
  • 10