Bug #78492 [Com]: Comparing Value from equation to 100
| From: | phpbugreports at gmail dot com | Date: | Wed, 04 Sep 2019 20:10:14 +0000 |
| Subject: | Bug #78492 [Com]: Comparing Value from equation to 100 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222560@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78492&edit=1
ID: 78492
Comment by: phpbugreports at gmail dot com
Reported by: email at brunofaria dot pt
Summary: Comparing Value from equation to 100
Status: Not a bug
Type: Bug
Package: *Math Functions
Operating System: Windows 10
PHP Version: 7.3.9
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
> You should always compare floats with <, >, <= or >=
for sure not
> In your case, make it $value>99.999 and you'll be good
even not for that isolated case because it would be >= 100 instead == 100 but not
"99.9" with a random count of 9 added at the end
what you always should do is be as explicit as you can and > is the exact opposite, properly cast
your values before compare and use === and !== where ever you can
Previous Comments:
------------------------------------------------------------------------
[2019-09-04 20:04:30] sinus at sinpi dot net
You should always compare floats with <, >, <= or >=. In your case, make it
$value>99.999 and you'll be good.
------------------------------------------------------------------------
[2019-09-04 17:58:54] phpbugreports at gmail dot com
convert to strings is sonsense even if it works by luck, limited precision was already explained,
the value is something like "100.0000000000000142108547152020037174224853515625"
if(100 == (int)$value)
if(100 === (int)$value)
one of both would work
<?php
$value = 61.8 + 20.1 + 11.4 + 6.7;
ini_set('precision', 1000);
if(100 == (int)$value)
{
echo "true: $value\n";
}
else
{
echo "false: $value\n";
}
?>
------------------------------------------------------------------------
[2019-09-04 17:47:12] email at brunofaria dot pt
I think I'm going to convert both values to strings and them compare them to work around this
problem.
------------------------------------------------------------------------
[2019-09-04 17:35:31] email at brunofaria dot pt
Doesn't matter if its == or === always returns false.
Also, the result from the foreach loop is exactly '100' and I'm comparing to 100 ,
its the same string representation.
------------------------------------------------------------------------
[2019-09-04 16:17:05] sinus at sinpi dot net
Rule #1: never compare floats with ==.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=78492
--
Edit this bug report at https://bugs.php.net/bug.php?id=78492&edit=1