1 /* tset.c -- Test file for mpfi_set functions.
2 
3 Copyright 2009, 2010,
4                      Spaces project, Inria Lorraine
5                      and Salsa project, INRIA Rocquencourt,
6                      and Arenaire project, Inria Rhone-Alpes, France
7                      and Lab. ANO, USTL (Univ. of Lille),  France
8 
9 
10 This file is part of the MPFI Library.
11 
12 The MPFI Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or (at your
15 option) any later version.
16 
17 The MPFI Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20 License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public License
23 along with the MPFI Library; see the file COPYING.LIB.  If not, write to
24 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 MA 02110-1301, USA. */
26 
27 #include "mpfi-tests.h"
28 
29 void
check_endpoints(mpfi_t i,const char * left,const char * right,const char * function_name)30 check_endpoints (mpfi_t i, const char *left, const char *right,
31 		 const char *function_name)
32 {
33   mpfr_t l,r;
34 
35   mpfr_inits2 (mpfi_get_prec (i), l, r, (mpfr_ptr)0);
36 
37   mpfr_set_str (l, left, 0, MPFI_RNDD);
38   mpfr_set_str (r, right, 0, MPFI_RNDU);
39 
40   if (!mpfr_equal_p (&(i->left), l) || !mpfr_equal_p (&(i->right), r)) {
41     printf ("Error in %s\nexpected [%s, %s]\n     got ",
42 	    function_name, left, right);
43     mpfi_out_str (stdout, 16, 0, i);
44     putchar ('\n');
45     exit (1);
46   }
47 
48   mpfr_clears (l, r, (mpfr_ptr)0);
49 }
50 
51 void
check_fi(mpfi_ptr out,mpfi_ptr in,int expected_inex,const char * left,const char * right)52 check_fi (mpfi_ptr out, mpfi_ptr in, int expected_inex,
53 	  const char *left, const char *right)
54 {
55   int inex;
56 
57   inex = mpfi_set (out, in);
58   if (inex != expected_inex) {
59     printf ("Error: mpfi_set returns unexpected value with input=");
60     mpfi_out_str (stdout, 10, 0, in);
61     printf (" and output precision=%lu\nexpected return value: %u, got: %u\n",
62 	    mpfi_get_prec (out), expected_inex, inex);
63     exit (1);
64   }
65   check_endpoints (out, left, right, "mpfi_set");
66 }
67 
68 int
main(int argc,char ** argv)69 main (int argc, char **argv)
70 {
71   mpfi_t fi1;
72   mpfi_t fi2;
73 
74   mpfi_init2 (fi1, 1024);
75   mpfi_init2 (fi2, 1024);
76 
77   mpfi_set_prec (fi1, 2);
78   mpfi_set_prec (fi2, 53);
79   mpfr_set_str (&(fi1->left),  "0.1", 0, MPFI_RNDD);
80   mpfr_set_str (&(fi1->right), "0.1", 0, MPFI_RNDU);
81   check_fi (fi2, fi1, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT,
82 	    "0b11p-5", "0b10p-4");
83 
84   mpfr_set_nan (&(fi1->left));
85   mpfr_set_nan (&(fi1->right));
86   mpfi_set (fi2, fi1);
87   if (!mpfr_nan_p (&(fi2->left)) || !mpfr_nan_p (&(fi2->right))) {
88     printf ("Error: mpfi_set does not handle NAN correctly\ngot: ");
89     mpfi_out_str (stdout, 10, 0, fi2);
90     putchar ('\n');
91     exit (1);
92   }
93 
94   mpfi_set_prec (fi1, 53);
95   mpfi_set_prec (fi2, 2);
96   mpfr_set_str (&(fi1->left),  "0.1", 0, MPFI_RNDD);
97   mpfr_set_str (&(fi1->right), "0.1", 0, MPFI_RNDU);
98   check_fi (fi2, fi1, MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT,
99 	    "0x18@-2", "0x2@-1");
100 
101   mpfr_set_str (&(fi1->left),  "0.1", 0, MPFI_RNDD);
102   mpfr_set_str (&(fi1->right), "0.5", 0, MPFI_RNDU);
103   check_fi (fi2, fi1, MPFI_FLAGS_LEFT_ENDPOINT_INEXACT,
104 	    "0x18@-2", "0x8@-1");
105 
106   mpfr_set_str (&(fi1->left),  "-0.5", 0, MPFI_RNDD);
107   mpfr_set_str (&(fi1->right), "-0.1", 0, MPFI_RNDU);
108   check_fi (fi2, fi1, MPFI_FLAGS_RIGHT_ENDPOINT_INEXACT,
109 	    "-0x8@-1", "-0x18@-2");
110 
111   mpfr_set_inf (&(fi1->left),  -1);
112   mpfr_set_inf (&(fi1->right), -1);
113   check_fi (fi2, fi1, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT,
114 	    "-@inf@", "-@inf@");
115 
116   /* signed zeros */
117   mpfr_set_ui (&(fi1->left), 0, MPFI_RNDU);
118   mpfr_neg (&(fi1->right), &(fi1->left), MPFI_RNDD);
119   check_fi (fi2, fi1, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT,
120 	    "+0", "-0");
121   if (mpfr_signbit (&(fi2->left)) || !mpfr_signbit (&(fi2->right))) {
122     printf ("Error: mpfi_set does not handle signed zeros correctly\ngot: ");
123     mpfi_out_str (stdout, 10, 0, fi2);
124     putchar ('\n');
125     exit (1);
126   }
127 
128   mpfi_clear (fi1);
129   mpfi_clear (fi2);
130 
131   return 0;
132 }
133