1 /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_fdiv_qr, mpz_fdiv_q,
4 Copyright (C) 1993, 1994, 1996 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 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.
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.
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. */
38 mpz_t dividend, divisor;
39 mpz_t quotient, remainder;
40 mpz_t quotient2, remainder2;
42 mp_size_t dividend_size, divisor_size;
47 reps = atoi (argv[1]);
54 mpz_init (remainder2);
57 for (i = 0; i < reps; i++)
59 dividend_size = urandom () % SIZE - SIZE/2;
60 mpz_random2 (dividend, dividend_size);
62 divisor_size = urandom () % SIZE - SIZE/2;
63 mpz_random2 (divisor, divisor_size);
64 if (mpz_cmp_ui (divisor, 0) == 0)
67 mpz_fdiv_qr (quotient, remainder, dividend, divisor);
68 mpz_fdiv_q (quotient2, dividend, divisor);
69 mpz_fdiv_r (remainder2, dividend, divisor);
71 /* First determine that the quotients and remainders computed
72 with different functions are equal. */
73 if (mpz_cmp (quotient, quotient2) != 0)
74 dump_abort (dividend, divisor);
75 if (mpz_cmp (remainder, remainder2) != 0)
76 dump_abort (dividend, divisor);
78 /* Check if the sign of the quotient is correct. */
79 if (mpz_cmp_ui (quotient, 0) != 0)
80 if ((mpz_cmp_ui (quotient, 0) < 0)
81 != ((mpz_cmp_ui (dividend, 0) ^ mpz_cmp_ui (divisor, 0)) < 0))
82 dump_abort (dividend, divisor);
84 /* Check if the remainder has the same sign as the divisor
85 (quotient rounded towards minus infinity). */
86 if (mpz_cmp_ui (remainder, 0) != 0)
87 if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (divisor, 0) < 0))
88 dump_abort (dividend, divisor);
90 mpz_mul (temp, quotient, divisor);
91 mpz_add (temp, temp, remainder);
92 if (mpz_cmp (temp, dividend) != 0)
93 dump_abort (dividend, divisor);
95 mpz_abs (temp, divisor);
96 mpz_abs (remainder, remainder);
97 if (mpz_cmp (remainder, temp) >= 0)
98 dump_abort (dividend, divisor);
104 dump_abort (dividend, divisor)
105 mpz_t dividend, divisor;
107 fprintf (stderr, "ERROR\n");
108 fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
109 fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
117 mpz_out_str (stderr, base, x); fputc ('\n', stderr);