0

Given the variables below, why does the elseif conditional get hit?

$i          = 0;
$rating     = 11.11111111111;
$scale_step = 11.11111111111;

if( $rating == $i * $scale_step )
{
    return 'checked';
}
elseif( $rating < (($i + 1) * $scale_step) 
     && $rating > $i * $scale_step )
{
   return 'checked';
}
else
{
    return '';
}

In the elseif above, $rating is equal to ($i + 1) * $scale_step - it is NOT less than. So why does it evaluate to true?

rwkiii
  • 5,716
  • 18
  • 65
  • 114
  • 7
    Welcome to the world of floating point math – John Conde Sep 12 '16 at 23:41
  • Please read the manual on floating points...... http://php.net/manual/en/language.types.float.php – Kevin Kopf Sep 12 '16 at 23:42
  • 2
    See http://0.30000000000000004.com/ – ChrisGPT was on strike Sep 12 '16 at 23:43
  • You're all pointing me in the right direction - I didn't know this about floating point math. I'm reading the answer I've apparently duplicated as well as the links you have provided here, but I am still not understanding how there are differences in the values I show in my question. I don't need as much precision as I posted, I could round them all if that would help. Is this just a matter of rounding all of the values to the same precision? Say, to 2? – rwkiii Sep 13 '16 at 00:06

0 Answers0