note 107471 deleted from language.operators.comparison by tularis

From: Date: Sun, 12 Feb 2012 11:39:00 +0000
Subject: note 107471 deleted from language.operators.comparison by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186085@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- Regarding post [sven dot heyll at web dot de 05-Dec-2004 01:19]: the function isn't valid for all cases (e.g. adding six additional lines to the end results in "not equal" and "not equal" being output, the opposite of the truth) -- <?php //! compare two floating point values, return true if they are equal //! (enough) or false otherwise function float_equal($f1, $f2) { return ($f1 > $f2) ? (false) : (!($f1 < $f2)); } // compare floats $f1 = 0.037; $f2 = 1000387.978; echo "$f1 and $f2 are ".(float_equal($f1,$f2)?("equal"):("not equal"))."<br />"; $f1 = 0.3; $f2 = 0.3; echo "$f1 and $f2 are ".(float_equal($f1,$f2)?("equal"):("not equal"))."<br />"; $f1 = 0.3; $f2 = 0.1 * 3; echo "$f1 and $f2 are ".(float_equal($f1,$f2)?("equal"):("not equal"))."<br />"; $f1 = 0.1 * 3; $f2 = 0.3; echo "$f1 and $f2 are ".(float_equal($f1,$f2)?("equal"):("not equal"))."<br />"; ?>

« previous php.notes (#186085) next »