PIPS
arith_mulprec.h
Go to the documentation of this file.
1 /*
2 
3  $Id: arith_mulprec.h 1641 2016-03-02 08:20:19Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is clear software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the clear Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 /**
26  * @file
27  * This header provides functions for performing multiple-precision arithmetic on
28  * integer or rational numbers, using GMP.
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #ifndef HAVE_GMP_H
36 #error "arith_mulprec.h requires GMP"
37 #endif
38 
39 #ifdef LINEAR_ARITH_FIXPREC_H
40 #error "arith_fixprec.h and arith_mulprec.h are both loaded"
41 #endif
42 
43 #ifndef LINEAR_ARITH_MULPREC_H
44 #define LINEAR_ARITH_MULPREC_H
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <gmp.h>
50 
51 #include "assert.h"
52 #include "arithmetique.h"
53 
54 #define NOWUNUSED __attribute__((unused))
55 
56 /**
57  * @name Integers Functions
58  * This section describes the functions for performing integer arithmetic.
59  * These functions start with the prefix @c zval_.
60  * Integers are stored in objects of type @c zval_t.
61  */
62 /**@{*/
63 
64 /**
65  * Type of integer numbers.
66  */
67 typedef mpz_t zval_t;
68 
69 /**
70  * Initialize @a z and set its value to 0.
71  */
72 #define zval_init(z) (mpz_init(z))
73 
74 /**
75  * Free the space occupied by @a z.
76  */
77 #define zval_clear(z) (mpz_clear(z))
78 
79 /**
80  * Set the value of @a z1 from @a z2.
81  */
82 #define zval_set(z1, z2) (mpz_set(z1, z2))
83 
84 /**
85  * Set the value of @a z from the <tt>signed long</tt> @a n.
86  */
87 #define zval_set_i(z, n) (mpz_set_si(z, n))
88 
89 /**
90  * Initialize @a z1 and set its value from @a z2.
91  */
92 #define zval_init_set(z1, z2) (mpz_init_set(z1, z2))
93 
94 /**
95  * Initialize @a z and set its value from the <tt>signed long</tt> @a n.
96  */
97 #define zval_init_set_i(z, n) (mpz_init_set_si(z, n))
98 
99 /**
100  * Return the value of @a z as a <tt>signed long</tt>.
101  */
102 #define zval_get_i(z) (mpz_get_si(z))
103 
104 /**
105  * Set @a z1 to @a z2 + @a z3.
106  */
107 #define zval_add(z1, z2, z3) (mpz_add(z1, z2, z3))
108 
109 /**
110  * Set @a z1 to @a z2 - @a z3.
111  */
112 #define zval_sub(z1, z2, z3) (mpz_sub(z1, z2, z3))
113 
114 /**
115  * Set @a z1 to @a z2 times @a z3.
116  */
117 #define zval_mul(z1, z2, z3) (mpz_mul(z1, z2, z3))
118 
119 /**
120  * Set @a z1 to @a z2/@a z3.
121  */
122 #define zval_div(z1, z2, z3) (mpz_fdiv_q(z1, z2, z3))
123 
124 /**
125  * Set @a z1 to @a z1 + @a z2 times @a z3.
126  */
127 #define zval_addmul(z1, z2, z3) (mpz_addmul(z1, z2, z3))
128 
129 /**
130  * Set @a z1 to @a z1 - @a z2 times @a z3.
131  */
132 #define zval_submul(z1, z2, z3) (mpz_submul(z1, z2, z3))
133 
134 /**
135  * Set @a z1 to @a -@a z2.
136  */
137 #define zval_neg(z1, z2) (mpz_neg(z1, z2))
138 
139 /**
140  * Set @a z1 to the absolute value of @a z2.
141  */
142 #define zval_abs(z1, z2) (mpz_abs(z1, z2))
143 
144 /**
145  * Set @a z1 to @a z2 @c mod @a z3.
146  */
147 #define zval_mod(z1, z2, z3) (mpz_mod(z1, z2, z3))
148 
149 /**
150  * Set @a z1 to the greatest common divisor of @a z2 and @a z3.
151  * The result is always positive, irrespective of the signs of @a z2 and @a z3.
152  * Except if both inputs are zero; then it is undefined.
153  */
154 #define zval_gcd(z1, z2, z3) (mpz_gcd(z1, z2, z3))
155 
156 /**
157  * Set @a z1 to the least common multiple of @a z2 and @a z3.
158  * The result is always positive, irrespective of the signs of @a z2 and @a z3.
159  * @a z1 will be zero if either @a z2 or @a z3 is zero.
160  */
161 #define zval_lcm(z1, z2, z3) (mpz_lcm(z1, z2, z3))
162 
163 /**
164  * Compare @a z1 and @a z2.
165  * Return a positive value if @a z1 > @a z2, zero if @a z1 = @a z2, or a
166  * negative value if @a z1 < @a z2.
167  */
168 #define zval_cmp(z1, z2) (mpz_cmp(z1, z2))
169 
170 /**
171  * Compare @a z with a <tt>signed long</tt> @a n.
172  * Return a positive value if @a z > @a n, zero if @a z = @a n, or a
173  * negative value if @a z < @a n.
174  */
175 #define zval_cmp_i(z, n) (mpz_cmp_si(z, n))
176 
177 /**
178  * Return +1 if @a z > 0, 0 if @a z = 0, and -1 if @a z < 0.
179  */
180 #define zval_sgn(z) (mpz_sgn(z))
181 
182 /**
183  * Return non-zero if @a z1 and @a z2 are equal, zero if they are non-equal.
184  */
185 #define zval_equal(z1, z2) (zval_cmp(z1, z2) == 0)
186 
187 /**
188  * Return non-zero if @a z and the <tt>unsigned long</tt> @a n are equal,
189  * zero if they are non-equal.
190  */
191 #define zval_equal_i(z, n) (zval_cmp_i(z, n) == 0)
192 
193 /**
194  * Output @a z on stdio stream @a stream.
195  */
196 #define zval_fprint(stream, z) (mpz_out_str(stream, 10, z))
197 
198 /**
199  * Output @a z on @c stdout.
200  */
201 #define zval_print(z) (mpz_out_str(stdout, 10, z))
202 
203 /**@}*/
204 
205 /**
206  * @name Rational Number Functions
207  * This section describes the functions for performing arithmetic on rational
208  * numbers.
209  * These functions start with the prefix @c qval_.
210  * Rational numbers are stored in objects of type @c qval_t.
211  */
212 /**@{*/
213 
214 typedef __mpq_struct qval_s;
215 
216 typedef mpq_ptr qval_p;
217 
218 /**
219  * Type of rational numbers.
220  */
221 typedef qval_s qval_t[1];
222 
223 /**
224  * Remove any factors that are common to the numerator and denominator of @a q,
225  * and make the denominator positive.
226  */
227 #define qval_canonicalize(q) (mpq_canonicalize(q))
228 
229 /**
230  * Initialize @a q and set its value to 0/1.
231  */
232 #define qval_init(q) (mpq_init(q))
233 
234 /**
235  * Free the space occupied by @a q.
236  */
237 #define qval_clear(q) (mpq_clear(q))
238 
239 /**
240  * Set the value of @a q1 from @a q2.
241  */
242 #define qval_set(q1, q2) (mpq_set(q1, q2))
243 
244 /**
245  * Set the value of @a q to @a q2num/@a q2den.
246  */
247 #define qval_set_i(q1, q2num, q2den) (mpq_set_si(q1, q2num, q2den))
248 
249 /**
250  * Set @a q1 to @a q2 + @a q3.
251  */
252 #define qval_add(q1, q2, q3) (mpq_add(q1, q2, q3))
253 
254 /**
255  * Set @a q1 to @a q2 - @a q3.
256  */
257 #define qval_sub(q1, q2, q3) (mpq_sub(q1, q2, q3)(
258 
259 /**
260  * Set @a q1 to @a q2 times @a q3.
261  */
262 #define qval_mul(q1, q2, q3) (mpq_mul(q1, q2, q3))
263 
264 /**
265  * Set @a q1 to @a q2/@a q3.
266  */
267 #define qval_div(q1, q2, q3) (mpq_div(q1, q2, q3))
268 
269 /**
270  * Set @a q1 to @a -@a q2.
271  */
272 #define qval_neg(q1, q2) (mpq_neg(q1, q2))
273 
274 /**
275  * Set @a q1 to the absolute value of @a q2.
276  */
277 #define qval_abs(q1, q2) (mpq_abs(q1, q2))
278 
279 /**
280  * Set @a q1 to 1/@a q2.
281  */
282 #define qval_inv(q1, q2) (mpq_inv(q1, q2))
283 
284 /**
285  * Compare @a q1 and @a q2.
286  * Return a positive value if @a q1 > @a q2, qero if @a q1 = @a q2, or a
287  * negative value if @a q1 < @a q2.
288  * To determine if two rationals are equal, @c qval_equal is faster than
289  * @c qval_cmp.
290  */
291 #define qval_cmp(q1, q2) (mpq_cmp(q1, q2))
292 
293 /**
294  * Compare @a q1 and @a q2num/@a q2den.
295  * Return a positive value if @a q1 > @a q2num/@a q2den,
296  * zero if @a q1 = @a q2num/@a q2den,
297  * or a negative value if @a q1 < @a q2num/@a q2den.
298  */
299 #define qval_cmp_i(q1, q2num, q2den) (mpq_cmp_si(q1, q2num, q2den))
300 
301 /**
302  * Return +1 if @a q > 0, 0 if @a q = 0, and -1 if @a q < 0.
303  */
304 #define qval_sgn(q) (mpq_sgn(q))
305 
306 /**
307  * Return non-zero if @a q1 and @a q2 are equal, zero if they are non-equal.
308  * Although @c qval_cmp can be used for the same purpose, this function is
309  * faster.
310  */
311 #define qval_equal(q1, q2) (mpq_equal(q1, q2))
312 
313 /**
314  * Return non-zero if @a q and @a q2num/@a q2den are equal,
315  * zero if they are non-equal.
316  */
317 #define qval_equal_i(q1, q2num, q2den) (qval_cmp_i(q1, q2num, q2den) == 0)
318 
319 /**
320  * Output @a q on stdio stream @a stream.
321  */
322 #define qval_fprint(stream, q) (mpq_out_str(stream, 10, q))
323 
324 /**
325  * Output @a q on @c stdout.
326  */
327 #define qval_print(q) (mpq_out_str(stdout, 10, q))
328 
329 /**@}*/
330 
331 #endif
332 
qval_s qval_t[1]
Type of rational numbers.
mpq_ptr qval_p
__mpq_struct qval_s
mpz_t zval_t
Type of integer numbers.
Definition: arith_mulprec.h:67