Re: Math Weirdness

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

 



On Tue, 2008-07-15 at 20:17 +0200, Jochem Maas wrote:
> Robin Vickery schreef:
> > 2008/7/15 tedd <tedd.sperling@xxxxxxxxx>:
> >> I said:
> >>
> >> "Round-off errors normally don't enter into things unless your doing
> >> multiplication and division operations."
> >>
> >> And that is not "Bull" -- it's true. You can add and subtract all the
> >> floating point numbers (the one's we are talking about here) you want
> >> without any rounding errors whatsoever.
> > 
> > $ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
> > false
> > 
> > The operation you do isn't important. You just can't represent certain
> > numbers exactly in binary, just as you can't represent 1/3 exactly in
> > decimal. They have to be rounded internally.
> 
> which makes php casting so much fun:
> 
> $ php -r 'echo (string)(0.7 - 0.2) == 0.5 ? "true\n" : "false\n";'
> true

Just make a simple function...

<?php

function close_enough( $float1, $float2, $delta=0.000001 )
{
    return (abs( $float1 - $float2 ) < $delta);
}

?>

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
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