1 /*
2 Copyright (C) 2010 Sebastian Pancratz
3
4 This file is part of FLINT.
5
6 FLINT is free software: you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version. See <http://www.gnu.org/licenses/>.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <gmp.h>
15 #include "flint.h"
16 #include "fmpz.h"
17
18 void
fmpz_tdiv_q(fmpz_t f,const fmpz_t g,const fmpz_t h)19 fmpz_tdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
20 {
21 fmpz c1 = *g;
22 fmpz c2 = *h;
23
24 if (fmpz_is_zero(h))
25 {
26 flint_printf("Exception (fmpz_tdiv_q). Division by zero.\n");
27 flint_abort();
28 }
29
30 if (!COEFF_IS_MPZ(c1)) /* g is small */
31 {
32 if (!COEFF_IS_MPZ(c2)) /* h is also small */
33 fmpz_set_si(f, c1 / c2);
34 else /* h is large */
35 fmpz_zero(f);
36 }
37 else /* g is large */
38 {
39 __mpz_struct *mpz_ptr = _fmpz_promote(f);
40
41 if (!COEFF_IS_MPZ(c2)) /* h is small */
42 {
43 if (c2 > 0) /* h > 0 */
44 {
45 flint_mpz_tdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), c2);
46 }
47 else
48 {
49 flint_mpz_tdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), -c2);
50 mpz_neg(mpz_ptr, mpz_ptr);
51 }
52 }
53 else /* both are large */
54 {
55 mpz_tdiv_q(mpz_ptr, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
56 }
57 _fmpz_demote_val(f); /* division by h may result in small value */
58 }
59 }
60