xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/td_sub.c (revision b49cc1491953ef2348eff9c84520ffd0678a5c8d)
1 /* Test file for mpfr_d_sub
2 
3 Copyright 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0)
30 
31 static void
32 check_nans (void)
33 {
34   mpfr_t  x, y;
35   int inexact;
36 
37   mpfr_init2 (x, 123);
38   mpfr_init2 (y, 123);
39 
40   /* 1.0 - nan is nan */
41   mpfr_set_nan (x);
42   mpfr_clear_flags ();
43   inexact = mpfr_d_sub (y, 1.0, x, MPFR_RNDN);
44   MPFR_ASSERTN (inexact == 0);
45   MPFR_ASSERTN ((__gmpfr_flags ^ MPFR_FLAGS_NAN) == 0);
46   MPFR_ASSERTN (mpfr_nan_p (y));
47 
48   /* 1.0 - +inf == -inf */
49   mpfr_set_inf (x, 1);
50   mpfr_clear_flags ();
51   inexact = mpfr_d_sub (y, 1.0, x, MPFR_RNDN);
52   MPFR_ASSERTN (inexact == 0);
53   MPFR_ASSERTN (__gmpfr_flags == 0);
54   MPFR_ASSERTN (mpfr_inf_p (y));
55   MPFR_ASSERTN (MPFR_IS_NEG (y));
56 
57   /* 1.0 - -inf == +inf */
58   mpfr_set_inf (x, -1);
59   mpfr_clear_flags ();
60   inexact = mpfr_d_sub (y, 1.0, x, MPFR_RNDN);
61   MPFR_ASSERTN (inexact == 0);
62   MPFR_ASSERTN (__gmpfr_flags == 0);
63   MPFR_ASSERTN (mpfr_inf_p (y));
64   MPFR_ASSERTN (MPFR_IS_POS (y));
65 
66   mpfr_clear (x);
67   mpfr_clear (y);
68 }
69 
70 #define TEST_FUNCTION mpfr_d_sub
71 #define DOUBLE_ARG1
72 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
73 #include "tgeneric.c"
74 
75 int
76 main (void)
77 {
78   mpfr_t x, y, z;
79   double d;
80   int inexact;
81 
82   tests_start_mpfr ();
83 
84   /* check with enough precision */
85   mpfr_init2 (x, IEEE_DBL_MANT_DIG);
86   mpfr_init2 (y, IEEE_DBL_MANT_DIG);
87   mpfr_init2 (z, IEEE_DBL_MANT_DIG);
88 
89   mpfr_set_str (y, "4096", 10, MPFR_RNDN);
90   d = 0.125;
91   mpfr_clear_flags ();
92   inexact = mpfr_d_sub (x, d, y, MPFR_RNDN);
93   if (inexact != 0)
94     {
95       printf ("Inexact flag error in mpfr_d_sub\n");
96       exit (1);
97     }
98   mpfr_set_str (z, "-4095.875", 10, MPFR_RNDN);
99   if (mpfr_cmp (z, x))
100     {
101       printf ("Error in mpfr_d_sub (");
102       mpfr_out_str (stdout, 10, 7, y, MPFR_RNDN);
103       printf (" + %.20g)\nexpected ", d);
104       mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN);
105       printf ("\ngot     ");
106       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
107       printf ("\n");
108       exit (1);
109     }
110   mpfr_clears (x, y, z, (mpfr_ptr) 0);
111 
112   check_nans ();
113 
114   test_generic (2, 1000, 100);
115 
116   tests_end_mpfr ();
117   return 0;
118 }
119 
120 #else
121 
122 int
123 main (void)
124 {
125   printf ("Warning! Test disabled for this MPFR version.\n");
126   return 0;
127 }
128 
129 #endif
130