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

Annotation of OpenXM_contrib/gmp/mpq/div.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpq_div -- divide two rational numbers.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1994, 1995, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa     4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2 ! maekawa     8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    10: option) any later version.
                     11:
                     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
1.1.1.2 ! maekawa    14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! maekawa    17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include "gmp.h"
                     23: #include "gmp-impl.h"
                     24:
                     25: void
                     26: #if __STDC__
                     27: mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2)
                     28: #else
                     29: mpq_div (quot, op1, op2)
                     30:      mpq_ptr quot;
                     31:      mpq_srcptr op1;
                     32:      mpq_srcptr op2;
                     33: #endif
                     34: {
                     35:   mpz_t gcd1, gcd2;
                     36:   mpz_t tmp1, tmp2;
                     37:   mpz_t numtmp;
1.1.1.2 ! maekawa    38:
        !            39:   if (op2->_mp_num._mp_size == 0)
        !            40:     DIVIDE_BY_ZERO;
1.1       maekawa    41:
                     42:   mpz_init (gcd1);
                     43:   mpz_init (gcd2);
                     44:   mpz_init (tmp1);
                     45:   mpz_init (tmp2);
                     46:   mpz_init (numtmp);
                     47:
                     48:   /* QUOT might be identical to either operand, so don't store the
                     49:      result there until we are finished with the input operands.  We
                     50:      dare to overwrite the numerator of QUOT when we are finished
                     51:      with the numerators of OP1 and OP2.  */
                     52:
                     53:   mpz_gcd (gcd1, &(op1->_mp_num), &(op2->_mp_num));
                     54:   mpz_gcd (gcd2, &(op2->_mp_den), &(op1->_mp_den));
                     55:
                     56:   if (gcd1->_mp_size > 1 || gcd1->_mp_d[0] != 1)
                     57:     mpz_divexact (tmp1, &(op1->_mp_num), gcd1);
                     58:   else
                     59:     mpz_set (tmp1, &(op1->_mp_num));
                     60:
                     61:   if (gcd2->_mp_size > 1 || gcd2->_mp_d[0] != 1)
                     62:     mpz_divexact (tmp2, &(op2->_mp_den), gcd2);
                     63:   else
                     64:     mpz_set (tmp2, &(op2->_mp_den));
                     65:
                     66:   mpz_mul (numtmp, tmp1, tmp2);
                     67:
                     68:   if (gcd1->_mp_size > 1 || gcd1->_mp_d[0] != 1)
                     69:     mpz_divexact (tmp1, &(op2->_mp_num), gcd1);
                     70:   else
                     71:     mpz_set (tmp1, &(op2->_mp_num));
                     72:
                     73:   if (gcd2->_mp_size > 1 || gcd2->_mp_d[0] != 1)
                     74:     mpz_divexact (tmp2, &(op1->_mp_den), gcd2);
                     75:   else
                     76:     mpz_set (tmp2, &(op1->_mp_den));
                     77:
                     78:   mpz_mul (&(quot->_mp_den), tmp1, tmp2);
                     79:
                     80:   /* We needed to go via NUMTMP to take care of QUOT being the same
                     81:      as either input operands.  Now move NUMTMP to QUOT->_mp_num.  */
                     82:   mpz_set (&(quot->_mp_num), numtmp);
                     83:
                     84:   /* Keep the denominator positive.  */
                     85:   if (quot->_mp_den._mp_size < 0)
                     86:     {
                     87:       quot->_mp_den._mp_size = -quot->_mp_den._mp_size;
                     88:       quot->_mp_num._mp_size = -quot->_mp_num._mp_size;
                     89:     }
                     90:
                     91:   mpz_clear (numtmp);
                     92:   mpz_clear (tmp2);
                     93:   mpz_clear (tmp1);
                     94:   mpz_clear (gcd2);
                     95:   mpz_clear (gcd1);
                     96: }

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