xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tsgn.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* tsgn -- Test for the sign of a floating point number.
2 
3 Copyright 2003, 2006-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 #define MPFR_TESTS_TSGN 1
24 #include "mpfr-test.h"
25 
26 static void
mpfr_sgn_test(mpfr_srcptr x,int res)27 mpfr_sgn_test (mpfr_srcptr x, int res)
28 {
29   mpfr_exp_t old_emin, old_emax, e;
30   mpfr_flags_t flags;
31   int i, v;
32 
33   old_emin = mpfr_get_emin ();
34   old_emax = mpfr_get_emax ();
35 
36   for (i = 0; i <= 1; i++)
37     {
38       if (i != 0)
39         {
40           e = MPFR_IS_SINGULAR (x) ? MPFR_EMIN_MIN : mpfr_get_exp (x);
41           set_emin (e);
42           set_emax (e);
43         }
44 
45       for (flags = 0; flags <= MPFR_FLAGS_ALL; flags++)
46         {
47           __gmpfr_flags = flags;
48           v = (mpfr_sgn) (x);
49           MPFR_ASSERTN (__gmpfr_flags == flags);
50           if (VSIGN (v) != res)
51             {
52               printf ("mpfr_sgn function error (got %d) for ", v);
53               mpfr_dump (x);
54               exit (1);
55             }
56           v = mpfr_sgn (x);
57           MPFR_ASSERTN (__gmpfr_flags == flags);
58           if (VSIGN (v) != res)
59             {
60               printf ("mpfr_sgn macro error (got %d) for ", v);
61               mpfr_dump (x);
62               exit (1);
63             }
64         }
65     }
66 
67   set_emin (old_emin);
68   set_emax (old_emax);
69 }
70 
71 static void
check_special(void)72 check_special (void)
73 {
74   mpfr_t x;
75   int i;
76 
77   mpfr_init (x);
78 
79   for (i = 1; i >= -1; i -= 2)
80     {
81       mpfr_set_zero (x, i);
82       mpfr_sgn_test (x, 0);
83       mpfr_set_inf (x, i);
84       mpfr_sgn_test (x, i);
85       mpfr_set_si (x, i, MPFR_RNDN);
86       mpfr_sgn_test (x, i);
87     }
88 
89   MPFR_SET_NAN (x);
90 
91   mpfr_clear_flags ();
92   if ((mpfr_sgn) (x) != 0 || !mpfr_erangeflag_p ())
93     {
94       printf ("Sgn error for NaN.\n");
95       exit (1);
96     }
97 
98   mpfr_clear_flags ();
99   if (mpfr_sgn (x) != 0 || !mpfr_erangeflag_p ())
100     {
101       printf ("Sgn error for NaN.\n");
102       exit (1);
103     }
104 
105   mpfr_clear (x);
106 }
107 
108 static void
check_sgn(void)109 check_sgn(void)
110 {
111   mpfr_t x;
112   int i, s1, s2;
113 
114   mpfr_init(x);
115   for(i = 0 ; i < 100 ; i++)
116     {
117       mpfr_urandomb (x, RANDS);
118       if (i&1)
119         {
120           MPFR_SET_POS(x);
121           s2 = 1;
122         }
123       else
124         {
125           MPFR_SET_NEG(x);
126           s2 = -1;
127         }
128       s1 = mpfr_sgn(x);
129       if (s1 < -1 || s1 > 1)
130         {
131           printf("Error for sgn: out of range.\n");
132           goto lexit;
133         }
134       else if (MPFR_IS_NAN(x) || MPFR_IS_ZERO(x))
135         {
136           if (s1 != 0)
137             {
138               printf("Error for sgn: Nan or Zero should return 0.\n");
139               goto lexit;
140             }
141         }
142       else if (s1 != s2)
143         {
144           printf("Error for sgn. Return %d instead of %d.\n", s1, s2);
145           goto lexit;
146         }
147     }
148   mpfr_clear(x);
149   return;
150 
151  lexit:
152   mpfr_clear(x);
153   exit(1);
154 }
155 
156 int
main(int argc,char * argv[])157 main (int argc, char *argv[])
158 {
159   tests_start_mpfr ();
160 
161   check_special ();
162   check_sgn ();
163 
164   tests_end_mpfr ();
165   return 0;
166 }
167