1 /* Test file for mpfr_sub_d
2
3 Copyright 2007-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba 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 https://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 <float.h>
24
25 #include "mpfr-test.h"
26
27 static void
check_nans(void)28 check_nans (void)
29 {
30 mpfr_t x, y;
31 int inexact;
32
33 mpfr_init2 (x, 123);
34 mpfr_init2 (y, 123);
35
36 /* nan - 1.0 is nan */
37 mpfr_set_nan (x);
38 mpfr_clear_flags ();
39 inexact = mpfr_sub_d (y, x, 1.0, MPFR_RNDN);
40 MPFR_ASSERTN (inexact == 0);
41 MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
42 MPFR_ASSERTN (mpfr_nan_p (y));
43
44 /* +inf - 1.0 == +inf */
45 mpfr_set_inf (x, 1);
46 mpfr_clear_flags ();
47 inexact = mpfr_sub_d (y, x, 1.0, MPFR_RNDN);
48 MPFR_ASSERTN (inexact == 0);
49 MPFR_ASSERTN (__gmpfr_flags == 0);
50 MPFR_ASSERTN (mpfr_inf_p (y));
51 MPFR_ASSERTN (MPFR_IS_POS (y));
52
53 /* -inf - 1.0 == -inf */
54 mpfr_set_inf (x, -1);
55 mpfr_clear_flags ();
56 inexact = mpfr_sub_d (y, x, 1.0, MPFR_RNDN);
57 MPFR_ASSERTN (inexact == 0);
58 MPFR_ASSERTN (__gmpfr_flags == 0);
59 MPFR_ASSERTN (mpfr_inf_p (y));
60 MPFR_ASSERTN (MPFR_IS_NEG (y));
61
62 mpfr_clear (x);
63 mpfr_clear (y);
64 }
65
66 #define TEST_FUNCTION mpfr_sub_d
67 #define DOUBLE_ARG2
68 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
69 #include "tgeneric.c"
70
71 int
main(void)72 main (void)
73 {
74 mpfr_t x, y, z;
75 double d;
76 int inexact;
77
78 tests_start_mpfr ();
79
80 /* check with enough precision */
81 mpfr_init2 (x, IEEE_DBL_MANT_DIG);
82 mpfr_init2 (y, IEEE_DBL_MANT_DIG);
83 mpfr_init2 (z, IEEE_DBL_MANT_DIG);
84
85 mpfr_set_str (y, "4096", 10, MPFR_RNDN);
86 d = 0.125;
87 mpfr_clear_flags ();
88 inexact = mpfr_sub_d (x, y, d, MPFR_RNDN);
89 if (inexact != 0)
90 {
91 printf ("Inexact flag error in mpfr_sub_d\n");
92 exit (1);
93 }
94 mpfr_set_str (z, "4095.875", 10, MPFR_RNDN);
95 if (mpfr_cmp (z, x))
96 {
97 printf ("Error in mpfr_sub_d (");
98 mpfr_out_str (stdout, 10, 7, y, MPFR_RNDN);
99 printf (" + %.20g)\nexpected ", d);
100 mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN);
101 printf ("\ngot ");
102 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
103 printf ("\n");
104 exit (1);
105 }
106 mpfr_clears (x, y, z, (mpfr_ptr) 0);
107
108 check_nans ();
109
110 test_generic (MPFR_PREC_MIN, 1000, 100);
111
112 tests_end_mpfr ();
113 return 0;
114 }
115