Ok, why is this happening...

From: Date: Tue, 19 Jul 2005 16:01:56 +0000
Subject: Ok, why is this happening...
Groups: php.general 
Request: Send a blank email to php-general+get-218965@lists.php.net to get a copy of this message
Consider the following test script: <script language="php"> set_time_limit( 0 ); echo '<html>'; echo '<head><title>Test Rounding Net Premium</title></head>'; echo '<body>'; echo 'Running test...<br>'; flush(); echo '<table>'; echo '<tr>'; echo '<td>Original Net</td>'; echo '<td>Commission %</td>'; echo '<td>Calculated Gross</td>'; echo '<td>Calculated Net</td>'; echo '</tr>'; $numberOfFailures = 0; for( $originalNet = 10000; $originalNet <= 10005; $originalNet++ ) { for( $commissionPct = 1; $commissionPct <= 20; ( $commissionPct += .1 )) { $calculatedGross = $originalNet + ( $originalNet * $commissionPct * .01 ); $calculatedNet = $calculatedGross / ( 1 + ( $commissionPct * .01 )); echo "if( $originalNet != $calculatedNet ) = " . ( (int)$originalNet !== (int)$calculatedNet ) . "<br>\n"; if( (int)$originalNet !== (int)$calculatedNet ) { $numberOfFailures++; echo '<tr>'; echo '<td>' . $originalNet . '</td>'; echo '<td>' . $commissionPct . '%</td>'; echo '<td>' . $calculatedGross . '</td>'; echo '<td>' . $calculatedNet . '</td>'; echo '</tr>'; flush(); } } } echo '</table>'; if( 0 < $numberOfFailures ) { echo number_format( $numberOfFailures ) . ' calculations failed to match the original net premium.<br>'; } echo '</body>'; echo '</html>'; </script> Why is the if( (int)$originalNet !== (int)$calculatedNet ) failing some times even though they are the exact same value? I've tried using just the != comparison operator, I've tried casting the values (as seen above) I've tried testing to see if the values are either less than or greater than one another (as a test for inequality). I just can't figure out why in the world the IF is showing the values are inequal when you can view the output and see they are exactly equal. What's going on? thnx, Chris

« previous php.general (#218965) next »