Re: Math Weirdness
| From: | Robert Cummings | Date: | Tue, 15 Jul 2008 18:25:43 +0000 |
| Subject: | Re: Math Weirdness | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-276874@lists.php.net to get a copy of this message | ||
On Tue, 2008-07-15 at 20:17 +0200, Jochem Maas wrote:
> Robin Vickery schreef:
> > 2008/7/15 tedd <tedd.sperling@gmail.com>:
> >> 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