From: Kevin Ryde <user42@zip.com.au>
Subject: inexact->exact bignum rounding
Date: Thu, 02 Oct 2003 10:40:53 +1000 [thread overview]
Message-ID: <87brt04gkq.fsf@zip.com.au> (raw)
I'd like to propose a new style scm_i_big2dbl, below (not really
tested yet).
This is of course used by exact->inexact, and also a few other places
where it makes sense to do the same as exact->inexact (in particular
make-rectangular for instance).
/* scm_i_big2dbl() rounds to the closest representable double, in accordance
with R5RS exact->inexact.
In GMP prior to 4.2, mpz_get_d rounding was unspecified. It followed the
hardware rounding mode, but on the absolute value actually. We get a 4.2
style truncate to zero by applying mpz_get_d to just the high
DBL_MANT_DIG bits (no rounding). This is not fast, but it's only for old
GMP.
In GMP 4.2 and up, mpz_get_d rounds towards zero. It basically takes the
high DBL_MANT_DIG bits, and we adjust to our desired round-to-nearest by
looking at the next bit below those.
Note that bignums exactly half way between representable doubles are
rounded to the next higher absolute value. This seems like an adequate
interpretation of "numerically closest", and it's easier to do than a
full "nearest-even" style. */
double
scm_i_big2dbl (SCM b)
{
double result;
size_t bits;
#if __GNU_MP_VERSION < 4 \
|| (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR < 2)
/* GMP prior to 4.2 */
mpz_t tmp;
mpz_init (tmp);
mpz_tdiv_q_2exp (tmp, SCM_I_BIG_MPZ (b), bits-DBL_MANT_DIG);
result = ldexp (mpz_get_d (tmp), bits-DBL_MANT_DIG);
mpz_clear (tmp);
#else
/* GMP 4.2 and up */
result = mpz_get_d (SCM_I_BIG_MPZ (b));
#endif
bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2);
if (bits > DBL_MANT_DIG)
if (mpz_tstbit (SCM_I_BIG_MPZ (b), bits-DBL_MANT_DIG-1))
result += ldexp ((double) mpz_sgn (SCM_I_BIG_MPZ (b)),
bits-DBL_MANT_DIG-1);
scm_remember_upto_here_1 (b);
return result;
}
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
next reply other threads:[~2003-10-02 0:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-02 0:40 Kevin Ryde [this message]
2003-10-07 16:05 ` inexact->exact bignum rounding Marius Vollmer
2003-10-19 0:36 ` 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=87brt04gkq.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).