1 /* mpq_set_d(mpq_t q, double d) -- Set q to d without rounding.
3 Copyright 2000, 2002, 2003 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/. */
23 #include <float.h> /* for DBL_MAX */
30 #if LIMBS_PER_DOUBLE > 4
35 mpq_set_d (mpq_ptr dest, double d)
39 mp_limb_t tp[LIMBS_PER_DOUBLE];
44 DOUBLE_NAN_INF_ACTION (d,
45 __gmp_invalid_operation (),
46 __gmp_invalid_operation ());
51 exp = __gmp_extract_double (tp, d);
53 /* There are two main version of the conversion. The `then' arm handles
54 numbers with a fractional part, while the `else' arm handles integers. */
55 #if LIMBS_PER_DOUBLE == 4
56 if (exp <= 1 || (exp == 2 && (tp[0] | tp[1]) != 0))
58 #if LIMBS_PER_DOUBLE == 3
59 if (exp <= 1 || (exp == 2 && tp[0] != 0))
61 #if LIMBS_PER_DOUBLE == 2
67 SIZ(&(dest->_mp_num)) = 0;
68 SIZ(&(dest->_mp_den)) = 1;
69 PTR(&(dest->_mp_den))[0] = 1;
74 MPZ_REALLOC (&(dest->_mp_num), 3);
75 np = PTR(&(dest->_mp_num));
76 #if LIMBS_PER_DOUBLE == 4
77 if ((tp[0] | tp[1] | tp[2]) == 0)
78 np[0] = tp[3], nn = 1;
79 else if ((tp[0] | tp[1]) == 0)
80 np[1] = tp[3], np[0] = tp[2], nn = 2;
82 np[2] = tp[3], np[1] = tp[2], np[0] = tp[1], nn = 3;
84 np[3] = tp[3], np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 4;
86 #if LIMBS_PER_DOUBLE == 3
87 if ((tp[0] | tp[1]) == 0)
88 np[0] = tp[2], nn = 1;
90 np[1] = tp[2], np[0] = tp[1], nn = 2;
92 np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 3;
94 #if LIMBS_PER_DOUBLE == 2
96 np[0] = tp[1], nn = 1;
98 np[1] = tp[1], np[0] = tp[0], nn = 2;
101 ASSERT_ALWAYS (dn > 0);
102 MPZ_REALLOC (&(dest->_mp_den), dn);
103 dp = PTR(&(dest->_mp_den));
104 MPN_ZERO (dp, dn - 1);
106 count_trailing_zeros (c, np[0] | dp[0]);
109 mpn_rshift (np, np, nn, c);
110 nn -= np[nn - 1] == 0;
111 mpn_rshift (dp, dp, dn, c);
112 dn -= dp[dn - 1] == 0;
114 SIZ(&(dest->_mp_den)) = dn;
115 SIZ(&(dest->_mp_num)) = negative ? -nn : nn;
120 MPZ_REALLOC (&(dest->_mp_num), nn);
121 np = PTR(&(dest->_mp_num));
125 MPN_ZERO (np, nn - LIMBS_PER_DOUBLE);
126 np += nn - LIMBS_PER_DOUBLE;
128 #if LIMBS_PER_DOUBLE == 2
130 np[1] = tp[1], np[0] = tp[0];
133 #if LIMBS_PER_DOUBLE == 3
135 np[2] = tp[2], np[1] = tp[1], np[0] = tp[0];
138 np[1] = tp[2], np[0] = tp[1];
141 #if LIMBS_PER_DOUBLE == 4
143 np[3] = tp[3], np[2] = tp[2], np[1] = tp[1], np[0] = tp[0];
146 np[2] = tp[3], np[1] = tp[2], np[0] = tp[1];
149 np[1] = tp[3], np[0] = tp[2];
153 dp = PTR(&(dest->_mp_den));
155 SIZ(&(dest->_mp_den)) = 1;
156 SIZ(&(dest->_mp_num)) = negative ? -nn : nn;