1 /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
3 Copyright 1996, 1999, 2001, 2002, 2004, 2005 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 3 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. If not, see http://www.gnu.org/licenses/. */
20 #include <stdio.h> /* for NULL */
26 /* As usual the aim is to produce PREC(r) limbs, with the high non-zero.
27 The basic mpn_tdiv_qr produces a quotient of nsize-dsize+1 limbs, with
28 either the high or second highest limb non-zero. We arrange for
29 nsize-dsize+1 to equal prec+1, hence giving either prec or prec+1 result
32 nsize-dsize+1 == prec+1 is achieved by adjusting num(q), either dropping
33 low limbs if it's too big, or padding with low zeros if it's too small.
34 The full given den(q) is always used.
36 We cannot truncate den(q), because even when it's much bigger than prec
37 the last limbs can still influence the final quotient. Often they don't,
38 but we leave optimization of that to a prospective quotient-only mpn
43 If den(q) is a power of 2 then we may end up with low zero limbs on the
44 result. But nothing is done about this, since it should be unlikely on
45 random data, and can be left to an application to call mpf_div_2exp if it
46 might occur with any frequency.
50 The high quotient limb is non-zero when high{np,dsize} >= {dp,dsize}. We
51 could make that comparison and use qsize==prec instead of qsize==prec+1,
52 to save one limb in the division.
56 If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of
57 padding n with zeros in temporary space.
59 If/when a quotient-only division exists it can be used here immediately.
60 remp is only to satisfy mpn_tdiv_qr, the remainder is not used. */
63 mpf_set_q (mpf_t r
, mpq_srcptr q
)
66 mp_size_t prec
, nsize
, dsize
, qsize
, prospective_qsize
, tsize
, zeros
;
67 mp_size_t sign_quotient
, high_zero
;
72 ASSERT (SIZ(&q
->_mp_den
) > 0); /* canonical q */
74 nsize
= SIZ (&q
->_mp_num
);
75 dsize
= SIZ (&q
->_mp_den
);
77 if (UNLIKELY (nsize
== 0))
89 sign_quotient
= nsize
;
91 np
= PTR (&q
->_mp_num
);
92 dp
= PTR (&q
->_mp_den
);
94 prospective_qsize
= nsize
- dsize
+ 1; /* q from using given n,d sizes */
95 exp
= prospective_qsize
; /* ie. number of integer limbs */
96 qsize
= prec
+ 1; /* desired q */
98 zeros
= qsize
- prospective_qsize
; /* n zeros to get desired qsize */
99 tsize
= nsize
+ zeros
; /* possible copy of n */
103 /* separate alloc blocks, for malloc debugging */
104 remp
= TMP_ALLOC_LIMBS (dsize
);
107 tp
= TMP_ALLOC_LIMBS (tsize
);
111 /* one alloc with a conditionalized size, for efficiency */
112 mp_size_t size
= dsize
+ (zeros
> 0 ? tsize
: 0);
113 remp
= TMP_ALLOC_LIMBS (size
);
119 /* pad n with zeros into temporary space */
120 MPN_ZERO (tp
, zeros
);
121 MPN_COPY (tp
+zeros
, np
, nsize
);
127 /* shorten n to get desired qsize */
132 ASSERT (nsize
-dsize
+1 == qsize
);
133 mpn_tdiv_qr (qp
, remp
, (mp_size_t
) 0, np
, nsize
, dp
, dsize
);
135 /* strip possible zero high limb */
136 high_zero
= (qp
[qsize
-1] == 0);
141 SIZ (r
) = sign_quotient
>= 0 ? qsize
: -qsize
;