On Thu, Sep 02, 2010 at 01:11:07AM +0200, Dr.Ruud wrote:
> On 2010-09-02 00:49, Paul Johnson wrote:
>
>> When you want to compare floating point numbers, check that their
>> difference is less than some appropriately small delta:
>>
>> $delta = 1e-8;
>>
>> if (abs($a - $b)<  $delta)  # numbers are "equal"
>
> Why call it delta when you can call it epsilon?
> ;)

You're absolutely right.  And see Test::Number::Delta on CPAN for an
easier way of doing this in tests.

> It also has no 'my', why is that?

Because it is a short snippet of code designed to show one principle.
Adding "my" would not have contributed to that goal.  I was also
expecting someone to pick me up on using $a and $b.  Perhaps they still
will.  The answer would be very similar.

> And OP should read at least one of these:
> http://docs.sun.com/source/806-3568/ncg_goldberg.html
> http://en.wikipedia.org/wiki/Floating_point
> http://hal.archives-ouvertes.fr/docs/00/28/14/29/PDF/floating-point-article.pdf

Yes, this is a well understood problem that nevertheless manages to
perplex many beginning programmers.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to