xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/trndna.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1299c6f0cSmrg /* Test file for mpfr_round_nearest_away.
2299c6f0cSmrg 
3*ba125506Smrg Copyright 2012-2023 Free Software Foundation, Inc.
4299c6f0cSmrg Contributed by the AriC and Caramba projects, INRIA.
5299c6f0cSmrg 
6299c6f0cSmrg This file is part of the GNU MPFR Library.
7299c6f0cSmrg 
8299c6f0cSmrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9299c6f0cSmrg it under the terms of the GNU Lesser General Public License as published by
10299c6f0cSmrg the Free Software Foundation; either version 3 of the License, or (at your
11299c6f0cSmrg option) any later version.
12299c6f0cSmrg 
13299c6f0cSmrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14299c6f0cSmrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15299c6f0cSmrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16299c6f0cSmrg License for more details.
17299c6f0cSmrg 
18299c6f0cSmrg You should have received a copy of the GNU Lesser General Public License
19299c6f0cSmrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
202ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21299c6f0cSmrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22299c6f0cSmrg 
23299c6f0cSmrg #include "mpfr-test.h"
24299c6f0cSmrg 
25299c6f0cSmrg static void
test_special(void)26299c6f0cSmrg test_special (void)
27299c6f0cSmrg {
28299c6f0cSmrg   mpfr_t x, y;
29299c6f0cSmrg   int inex;
30299c6f0cSmrg 
31299c6f0cSmrg   mpfr_init2 (x, MPFR_PREC_MIN);
32299c6f0cSmrg   mpfr_init2 (y, MPFR_PREC_MIN);
33299c6f0cSmrg 
34299c6f0cSmrg   mpfr_set_nan (x);
35299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_sin, y, x);
36299c6f0cSmrg   if (inex != 0)
37299c6f0cSmrg     {
38299c6f0cSmrg       printf ("Wrong ternary value for sin(NaN)\n");
39299c6f0cSmrg       exit (1);
40299c6f0cSmrg     }
41299c6f0cSmrg   if (mpfr_nan_p (y) == 0)
42299c6f0cSmrg     {
43299c6f0cSmrg       printf ("Wrong output for sin(NaN)\n");
44299c6f0cSmrg       exit (1);
45299c6f0cSmrg     }
46299c6f0cSmrg 
47299c6f0cSmrg   mpfr_set_inf (x, 1);
48299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_exp, y, x);
49299c6f0cSmrg   if (inex != 0)
50299c6f0cSmrg     {
51299c6f0cSmrg       printf ("Wrong ternary value for exp(+Inf)\n");
52299c6f0cSmrg       printf ("expected 0, got %d\n", inex);
53299c6f0cSmrg       exit (1);
54299c6f0cSmrg     }
55299c6f0cSmrg   if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0)
56299c6f0cSmrg     {
57299c6f0cSmrg       printf ("Wrong output for exp(+Inf)\n");
58299c6f0cSmrg       exit (1);
59299c6f0cSmrg     }
60299c6f0cSmrg 
61299c6f0cSmrg   mpfr_set_inf (x, -1);
62299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_cbrt, y, x);
63299c6f0cSmrg   if (inex != 0)
64299c6f0cSmrg     {
65299c6f0cSmrg       printf ("Wrong ternary value for cbrt(-Inf)\n");
66299c6f0cSmrg       exit (1);
67299c6f0cSmrg     }
68299c6f0cSmrg   if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) >= 0)
69299c6f0cSmrg     {
70299c6f0cSmrg       printf ("Wrong output for cbrt(-Inf)\n");
71299c6f0cSmrg       exit (1);
72299c6f0cSmrg     }
73299c6f0cSmrg 
74299c6f0cSmrg   mpfr_clear (x);
75299c6f0cSmrg   mpfr_clear (y);
76299c6f0cSmrg }
77299c6f0cSmrg 
78299c6f0cSmrg static void
test_nonspecial(void)79299c6f0cSmrg test_nonspecial (void)
80299c6f0cSmrg {
81299c6f0cSmrg   mpfr_t x, y;
82299c6f0cSmrg   int inex;
83299c6f0cSmrg 
84299c6f0cSmrg   mpfr_init2 (x, 10);
85299c6f0cSmrg   mpfr_init2 (y, 10);
86299c6f0cSmrg 
87299c6f0cSmrg   /* case where the computation on n+1 bits ends with a '0' */
88299c6f0cSmrg   mpfr_set_ui (x, 2, MPFR_RNDN);
89299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_sin, y, x);
90299c6f0cSmrg   if (inex >= 0)
91299c6f0cSmrg     {
92299c6f0cSmrg       printf ("Wrong ternary value for sin(2)\n");
93299c6f0cSmrg       exit (1);
94299c6f0cSmrg     }
95299c6f0cSmrg   if (mpfr_cmp_ui_2exp (y, 931, -10) != 0)
96299c6f0cSmrg     {
97299c6f0cSmrg       printf ("Wrong output for sin(2)\n");
98299c6f0cSmrg       exit (1);
99299c6f0cSmrg     }
100299c6f0cSmrg 
101299c6f0cSmrg   /* case where the computation on n+1 bits ends with a '1' and is exact */
102299c6f0cSmrg   mpfr_set_ui (x, 37, MPFR_RNDN);
103299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_sqr, y, x);
104299c6f0cSmrg   if (inex <= 0)
105299c6f0cSmrg     {
106299c6f0cSmrg       printf ("Wrong ternary value for sqr(37)\n");
107299c6f0cSmrg       exit (1);
108299c6f0cSmrg     }
109299c6f0cSmrg   if (mpfr_cmp_ui (y, 1370) != 0)
110299c6f0cSmrg     {
111299c6f0cSmrg       printf ("Wrong output for sqr(37)\n");
112299c6f0cSmrg       exit (1);
113299c6f0cSmrg     }
114299c6f0cSmrg 
115299c6f0cSmrg   /* case where the computation on n+1 bits ends with a '1' but is inexact */
116299c6f0cSmrg   mpfr_set_ui (x, 91, MPFR_RNDN);
117299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_sqr, y, x);
118299c6f0cSmrg   if (inex <= 0)
119299c6f0cSmrg     {
120299c6f0cSmrg       printf ("Wrong ternary value for sqr(91)\n");
121299c6f0cSmrg       exit (1);
122299c6f0cSmrg     }
123299c6f0cSmrg   if (mpfr_cmp_ui (y, 8288) != 0)
124299c6f0cSmrg     {
125299c6f0cSmrg       printf ("Wrong output for sqr(91)\n");
126299c6f0cSmrg       exit (1);
127299c6f0cSmrg     }
128299c6f0cSmrg 
129299c6f0cSmrg   mpfr_set_ui (x, 131, MPFR_RNDN);
130299c6f0cSmrg   inex = mpfr_round_nearest_away (mpfr_sqr, y, x);
131299c6f0cSmrg   if (inex >= 0)
132299c6f0cSmrg     {
133299c6f0cSmrg       printf ("Wrong ternary value for sqr(131)\n");
134299c6f0cSmrg       exit (1);
135299c6f0cSmrg     }
136299c6f0cSmrg   if (mpfr_cmp_ui (y, 17152) != 0)
137299c6f0cSmrg     {
138299c6f0cSmrg       printf ("Wrong output for sqr(131)\n");
139299c6f0cSmrg       exit (1);
140299c6f0cSmrg     }
141299c6f0cSmrg 
142299c6f0cSmrg   mpfr_clear (x);
143299c6f0cSmrg   mpfr_clear (y);
144299c6f0cSmrg }
145299c6f0cSmrg 
146299c6f0cSmrg int
main(int argc,char * argv[])147299c6f0cSmrg main (int argc, char *argv[])
148299c6f0cSmrg {
149299c6f0cSmrg   mpfr_exp_t emin;
150299c6f0cSmrg 
151299c6f0cSmrg   tests_start_mpfr ();
152299c6f0cSmrg 
153299c6f0cSmrg   /* mpfr_round_nearest_away requires emin is not the smallest possible */
154299c6f0cSmrg   if ((emin = mpfr_get_emin ()) == mpfr_get_emin_min ())
155*ba125506Smrg     set_emin (mpfr_get_emin_min () + 1);
156299c6f0cSmrg 
157299c6f0cSmrg   test_special ();
158299c6f0cSmrg 
159299c6f0cSmrg   test_nonspecial ();
160299c6f0cSmrg 
161*ba125506Smrg   set_emin (emin);
162299c6f0cSmrg 
163299c6f0cSmrg   tests_end_mpfr ();
164299c6f0cSmrg   return 0;
165299c6f0cSmrg }
166299c6f0cSmrg 
167