|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-06-26 01:23 UTC] derick@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 07:00:01 2025 UTC |
Comparison of float values does not always work. ./configure --with-mysql=/usr/local/mysql --with-xml --with-apache=../../apache/apache_1.3.26/ Test code follows: <?php $a = 332.06; $b = 45.11 + 134.38 + 85.35 + 67.22; echo "output\n"; var_dump($a); var_dump($b); print("Total = $a\n"); print("Production = $b\n"); if($a != $b) { print("Totals do not Match!\n"); } else { print("Totals are in Balance\n"); } if($a !== $b) { print("Totals do not Match!\n"); } else { print("Totals are in Balance\n"); } if($a <> $b) { print("Totals do not Match!\n"); } else { print("Totals are in Balance\n"); } if($a > $b) { print("a is greater!\n"); } else { print("b is greater!\n"); } settype($a,"string"); settype($b,"string"); if ($a != $b) echo "String compare failed!\n\n"; settype($a,"float"); settype($b,"float"); if ($a != $b) echo "recast to float compare failed!\n\n"; echo "end output\n"; ?> ----------------------------- output float(332.06) float(332.06) Total = 332.06 Production = 332.06 Totals do not Match! Totals do not Match! Totals do not Match! b is greater! end output ------------------------------ Summary: float compare fails (!=) float compare fails (<>) float compare fails (!==) "b" is somehow greater than "a" cast to string, compare okay. recast to float, compare okay. -Mike