You are here: Ok, why is this happening... « PHP « IT news, forums, messages
Ok, why is this happening...

Posted by "Chris Boget" on 07/19/05 19:01

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация