1 /* mpq_inv(dest,src) -- invert a rational number, i.e. set DEST to SRC
2 with the numerator and denominator swapped.
4 Copyright (C) 1991 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 The GNU MP Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the GNU MP Library; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 mpq_inv (MP_RAT *dest, const MP_RAT *src)
34 mp_size num_size = src->num.size;
35 mp_size den_size = src->den.size;
38 num_size = 1 / num_size; /* Divide by zero! */
45 dest->den.size = num_size;
46 dest->num.size = den_size;
48 /* If dest == src we may just swap the numerator and denominator, but
49 we have to ensure the new denominator is positive. */
53 mp_size alloc = dest->num.alloc;
54 mp_ptr limb_ptr = dest->num.d;
56 dest->num.alloc = dest->den.alloc;
57 dest->num.d = dest->den.d;
59 dest->den.alloc = alloc;
60 dest->den.d = limb_ptr;
64 den_size = ABS (den_size);
65 if (dest->num.alloc < den_size)
66 _mpz_realloc (&(dest->num), den_size);
68 if (dest->den.alloc < num_size)
69 _mpz_realloc (&(dest->den), num_size);
71 MPN_COPY (dest->num.d, src->den.d, den_size);
72 MPN_COPY (dest->den.d, src->num.d, num_size);