Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tfrexp.c
1 /* Test file for mpfr_frexp.
2
3 Copyright 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR 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 Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include <stdlib.h> /* for exit */
24 #include "mpfr-test.h"
25
26 static void
27 check_special (void)
28 {
29   mpfr_t x, y;
30   int inex;
31   mpfr_exp_t exp;
32
33   mpfr_init2 (x, 53);
34   mpfr_init2 (y, 53);
35
36   mpfr_set_nan (x);
37   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
38   if (mpfr_nan_p (y) == 0 || inex != 0)
39     {
40       printf ("Error for mpfr_frexp(NaN)\n");
41       exit (1);
42     }
43
44   mpfr_set_inf (x, 1);
45   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
46   if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0 || inex != 0)
47     {
48       printf ("Error for mpfr_frexp(+Inf)\n");
49       exit (1);
50     }
51
52   mpfr_set_inf (x, -1);
53   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
54   if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) >= 0 || inex != 0)
55     {
56       printf ("Error for mpfr_frexp(-Inf)\n");
57       exit (1);
58     }
59
60   mpfr_set_zero (x, 1);
61   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
62   if (mpfr_zero_p (y) == 0 || mpfr_signbit (y) != 0 || inex != 0 || exp != 0)
63     {
64       printf ("Error for mpfr_frexp(+0)\n");
65       exit (1);
66     }
67
68   mpfr_set_zero (x, -1);
69   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
70   if (mpfr_zero_p (y) == 0 || mpfr_signbit (y) == 0 || inex != 0 || exp != 0)
71     {
72       printf ("Error for mpfr_frexp(-0)\n");
73       exit (1);
74     }
75
76   mpfr_set_ui (x, 17, MPFR_RNDN);
77   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
78   /* 17 = 17/32*2^5 */
79   if (mpfr_cmp_ui_2exp (y, 17, -5) != 0 || inex != 0 || exp != 5)
80     {
81       printf ("Error for mpfr_frexp(17)\n");
82       exit (1);
83     }
84
85   mpfr_set_si (x, -17, MPFR_RNDN);
86   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
87   if (mpfr_cmp_si_2exp (y, -17, -5) != 0 || inex != 0 || exp != 5)
88     {
89       printf ("Error for mpfr_frexp(-17)\n");
90       exit (1);
91     }
92
93   /* now reduce the precision of y */
94   mpfr_set_prec (y, 4);
95   mpfr_set_ui (x, 17, MPFR_RNDN);
96   inex = mpfr_frexp (&exp, y, x, MPFR_RNDN);
97   /* 17 -> 16/32*2^5 */
98   if (mpfr_cmp_ui_2exp (y, 16, -5) != 0 || inex >= 0 || exp != 5)
99     {
100       printf ("Error for mpfr_frexp(17) with prec=4, RNDN\n");
101       exit (1);
102     }
103
104   mpfr_set_ui (x, 17, MPFR_RNDN);
105   inex = mpfr_frexp (&exp, y, x, MPFR_RNDZ);
106   if (mpfr_cmp_ui_2exp (y, 16, -5) != 0 || inex >= 0 || exp != 5)
107     {
108       printf ("Error for mpfr_frexp(17) with prec=4, RNDZ\n");
109       exit (1);
110     }
111
112   mpfr_set_ui (x, 17, MPFR_RNDN);
113   inex = mpfr_frexp (&exp, y, x, MPFR_RNDD);
114   if (mpfr_cmp_ui_2exp (y, 16, -5) != 0 || inex >= 0 || exp != 5)
115     {
116       printf ("Error for mpfr_frexp(17) with prec=4, RNDD\n");
117       exit (1);
118     }
119
120   mpfr_set_ui (x, 17, MPFR_RNDN);
121   inex = mpfr_frexp (&exp, y, x, MPFR_RNDU);
122   if (mpfr_cmp_ui_2exp (y, 18, -5) != 0 || inex <= 0 || exp != 5)
123     {
124       printf ("Error for mpfr_frexp(17) with prec=4, RNDU\n");
125       exit (1);
126     }
127
128   mpfr_clear (y);
129   mpfr_clear (x);
130 }
131
132 int
133 main (int argc, char *argv[])
134 {
135   tests_start_mpfr ();
136
137   check_special ();
138
139   tests_end_mpfr ();
140   return 0;
141 }