1

i've a question on PHP and mathematical operations.

why 9000.05 - 9000 return 0.049999999999272 and not 0.05 ???

please look that example

function calculate ( $financed, $months, $rate){
    $installment = 782.90;  
    $rate= $rate / 100;
    $delta = 0;

    $OUT = $financed;
    $IN  = 0;

    for ($t = 1; $t <= $months; $t++){
        echo "installment $t \n";
        $installment_act =  $installment / pow((1 + ($rate / 12)), $t);
        $installment_act = round($installment_act, 2);
        echo "installment actualized $installment_act \n\n";
        $IN += $installment_act;
        echo "\t\t\t\t $IN\n";
    }
    echo "\n IN ($IN) - OUT ($OUT) = ";
    $delta = $IN - $OUT;
    echo "delta = $delta\n  ";
}

calculate(9000, 12, 8);

//RESULT

installment 1 
installment actualized 777.72 

                 777.72
installment 2 
installment actualized 772.56 

                 1550.28
installment 3 
installment actualized 767.45 

                 2317.73
installment 4 
installment actualized 762.37 

                 3080.1
installment 5 
installment actualized 757.32 

                 3837.42
installment 6 
installment actualized 752.3 

                 4589.72
installment 7 
installment actualized 747.32 

                 5337.04
installment 8 
installment actualized 742.37 

                 6079.41
installment 9 
installment actualized 737.45 

                 6816.86
installment 10 
installment actualized 732.57 

                 7549.43
installment 11 
installment actualized 727.72 

                 8277.15
installment 12 
installment actualized 722.9 

                 9000.05

IN (9000.05) - OUT (9000) = delta = 0.049999999999272

as you see $IN - $OUT return a wrong result, why? and how could i fix it?

thanks advance for any suggestion Michele

Zauker
  • 2,344
  • 3
  • 27
  • 36
  • most fractional decimal numbers can **NOT** be represented exactly in floating point format. Think of fractional numbers in the same way as the old "We are Pentium of borg, precision is futile. prepare to be approximated" gag line went. – Marc B Jul 04 '13 at 16:22
  • 4
    http://en.wikipedia.org/wiki/floating_point#accuracy_problems - applies to almost every programming language known to man – Mark Baker Jul 04 '13 at 16:22
  • Use http://php.net/bcmath. Another way to handle money is to use a [Money object](http://martinfowler.com/eaaCatalog/money.html) – Gordon Jul 04 '13 at 16:25
  • 2
    ***http://php.net/float - do you see the big warning on that page?*** (common mistake) – hakre Jul 04 '13 at 16:27
  • 3
    A way to handle currency amount with upto two digit decimal place is to multiply all numbers to 100 and play with them as integers. Finally to print, divide by 100 or deliberately put a decimal before last two digits. – Shashwat Kumar Jul 04 '13 at 16:32
  • @ShashwatKumar good point, maybe convert it into an answer even – CSᵠ Jul 04 '13 at 16:33
  • @Gordon i've tried to use bcmath, only one dubt: with bcdiv with precision 4 i have this result: 727.7190 but if i use bcdiv with scale precision 2 i have 727.71... is't right to have 727.72 ? – Zauker Jul 05 '13 at 04:39
  • @ShashwatKumar thank i've think about that solution, now i'm looking for see if exist a 'better way' to solve this problem – Zauker Jul 05 '13 at 04:43

0 Answers0