Set license using %license
[platform/upstream/mpfr.git] / tests / tget_d_2exp.c
1 /* Test mpfr_get_d_2exp.
2
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 <stdio.h>
24 #include <stdlib.h>
25 #include <float.h>
26
27 #include "mpfr-test.h"
28
29
30 /* Check that hardware rounding doesn't make mpfr_get_d_2exp return a value
31    outside its defined range. */
32 static void
33 check_round (void)
34 {
35   static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
36   mpfr_t  f;
37   double  got;
38   long    got_exp;
39   int     i, rnd_mode, neg;
40
41   mpfr_init2 (f, 1024L);
42
43   for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX ; rnd_mode++)
44     {
45       for (i = 0; i < (int) numberof (data); i++)
46         {
47           mpfr_set_ui (f, 1L, MPFR_RNDZ);
48           mpfr_mul_2exp (f, f, data[i], MPFR_RNDZ);
49           mpfr_sub_ui (f, f, 1L, MPFR_RNDZ);
50
51           for (neg = 0; neg <= 1; neg++)
52             {
53               got = mpfr_get_d_2exp (&got_exp, f, (mpfr_rnd_t) rnd_mode);
54
55               if (neg == 0
56                   ? (got < 0.5 || got >= 1.0)
57                   : (got <= -1.0 || got > -0.5))
58                 {
59                   printf  ("mpfr_get_d_2exp wrong on 2**%lu-1\n", data[i]);
60                   printf  ("result out of range, expect 0.5 <= got < 1.0\n");
61                   printf  ("   rnd_mode = %d\n", rnd_mode);
62                   printf  ("   data[i]  = %lu\n", data[i]);
63                   printf  ("   f    ");
64                   mpfr_out_str (stdout, 2, 0, f, MPFR_RNDN);
65                   printf  ("\n");
66                   d_trace ("   got  ", got);
67                   printf  ("   got exp  %ld\n", got_exp);
68                   exit(1);
69                 }
70
71               mpfr_neg (f, f, MPFR_RNDZ);
72             }
73         }
74     }
75
76   mpfr_clear (f);
77 }
78
79
80 static void
81 check_inf_nan (void)
82 {
83   /* only if nans and infs are available */
84 #if _GMP_IEEE_FLOATS
85   mpfr_t  x;
86   double  d;
87   long    exp;
88
89   mpfr_init2 (x, 123);
90
91   mpfr_set_inf (x, 1);
92   d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
93   ASSERT_ALWAYS (d > 0);
94   ASSERT_ALWAYS (DOUBLE_ISINF (d));
95
96   mpfr_set_inf (x, -1);
97   d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
98   ASSERT_ALWAYS (d < 0);
99   ASSERT_ALWAYS (DOUBLE_ISINF (d));
100
101   mpfr_set_nan (x);
102   d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
103   ASSERT_ALWAYS (DOUBLE_ISNAN (d));
104
105   mpfr_clear (x);
106 #endif
107 }
108
109
110 int
111 main (void)
112 {
113   tests_start_mpfr ();
114   mpfr_test_init ();
115
116   check_round ();
117   check_inf_nan ();
118
119   tests_end_mpfr ();
120   return 0;
121 }