note 47911 modified in language.operators.comparison by danbrown

From: Date: Thu, 18 Jun 2009 14:14:53 +0000
Subject: note 47911 modified in language.operators.comparison by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157088@lists.php.net to get a copy of this message
Hi folks, to the float comparison problem... This worked for me: <?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 />"; ?> --was-- Hi folks, to the float comparison problem... This worked for me: //! 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 />"; http://php.net/manual/en/language.operators.comparison.php

« previous php.notes (#157088) next »