1

I have this if condition :

if ($dc_real_vol != $dc_sum_vol) {
    echo '||+++ REAL VOL ' . $dc_real_vol . PHP_EOL;
    echo '||+++ SUM VOL ' . $dc_sum_vol . PHP_EOL;
    echo '||+++ THIS TRADE IS DAMAGE ' . PHP_EOL;
}

when I checked the output :

||+++ REAL VOL 0.60533000
||+++ SUM VOL 0.60533
||+++ THIS TRADE IS DAMAGE 

why PHP consider 0.60533000 as different with 0.60533? how to make this condition marked as true?

update :

I tried solution from Loek below, and I changed my code like this :

$dc_real_vol = (float) $dc_real_vol;
$dc_sum_vol = (float) $dc_sum_vol;

echo '||*** REAL VOL ' . $dc_real_vol . PHP_EOL;
echo '||*** SUM VOL ' . $dc_sum_vol . PHP_EOL;

if ($dc_real_vol !== $dc_sum_vol) {

    var_dump($dc_real_vol);
    var_dump($dc_sum_vol);

    echo '||+++ THIS TRADE IS DAMAGE ' . PHP_EOL;
}

and here's the result :

||*** REAL VOL 0.60533
||*** SUM VOL 0.60533
float(0.60533)
float(0.60533)
||+++ THIS TRADE IS DAMAGE 

why same number, same type but PHP still recognised as different thing?

Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • Relevant reading over [here](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – DarkBee Jun 05 '18 at 13:48
  • 2
    Real numbers don’t have trailing zeros after the decimal point. So if outputting an unformatted “number” gives you `0.60533000`, then you did not have an actual numeric value to begin with, but a string. – CBroe Jun 05 '18 at 13:49
  • *"why PHP consider 0.60533000 as different with 0.60533"* -- are they numbers or strings? – axiac Jun 05 '18 at 13:49

2 Answers2

1

I can't really explain why PHP thinks the numbers are different, I just know that they in fact are different at byte level.

The easiest way I can think of is to cast both numbers to a float and then just proceed as you normally would: https://3v4l.org/uQdAP

// Note we can even use !== instead of != now
if ((float) $dc_real_vol !== (float) $dc_sum_vol) {
    echo '||+++ REAL VOL ' . $dc_real_vol . PHP_EOL;
    echo '||+++ SUM VOL ' . $dc_sum_vol . PHP_EOL;
    echo '||+++ THIS TRADE IS DAMAGE ' . PHP_EOL;
}
Loek
  • 4,037
  • 19
  • 35
  • I tried your solution, bro. but still gives me same result. – Saint Robson Jun 05 '18 at 13:53
  • 1
    That's because this isn't a solution. You are attempting to compare small numbers (most likely in a cryptocoin trading app) so just use strings as `number_format` – Mike Jun 05 '18 at 13:55
  • 1
    @SaintRobson but it works based on the code and numbers you gave us? https://3v4l.org/LAZ0b – Loek Jun 05 '18 at 13:55
  • 1
    This is a bad solution because he's using this with crypto. Due to the limitations of float precision, it's best to just keep strings and avoid casting `(float)` to the variables. – Mike Jun 05 '18 at 14:01
  • Yeah sure, but I don't know anything about crypto and that isn't in the question. Upvoted your answer since it's clear and concise, but that doesn't make mine bad based on what the question provided me with. – Loek Jun 05 '18 at 14:03
  • @Loek fair enough. – Mike Jun 05 '18 at 14:03
  • @Loek please check my update above. I tried to `var_dump` also. – Saint Robson Jun 05 '18 at 14:16
  • @Loek he's right. when I tried with `number_format`, PHP recognised as same thing. thank you for your answer and thanks to you, Mike. – Saint Robson Jun 05 '18 at 14:23
  • Glad to help. Still wondering why your updated code doesn't work though. Probably floating point precision getting in the way. – Loek Jun 05 '18 at 14:24
1

Format the numbers prior to comparison.

$dc_real_vol = number_format($dc_real_vol, 9);
$dc_sum_vol = number_format($dc_sum_vol, 9);

The actual reason your comparison isn't working is because you are most likely assigning these variables as strings. PHP is weak typing. You can verify this by checking your variables prior to comparison with var_dump. Most likely PHP will report it has typed your variables as strings. In which case, you should utilize number_format to properly compare them.

Mike
  • 171
  • 1
  • 9
  • This seems to work, even for strings: https://3v4l.org/XPOHl . Like to point out that there is not such thing as 'loose' typing however. It's either static/dynamic or weak/strong. – Loek Jun 05 '18 at 14:01
  • 1
    @Loek fixed to "weak". I was mistakenly referring to the comparison. – Mike Jun 05 '18 at 14:03