unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: inum/double = on 64-bits
Date: Thu, 06 Jan 2005 11:29:14 +1100	[thread overview]
Message-ID: <87brc3mmcl.fsf@zip.com.au> (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


             reply	other threads:[~2005-01-06  0:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-06  0:29 Kevin Ryde [this message]
2005-01-06 15:06 ` inum/double = on 64-bits Greg Troxel
2005-01-06 23:15   ` Kevin Ryde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87brc3mmcl.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).