1/* Test file for mpfr_sinh_cosh.
2
3Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4Contributed by the AriC and Caramel 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
26#include "mpfr-test.h"
27
28#if MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0)
29
30static void
31failed (mpfr_t x, mpfr_t esh, mpfr_t gsh, mpfr_t ech, mpfr_t gch)
32{
33  printf ("error : mpfr_sinh_cosh (x) x = ");
34  mpfr_out_str (stdout, 10, 0, x, MPFR_RNDD);
35  printf ("\nsinh(x) expected ");
36  mpfr_out_str (stdout, 10, 0, esh, MPFR_RNDD);
37  printf ("\n        got ");
38  mpfr_out_str (stdout, 10, 0, gsh, MPFR_RNDD);
39  printf ("\ncosh(x) expected ");
40  mpfr_out_str (stdout, 10, 0, ech, MPFR_RNDD);
41  printf ("\n        got ");
42  mpfr_out_str (stdout, 10, 0, gch, MPFR_RNDD);
43  putchar ('\n');
44
45  mpfr_clears (x, esh, gsh, ech, gch, (mpfr_ptr) 0);
46  exit (1);
47}
48
49/* check against sinh, cosh */
50static void
51check (mpfr_t x, mpfr_rnd_t rnd)
52{
53  mpfr_t s, c, sx, cx;
54  int isc, is, ic;
55
56  mpfr_inits2 (MPFR_PREC(x), s, c, sx, cx, (mpfr_ptr) 0);
57
58  isc = mpfr_sinh_cosh (sx, cx, x, rnd);
59  is = mpfr_sinh (s, x, rnd);
60  ic = mpfr_cosh (c, x, rnd);
61
62  if (!mpfr_equal_p (s, sx) || !mpfr_equal_p (c, cx))
63    failed (x, s, sx, c, cx);
64  MPFR_ASSERTN (isc = is || ic);
65
66  mpfr_clears (s, c, sx, cx, (mpfr_ptr) 0);
67}
68
69static void
70check_nans (void)
71{
72  mpfr_t  x, sh, ch;
73
74  mpfr_init2 (x, 123);
75  mpfr_init2 (sh, 123);
76  mpfr_init2 (ch, 123);
77
78  /* nan */
79  mpfr_set_nan (x);
80  mpfr_sinh_cosh (sh, ch, x, MPFR_RNDN);
81  MPFR_ASSERTN (mpfr_nan_p (sh));
82  MPFR_ASSERTN (mpfr_nan_p (ch));
83
84  /* +inf */
85  mpfr_set_inf (x, 1);
86  mpfr_sinh_cosh (sh, ch, x, MPFR_RNDN);
87  MPFR_ASSERTN (mpfr_inf_p (sh));
88  MPFR_ASSERTN (mpfr_sgn (sh) > 0);
89  MPFR_ASSERTN (mpfr_inf_p (ch));
90  MPFR_ASSERTN (mpfr_sgn (ch) > 0);
91
92  /* -inf */
93  mpfr_set_inf (x, -1);
94  mpfr_sinh_cosh (sh, ch, x, MPFR_RNDN);
95  MPFR_ASSERTN (mpfr_inf_p (sh));
96  MPFR_ASSERTN (mpfr_sgn (sh) < 0);
97  MPFR_ASSERTN (mpfr_inf_p (ch));
98  MPFR_ASSERTN (mpfr_sgn (ch) > 0);
99
100  mpfr_clear (x);
101  mpfr_clear (sh);
102  mpfr_clear (ch);
103}
104
105int
106main (int argc, char *argv[])
107{
108  int i;
109  mpfr_t x;
110
111  tests_start_mpfr ();
112
113  check_nans ();
114
115  /* check against values given by sinh(x), cosh(x) */
116  mpfr_init2 (x, 53);
117  mpfr_set_str (x, "FEDCBA987654321p-48", 16, MPFR_RNDN);
118  for (i = 0; i < 10; ++i)
119    {
120      /* x = i - x / 2 : boggle sign and bits */
121      mpfr_ui_sub (x, i, x, MPFR_RNDD);
122      mpfr_div_2ui (x, x, 2, MPFR_RNDD);
123
124      check (x, MPFR_RNDN);
125      check (x, MPFR_RNDU);
126      check (x, MPFR_RNDD);
127    }
128  mpfr_clear (x);
129
130  tests_end_mpfr ();
131  return 0;
132}
133
134#else
135
136int
137main (void)
138{
139  printf ("Warning! Test disabled for this MPFR version.\n");
140  return 0;
141}
142
143#endif
144