Ok, why is this happening...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux