[Top][All Lists]

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

inum/double = on 64-bits

From: Kevin Ryde
Subject: inum/double = on 64-bits
Date: Thu, 06 Jan 2005 11:29:14 +1100
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux)

On a debian alpha, a comparison like

        (= #x111111111111111 (exact->inexact #x111111111111110))

gives #t, where on a 32-bit system it's #f.  The problem is rounding
in the scm_num_eq_p, an inum doesn't fit the mantissa of a double.

You can see exact->inexact isn't corrupting the value with say
(number->string (inexact->exact (exact->inexact #x111111111111110)) 16)

I'm looking at a fix like the following, which will add xx==(long)yy
on a system with a big inum.

      /* On a 32-bit system an inum fits a double, we can cast the inum to a
         double and compare.

         But on a 64-bit system an inum is bigger than a double and casting
         it to a double (call it dxx) will round.  dxx is at worst 1 bigger
         or smaller than xx, so if dxx==yy we know yy is an integer and fits
         a long.  So we cast yy to a long and compare with plain xx.

         An alternative (for any size system actually) would be to check yy
         is an integer (with floor) and is in range of an inum (compare
         against appropriate powers of 2) then test xx==(long)yy.  It's just
         a matter of which casts/comparisons might be fastest or easiest for
         the cpu.  */

      double yy = SCM_REAL_VALUE (y);
      return SCM_BOOL ((double) xx == yy
                       && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
                           || xx == (long) yy));

reply via email to

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