note 89842 added to language.types.float
| From: | Boylett at osu1 dot php dot net | Date: | Wed, 25 Mar 2009 18:06:47 +0000 |
| Subject: | note 89842 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152436@lists.php.net to get a copy of this message | ||
I had trouble with comparing floats. Basicly, I was reading floats from a database, and comparing
with other floats from a database, and ended up with (5.0 != 5.0) being true.
Anyway, i made some simple functions which fix the problem:
<?php
function floatcmp($f1,$f2,$precision = 10) // are 2 floats equal
{
$e = pow(10,$precision);
$i1 = intval($f1 * $e);
$i2 = intval($f2 * $e);
return ($i1 == $i2);
}
function floatgtr($big,$small,$precision = 10) // is one float bigger than another
{
$e = pow(10,$precision);
$ibig = intval($big * $e);
$ismall = intval($small * $e);
return ($ibig > $ismall);
}
function floatgtre($big,$small,$precision = 10) // is on float bigger or equal to another
{
$e = pow(10,$precision);
$ibig = intval($big * $e);
$ismall = intval($small * $e);
return ($ibig >= $ismall);
}
?>
----
Server IP: 87.124.35.190
Probable Submitter: 86.30.9.29
----
Manual Page -- http://www.php.net/manual/en/language.types.float.php
Edit -- https://master.php.net/note/edit/89842
Del: integrated -- https://master.php.net/note/delete/89842/integrated
Del: useless -- https://master.php.net/note/delete/89842/useless
Del: bad code -- https://master.php.net/note/delete/89842/bad+code
Del: spam -- https://master.php.net/note/delete/89842/spam
Del: non-english -- https://master.php.net/note/delete/89842/non-english
Del: in docs -- https://master.php.net/note/delete/89842/in+docs
Del: other reasons-- https://master.php.net/note/delete/89842
Reject -- https://master.php.net/note/reject/89842
Search -- https://master.php.net/manage/user-notes.php