-1

hello I have a problem wtich two variables in php. i have this code:

     var_dump($total);echo '<br/>';
     var_dump($reserva->getAdelanto());
     if ($total == $reserva->getAdelanto()){
        $total = 0;
        echo "hello";
        }
    else
        $total = $total - $reserva->getAdelanto();

print :

float(3940.2) 
float(3940.2)

but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.

faisbu
  • 275
  • 1
  • 3
  • 18
  • I've faced this problem in C++ too. To solve this what I do is to check their difference. for an example if(abs($x-$y)<$eps)echo "They are equal"; where $eps = 0.000000001 or the tolerance value – Fallen Jun 25 '13 at 12:00

1 Answers1

1

May be try with abs like

if ((abs($a)-abs($b)) <= 0.00001) {
   echo "same";
}

Or

if (abs($a - $b) <= 0.00001) {
   echo "same";
}

Or you also try like

var_dump( bccomp($a, $b) == 0 )

returns true if they are same

GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • 2
    it should be abs($a-$b) – Fallen Jun 25 '13 at 12:01
  • 1
    for $a = 10, $b = 5, your code will say that these 2 are same :P – Fallen Jun 25 '13 at 12:02
  • @MuhammedHedayet I have added that also...could you see once,and also my third ans – GautamD31 Jun 25 '13 at 12:05
  • well my second comment is for the first code. for $a = 10, $b = 5, abs($a)-abs($b)=-5 which is less than 0.00001 but these two are not same. We need the absolute value of the difference, not the difference of absolute values :) – Fallen Jun 25 '13 at 12:06