Re: Float comparison
| From: | Richard Quadling | Date: | Fri, 02 May 2008 23:59:34 +0000 |
| Subject: | Re: Float comparison | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-37415@lists.php.net to get a copy of this message | ||
2008/5/3 Alain Williams <addw@phcomp.co.uk>:
> On Fri, May 02, 2008 at 01:03:45PM -0700, Todd Ruth wrote:
> > On Fri, 2008-05-02 at 11:41 -0700, Rasmus Lerdorf wrote:
> > > Stefan Walk wrote:
> > > > And you'll quickly see that the "cast to string before
> > > > comparision" is a
> > > > bad idea, because:
> > > > $ php -dprecision=1 -r 'var_dump((string)1.4 == (string)1.1);'
> > > > bool(true)
> > > >
> > > > Having display settings affect comparisions seems like a really bad idea
> > > > to me ;)
> > >
> > > Yup, it would be a fatal flaw in the language if that ever came to be.
> > >
> > > -Rasmus
> >
> > Most people don't care about floating numbers beyond a certain
> > number of digits. If precision were the only ini setting that
> > gave us an idea of how much the person cared, I think it would
> > be fair to use the setting or introduce a new setting. As it
> > happens, there is another setting. How would you feel about
> > this returning true:
> >
> > php -dserialize_precision=1 -r 'var_dump(1.4 == 1.1);'
> >
> > Setting serialize_precision to 1 is a very strong statement of
> > not caring about those digits. Personally, I don't care about
> > more than about 6 digits and I don't do much math, so I could
> > set it to 8 digits and be happy. Not everyone has the luxury
> > of controlling ini settings, but for those that do, this
> > would be much nicer than having
>
> Hmmm, what you are talking about is implying:
>
> $a == $b
>
> be implemented as:
>
> abs($a - $b) < (max(abs($a), abs($b)) / EPSILON)
>
> Where EPSILON is some sort of accuracy factor. It might be
> possible to implement this easier/faster at a machine code level.
>
> The trouble is that doing something like this would be global and
> might mess up some code written elsewhere. Mind you: any programmer
> going '$a == $b' on true floating point has got to expect rubbish.
>
> This is making things easier for the naive/novice programmer. In this case
> I don't think that it realy helps since the naive programmer isn't
> going to have a clue about choosing a good value for EPSILON.
>
> Best leave it the way that it is.
What about introducing a "money" or "currency" numeric type which
guarantees say accuracy to 8DP?
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"