unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* inum/double = on 64-bits
@ 2005-01-06  0:29 Kevin Ryde
  2005-01-06 15:06 ` Greg Troxel
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Ryde @ 2005-01-06  0:29 UTC (permalink / raw)


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));


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-06 23:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-06  0:29 inum/double = on 64-bits Kevin Ryde
2005-01-06 15:06 ` Greg Troxel
2005-01-06 23:15   ` Kevin Ryde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).