unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Marius Vollmer <marius.vollmer@uni-dortmund.de>
Cc: Bill Schottstaedt <bil@ccrma.stanford.edu>
Subject: Re: ratio implementation
Date: Fri, 17 Oct 2003 12:09:02 +0200	[thread overview]
Message-ID: <ljllrkp45d.fsf@troy.dt.e-technik.uni-dortmund.de> (raw)
In-Reply-To: <87oewjcrwq.fsf@zip.com.au> (Kevin Ryde's message of "Wed, 15 Oct 2003 09:37:25 +1000")

Kevin Ryde <user42@zip.com.au> writes:

> Marius Vollmer <marius.vollmer@uni-dortmund.de> writes:
>>
>> I don't have the details ready yet, but I think I'll try to come up
>> with code for this... (when it isn't already in GMP).
>
> gmp has an mpq_set_d, which does what you imagine, extract the bits to
> make a binary fraction.  The actual implementation is uglified by the
> details of gmp internals though.

Yes, that's what I was looking for.  I now have in scm_inexact_to_exact

  else if (SCM_REALP (z))
    {
      mpq_t frac;
      SCM q;
      mpq_init (frac);
      mpq_set_d (frac, SCM_REAL_VALUE (z));
      q = scm_make_ratio (scm_i_mpz2num (mpq_numref (frac)),
			  scm_i_mpz2num (mpq_denref (frac)));
      mpq_clear (frac);
      return q;
    }

using the helper

    static SCM_C_INLINE_KEYWORD SCM
    scm_i_mpz2num (mpz_t b)
    {
      /* convert a mpz number to a SCM number. */
      if (mpz_fits_slong_p (b))
        {
          long val = mpz_get_si (b);
          if (SCM_FIXABLE (val))
            return SCM_MAKINUM (val);
        }

      {
        SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
        mpz_init_set (SCM_I_BIG_MPZ (z), b);
        return z;
      }
    }

Kevin, does this look OK from a GMP point of view?  I.e., no memory
leaks, etc?


That code gives perfect accuracy but that can be strange as well:

    guile> (exact->inexact 1/1000)
    0.001
    guile> (inexact->exact (exact->inexact 1/1000))
    1152921504606847/1152921504606846976
    guile> (rationalize (exact->inexact 1/1000) 1e-16)
    1/1000

The above behavior of inexact->exact better fits R5RS, I'd say, since
R5RS calls for the _closest_ representable exact number, not for the
simplest, as with rationalize.  So I'm inclined to use it intead of
rationalize.  Ok?


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


  parent reply	other threads:[~2003-10-17 10:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-28 11:24 ratio implementation Bill Schottstaedt
2003-07-28 12:08 ` Han-Wen Nienhuys
2003-07-29  0:41 ` Kevin Ryde
2003-07-29 11:57   ` Bill Schottstaedt
2003-07-30 22:42     ` Kevin Ryde
2003-09-15 10:35 ` Marius Vollmer
2003-09-15 16:19   ` Rob Browning
2003-09-15 22:06   ` Dirk Herrmann
2003-09-15 22:59     ` Kevin Ryde
2003-09-16 11:39     ` Bill Schottstaedt
2003-09-16 21:36       ` Rob Browning
2003-09-18 21:09       ` Dirk Herrmann
2003-10-07 15:26         ` Marius Vollmer
2003-10-13 10:58           ` Bill Schottstaedt
2003-10-14  8:57             ` Marius Vollmer
2004-02-18 14:25               ` fractions.test Bill Schottstaedt
2003-10-14 12:39             ` ratio implementation Marius Vollmer
2003-10-14 22:56               ` Kevin Ryde
2003-10-14 13:03             ` Marius Vollmer
2003-10-14 23:37               ` Kevin Ryde
2003-10-16 11:49                 ` Bill Schottstaedt
2003-10-17 10:09                 ` Marius Vollmer [this message]
2003-10-17 11:47                   ` Bill Schottstaedt
2003-10-17 15:04                   ` Rob Browning
2003-10-18  0:45                   ` Kevin Ryde
2003-10-15 12:57               ` Bill Schottstaedt
2003-10-17 10:20                 ` Marius Vollmer
2003-10-17 15:14                   ` Rob Browning
2003-10-17 15:42                     ` Marius Vollmer
2003-10-14 23:01             ` Kevin Ryde
2003-10-18  0:55               ` ash using shifts (was: ratio implementation) Kevin Ryde
2003-10-07 15:24       ` ratio implementation Marius Vollmer

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=ljllrkp45d.fsf@troy.dt.e-technik.uni-dortmund.de \
    --to=marius.vollmer@uni-dortmund.de \
    --cc=bil@ccrma.stanford.edu \
    /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).