Import Upstream version 0.8.2
[platform/upstream/mpc.git] / tests / tdiv.c
1 /* tdiv -- test file for mpc_div.
2
3 Copyright (C) 2002, 2008, 2009 Andreas Enge, Paul Zimmermann, Philippe Th\'eveny
4
5 This file is part of the MPC Library.
6
7 The MPC Library is free software; you can redistribute it and/or modify
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
10 option) any later version.
11
12 The MPC Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPC 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 <stdlib.h>
23 #include "mpc-tests.h"
24
25 static void
26 check_regular (void)
27 {
28   mpc_t b, c, q;
29   int inex;
30
31   mpc_init2 (b, 10);
32   mpc_init2 (c, 10);
33   mpc_init2 (q, 10);
34
35   /* inexact result */
36   mpc_set_ui_ui (b, 973, 964, MPC_RNDNN);
37   mpc_set_ui_ui (c, 725, 745, MPC_RNDNN);
38   inex = mpc_div (q, b, c, MPC_RNDZZ);
39   mpc_set_si_si (b, 43136, -787, MPC_RNDNN);
40   mpc_div_2exp (b, b, 15, MPC_RNDNN);
41   if (mpc_cmp (q, b) || MPC_INEX_RE (inex) == 0 || MPC_INEX_IM (inex) == 0)
42     {
43       printf ("mpc_div failed for (973+I*964)/(725+I*745)\n");
44       exit (1);
45     }
46
47   /* exact result */
48   mpc_set_si_si (b, -837, 637, MPC_RNDNN);
49   mpc_set_si_si (c, 63, -5, MPC_RNDNN);
50   inex = mpc_div (q, b, c, MPC_RNDZN);
51   mpc_set_si_si (b, -14, 9, MPC_RNDNN);
52   if (mpc_cmp (q, b) || inex != 0)
53     {
54       printf ("mpc_div failed for (-837+I*637)/(63-I*5)\n");
55       exit (1);
56     }
57
58   mpc_set_prec (b, 2);
59   mpc_set_prec (c, 2);
60   mpc_set_prec (q, 2);
61
62   /* exact result */
63   mpc_set_ui_ui (b, 4, 3, MPC_RNDNN);
64   mpc_set_ui_ui (c, 1, 2, MPC_RNDNN);
65   inex = mpc_div (q, b, c, MPC_RNDNN);
66   mpc_set_si_si (b, 2, -1, MPC_RNDNN);
67   if (mpc_cmp (q, b) || inex != 0)
68     {
69       printf ("mpc_div failed for (4+I*3)/(1+I*2)\n");
70       exit (1);
71     }
72
73   /* pure real dividend BUG-20080923 */
74   mpc_set_prec (b, 4);
75   mpc_set_prec (c, 4);
76   mpc_set_prec (q, 4);
77
78   mpc_set_si_si (b, -3, 0, MPC_RNDNN);
79   mpc_div_2exp (b, b, 206, MPC_RNDNN);
80   mpc_set_si_si (c, -1, -5, MPC_RNDNN);
81   mpfr_div_2ui (MPC_RE (c), MPC_RE (c), 733, GMP_RNDN);
82   mpfr_div_2ui (MPC_IM (c), MPC_IM (c), 1750, GMP_RNDN);
83   inex = mpc_div (q, b, c, MPC_RNDNZ);
84   mpc_set_si_si (b, 3, -7, MPC_RNDNN);
85   mpfr_mul_2ui (MPC_RE (b), MPC_RE (b), 527, GMP_RNDN);
86   mpfr_div_2ui (MPC_IM (b), MPC_IM (b), 489, GMP_RNDN);
87
88   if (mpc_cmp (q, b))
89     {
90       printf ("mpc_div failed for -3p-206/(-1p-733 -I* 5p-1750)\n");
91       exit (1);
92     }
93
94   /* pure real divisor */
95   mpc_set_prec (b, 4);
96   mpc_set_prec (c, 4);
97   mpc_set_prec (q, 4);
98   mpc_set_si_si (b, 15, 14, MPC_RNDNN);
99   mpc_set_si_si (c, 11, 0, MPC_RNDNN);
100   inex = mpc_div (q, b, c, MPC_RNDNN); /* should be 11/8 + 5/4*I */
101   mpc_set_si_si (b, 11, 10, MPC_RNDNN);
102   mpc_div_ui (b, b, 8, MPC_RNDNN);
103   if (mpc_cmp (q, b) || inex != MPC_INEX(1, -1))
104     {
105       printf ("mpc_div failed for (15+14*I)/11\n");
106       exit (1);
107     }
108
109   mpc_clear (b);
110   mpc_clear (c);
111   mpc_clear (q);
112 }
113
114 int
115 main (void)
116 {
117   DECL_FUNC (CCC, f, mpc_div);
118
119   test_start ();
120
121   check_regular ();
122
123   data_check (f, "div.dat");
124   tgeneric (f, 2, 1024, 7, 4096);
125
126   test_end ();
127   return 0;
128 }