Search code examples
phpfloating-pointfloating-accuracyfloating-point-exceptions

Subtracting close together numbers in php


Why does

<?php echo 194.95-194.94; ?>

output

0.0099999999999909

What am i missing here? This is php 5.2.


Solution

  • The issue is that you cannot represent 0.01 exactly in floating point.

    Have a look at what every programmer should know about floating point for a great explanation of why this is, and what to do about it.