Bug #78492 [Com]: Comparing Value from equation to 100

From: Date: Wed, 04 Sep 2019 16:17:05 +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-222555@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: sinus at sinpi dot net 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: Rule #1: never compare floats with ==. Previous Comments: ------------------------------------------------------------------------ [2019-09-04 14:34:26] cmb@php.net Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is, read this: http://www.floating-point-gui.de/ Thank you for your interest in PHP. ------------------------------------------------------------------------ [2019-09-04 14:30:55] email at brunofaria dot pt Description: ------------ I'm adding this numbers from an array with key[0]: array(4) { [0]=> array(2) { [0]=> string(4) "61.8" [1]=> string(11) " Poliéster" } [1]=> array(2) { [0]=> string(4) "20.1" [1]=> string(6) " Nylon" } [2]=> array(2) { [0]=> string(4) "11.4" [1]=> string(9) " Algodão" } [3]=> array(2) { [0]=> string(3) "6.7" [1]=> string(6) " Rayon" } } 61.8 + 20.1 + 11.4 + 6.7 = 100 Then I'm comparing to 100, Returns False. Test script: --------------- $value = 0; foreach ($array as $key) { $value += floatval ( preg_replace ('/\s+/', '', $key[0]) ) ; } if ($value == 100 ) { return true; } else { return false; } Expected result: ---------------- true Actual result: -------------- false ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78492&edit=1

« previous php.bugs (#222555) next »