1
/* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
3
Copyright 1996, 1999, 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
27
mpf_set_q (mpf_t r, mpq_srcptr q)
31
mp_size_t nsize, dsize;
32
mp_size_t qsize, rsize;
33
mp_size_t sign_quotient;
40
nsize = SIZ (&q->_mp_num);
41
dsize = SIZ (&q->_mp_den);
56
sign_quotient = nsize ^ dsize;
59
np = PTR (&q->_mp_num);
60
dp = PTR (&q->_mp_den);
75
rsize = MAX (nsize, dsize);
76
rp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);
78
/* Normalize the denominator, i.e. make its most significant bit set by
79
shifting it NORMALIZATION_STEPS bits to the left. Also shift the
80
numerator the same number of steps (to keep the quotient the same!). */
81
if (! (dp[dsize-1] & MP_LIMB_T_HIGHBIT))
85
unsigned normalization_steps;
87
count_leading_zeros (normalization_steps, dp[dsize - 1]);
89
/* Shift up the denominator setting the most significant bit of
90
the most significant limb. Use temporary storage not to clobber
91
the original contents of the denominator. */
92
tp = (mp_ptr) TMP_ALLOC (dsize * BYTES_PER_MP_LIMB);
93
mpn_lshift (tp, dp, dsize, normalization_steps);
98
MPN_ZERO (rp, rsize - nsize);
99
nlimb = mpn_lshift (rp + (rsize - nsize),
100
np, nsize, normalization_steps);
104
nlimb = mpn_lshift (rp, np, rsize, normalization_steps);
110
/* Don't just increase rsize, chop off rp at the low end instead. */
121
MPN_ZERO (rp, rsize - nsize);
122
MPN_COPY (rp + (rsize - nsize), np, nsize);
126
MPN_COPY (rp, np, rsize);
130
qlimb = mpn_divrem (qp, prec - 1 - (rsize - dsize), rp, rsize, dp, dsize);
140
SIZ (r) = sign_quotient >= 0 ? qsize : -qsize;