[BACK]Return to inv.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpq

Annotation of OpenXM_contrib/gmp/mpq/inv.c, Revision 1.1

1.1     ! maekawa     1: /* mpq_inv(dest,src) -- invert a rational number, i.e. set DEST to SRC
        !             2:    with the numerator and denominator swapped.
        !             3:
        !             4: Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
        !             5:
        !             6: This file is part of the GNU MP Library.
        !             7:
        !             8: The GNU MP Library is free software; you can redistribute it and/or modify
        !             9: it under the terms of the GNU Library General Public License as published by
        !            10: the Free Software Foundation; either version 2 of the License, or (at your
        !            11: option) any later version.
        !            12:
        !            13: The GNU MP Library is distributed in the hope that it will be useful, but
        !            14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
        !            16: License for more details.
        !            17:
        !            18: You should have received a copy of the GNU Library General Public License
        !            19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            21: MA 02111-1307, USA. */
        !            22:
        !            23: #include "gmp.h"
        !            24: #include "gmp-impl.h"
        !            25:
        !            26: void
        !            27: #if __STDC__
        !            28: mpq_inv (MP_RAT *dest, const MP_RAT *src)
        !            29: #else
        !            30: mpq_inv (dest, src)
        !            31:      MP_RAT *dest;
        !            32:      const MP_RAT *src;
        !            33: #endif
        !            34: {
        !            35:   mp_size_t num_size = src->_mp_num._mp_size;
        !            36:   mp_size_t den_size = src->_mp_den._mp_size;
        !            37:
        !            38:   if (num_size == 0)
        !            39:     num_size = 1 / num_size;   /* Divide by zero!  */
        !            40:
        !            41:   if (num_size < 0)
        !            42:     {
        !            43:       num_size = -num_size;
        !            44:       den_size = -den_size;
        !            45:     }
        !            46:   dest->_mp_den._mp_size = num_size;
        !            47:   dest->_mp_num._mp_size = den_size;
        !            48:
        !            49:   /* If dest == src we may just swap the numerator and denominator, but
        !            50:      we have to ensure the new denominator is positive.  */
        !            51:
        !            52:   if (dest == src)
        !            53:     {
        !            54:       mp_size_t alloc = dest->_mp_num._mp_alloc;
        !            55:       mp_ptr limb_ptr = dest->_mp_num._mp_d;
        !            56:
        !            57:       dest->_mp_num._mp_alloc = dest->_mp_den._mp_alloc;
        !            58:       dest->_mp_num._mp_d = dest->_mp_den._mp_d;
        !            59:
        !            60:       dest->_mp_den._mp_alloc = alloc;
        !            61:       dest->_mp_den._mp_d = limb_ptr;
        !            62:     }
        !            63:   else
        !            64:     {
        !            65:       den_size = ABS (den_size);
        !            66:       if (dest->_mp_num._mp_alloc < den_size)
        !            67:        _mpz_realloc (&(dest->_mp_num), den_size);
        !            68:
        !            69:       if (dest->_mp_den._mp_alloc < num_size)
        !            70:        _mpz_realloc (&(dest->_mp_den), num_size);
        !            71:
        !            72:       MPN_COPY (dest->_mp_num._mp_d, src->_mp_den._mp_d, den_size);
        !            73:       MPN_COPY (dest->_mp_den._mp_d, src->_mp_num._mp_d, num_size);
        !            74:     }
        !            75: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>