chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] bug and potential fix for numbers egg


From: Alex Shinn
Subject: [Chicken-users] bug and potential fix for numbers egg
Date: Fri, 28 Mar 2008 15:31:31 +0900
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (darwin)

$ csi -R numbers
...
#;1> (= 0.1 (/ 1 10))
#f
#;2> (= 0.1 (exact->inexact (/ 1 10)))
#f
#;3> (exact->inexact (/ 1 10))
0.1

Note that both equalities hold when not using the numbers
egg.  Exact = comparisons on floating point numbers are
rare, but this is just a symptom of a more general issue
which was triggering a 1 in 10,000 bug in the new set of fmt
formatting tests.

The problem is that numbers uses the function mpq_get_d for
converting from a rational to a double, but the description
of that function is:

   Function: double mpq_get_d (mpq_t op)

   Convert op to a double, truncating if necessary
       (ie. rounding towards zero).

So it deliberately loses precision by truncating instead of
rounding.  There is no function to do the same with rounding
in GMP.

As a potential workaround, I've attached a patch which first
checks if both the numerator and denominator fit within a
long (there's no direct way to check if they fit in a
double), and if so just uses the straight C operations

    (double) numerator / (double) denominator

which is more accurate than what GMP does!

If they don't fit we fall back on mpq_get_d, but in that
case we're going to lose precision whether we want to or not
so it's not a big deal.

The attached patch implements this, and passes the above
test, the numbers egg tests, and the fmt egg tests, but I'd
prefer someone more familiar with GMP look at it before
checking it in.

-- 
Alex

Attachment: numbers-c.c.diff
Description: numbers patch


reply via email to

[Prev in Thread] Current Thread [Next in Thread]