Summary

test egg 'approx-equal?' bug?

Metadata

Description

There may be a bug in 'approx-equal?' in the test egg.

I was puzzled as to why testing against an expected value of 0.0 always seems to fail, even with a large epsilon (1.0).

   (use test)
   (test 0.0 1e-18) => [[FAIL]|]

Here is the source code of 'approx-equal?' for reference:

   (define (approx-equal? a b epsilon)
     (cond
      ((> (abs a) (abs b))
       (approx-equal? b a epsilon))
      ((zero? b)
       (< (abs a) epsilon))
      (else
       (< (abs (/ (- a b) b)) epsilon))))

I note that the conditional branch (zero? b), which is apparently a special case for testing against 0, will never be reached unless both a and b are 0, because if b is 0 the absolute value of any other number is greater, so the previous condition will catch it and flip a and b.

Should that middle condition instead be the following?

      ((zero? a)
       (< (abs b) epsilon))

Changes and comments

[2014-09-19 03:53:25 UTC] mario set keywords to test

[2014-09-19 03:53:25 UTC] mario changed status from new to assigned

[2014-09-19 03:53:25 UTC] mario changed component from unknown to extensions

[2014-09-19 03:53:25 UTC] mario set owner to ashinn

[2014-09-19 04:03:43 UTC] ashinn changed status from assigned to closed

[2014-09-19 04:03:43 UTC] ashinn set resolution to fixed

[2014-09-19 04:03:43 UTC] ashinn wrote:

Thanks! This was a bug and was also present in chibi. Fixed.