1/* Test file for mpfr_sub_ui
2
3Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4Contributed by the Arenaire and Cacao projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 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/* checks that x-y gives the right results with 53 bits of precision */
30static void
31check3 (const char *xs, unsigned long y, mpfr_rnd_t rnd_mode, const char *zs)
32{
33  mpfr_t xx,zz;
34
35  mpfr_inits2 (53, xx, zz, (mpfr_ptr) 0);
36  mpfr_set_str1 (xx, xs);
37  mpfr_sub_ui (zz, xx, y, rnd_mode);
38  if (mpfr_cmp_str1(zz, zs))
39    {
40      printf ("expected sum is %s, got ", zs);
41      mpfr_print_binary(zz);
42      printf ("\nmpfr_sub_ui failed for x=%s y=%lu with rnd_mode=%s\n",
43              xs, y, mpfr_print_rnd_mode (rnd_mode));
44      exit (1);
45    }
46  mpfr_clears (xx, zz, (mpfr_ptr) 0);
47}
48
49/* FastTwoSum: if EXP(x) >= EXP(y), u = o(x+y), v = o(u-x), w = o(y-v),
50               then x + y = u + w
51thus if u = o(y-x), v = o(u+x), w = o(v-y), then y-x = u-w */
52static void
53check_two_sum (mpfr_prec_t p)
54{
55  unsigned int x;
56  mpfr_t y, u, v, w;
57  mpfr_rnd_t rnd;
58  int inexact;
59
60  mpfr_inits2 (p, y, u, v, w, (mpfr_ptr) 0);
61  do
62    {
63      x = randlimb ();
64    }
65  while (x < 1);
66  mpfr_urandomb (y, RANDS);
67  rnd = MPFR_RNDN;
68  inexact = mpfr_sub_ui (u, y, x, rnd);
69  mpfr_add_ui (v, u, x, rnd);
70  mpfr_sub (w, v, y, rnd);
71  /* as u - (y-x) = w, we should have inexact and w of same sign */
72  if (((inexact == 0) && mpfr_cmp_ui (w, 0)) ||
73      ((inexact > 0) && (mpfr_cmp_ui (w, 0) <= 0)) ||
74      ((inexact < 0) && (mpfr_cmp_ui (w, 0) >= 0)))
75    {
76      printf ("Wrong inexact flag for prec=%u, rnd=%s\n",
77              (unsigned int) p, mpfr_print_rnd_mode (rnd));
78      printf ("x=%u\n", x);
79      printf ("y="); mpfr_print_binary(y); puts ("");
80      printf ("u="); mpfr_print_binary(u); puts ("");
81      printf ("v="); mpfr_print_binary(v); puts ("");
82      printf ("w="); mpfr_print_binary(w); puts ("");
83      printf ("inexact = %d\n", inexact);
84      exit (1);
85    }
86  mpfr_clears (y, u, v, w, (mpfr_ptr) 0);
87}
88
89static void
90check_nans (void)
91{
92  mpfr_t  x, y;
93
94  mpfr_init2 (x, 123L);
95  mpfr_init2 (y, 123L);
96
97  /* nan - 1 == nan */
98  mpfr_set_nan (x);
99  mpfr_sub_ui (y, x, 1L, MPFR_RNDN);
100  MPFR_ASSERTN (mpfr_nan_p (y));
101
102  /* +inf - 1 == +inf */
103  mpfr_set_inf (x, 1);
104  mpfr_sub_ui (y, x, 1L, MPFR_RNDN);
105  MPFR_ASSERTN (mpfr_inf_p (y));
106  MPFR_ASSERTN (mpfr_sgn (y) > 0);
107
108  /* -inf - 1 == -inf */
109  mpfr_set_inf (x, -1);
110  mpfr_sub_ui (y, x, 1L, MPFR_RNDN);
111  MPFR_ASSERTN (mpfr_inf_p (y));
112  MPFR_ASSERTN (mpfr_sgn (y) < 0);
113
114  mpfr_clear (x);
115  mpfr_clear (y);
116}
117
118#define TEST_FUNCTION mpfr_sub_ui
119#define INTEGER_TYPE  unsigned long
120#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
121#include "tgeneric_ui.c"
122
123int
124main (int argc, char *argv[])
125{
126  mpfr_prec_t p;
127  int k;
128
129  tests_start_mpfr ();
130
131  check_nans ();
132
133  for (p=2; p<200; p++)
134    for (k=0; k<200; k++)
135      check_two_sum (p);
136
137  check3 ("0.9999999999", 1, MPFR_RNDN,
138          "-10000000827403709990903735160827636718750e-50");
139
140  test_generic_ui (2, 1000, 100);
141
142  tests_end_mpfr ();
143  return 0;
144}
145