xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tlog1p.c (revision 7e30e94394d0994ab9534f68a8f91665045c91ce)
1 /* Test file for mpfr_log1p.
2 
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 http://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 <stdio.h>
24 #include <stdlib.h>
25 
26 #include "mpfr-test.h"
27 
28 #ifdef CHECK_EXTERNAL
29 static int
30 test_log1p (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode)
31 {
32   int res;
33   int ok = rnd_mode == MPFR_RNDN && mpfr_number_p (b) && mpfr_get_prec (a)>=53;
34   if (ok)
35     {
36       mpfr_print_raw (b);
37     }
38   res = mpfr_log1p (a, b, rnd_mode);
39   if (ok)
40     {
41       printf (" ");
42       mpfr_print_raw (a);
43       printf ("\n");
44     }
45   return res;
46 }
47 #else
48 #define test_log1p mpfr_log1p
49 #endif
50 
51 #define TEST_FUNCTION test_log1p
52 #define TEST_RANDOM_EMAX 80
53 #include "tgeneric.c"
54 
55 static void
56 special (void)
57 {
58   mpfr_t x;
59   int inex;
60 
61   mpfr_init (x);
62 
63   mpfr_set_nan (x);
64   mpfr_clear_flags ();
65   inex = test_log1p (x, x, MPFR_RNDN);
66   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
67   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
68 
69   mpfr_set_inf (x, -1);
70   mpfr_clear_flags ();
71   inex = test_log1p (x, x, MPFR_RNDN);
72   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
73   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
74 
75   mpfr_set_inf (x, 1);
76   mpfr_clear_flags ();
77   inex = test_log1p (x, x, MPFR_RNDN);
78   MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0);
79   MPFR_ASSERTN (__gmpfr_flags == 0);
80 
81   mpfr_set_ui (x, 0, MPFR_RNDN);
82   mpfr_clear_flags ();
83   inex = test_log1p (x, x, MPFR_RNDN);
84   MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x) && inex == 0);
85   MPFR_ASSERTN (__gmpfr_flags == 0);
86   mpfr_neg (x, x, MPFR_RNDN);
87   mpfr_clear_flags ();
88   inex = test_log1p (x, x, MPFR_RNDN);
89   MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG (x) && inex == 0);
90   MPFR_ASSERTN (__gmpfr_flags == 0);
91 
92   mpfr_set_si (x, -1, MPFR_RNDN);
93   mpfr_clear_flags ();
94   inex = test_log1p (x, x, MPFR_RNDN);
95   MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
96   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
97 
98   mpfr_set_si (x, -2, MPFR_RNDN);
99   mpfr_clear_flags ();
100   inex = test_log1p (x, x, MPFR_RNDN);
101   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
102   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
103 
104   mpfr_clear (x);
105 }
106 
107 static void
108 other (void)
109 {
110   mpfr_t x, y;
111 
112   /* Bug reported by Guillaume Melquiond on 2006-08-14. */
113   mpfr_init2 (x, 53);
114   mpfr_set_str (x, "-1.5e4f72873ed9a@-100", 16, MPFR_RNDN);
115   mpfr_init2 (y, 57);
116   mpfr_log1p (y, x, MPFR_RNDU);
117   if (mpfr_cmp (x, y) != 0)
118     {
119       printf ("Error in tlog1p for x = ");
120       mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
121       printf (", rnd = MPFR_RNDU\nExpected ");
122       mpfr_out_str (stdout, 16, 15, x, MPFR_RNDN);
123       printf ("\nGot      ");
124       mpfr_out_str (stdout, 16, 15, y, MPFR_RNDN);
125       printf ("\n");
126       exit (1);
127     }
128 
129   mpfr_clear (y);
130   mpfr_clear (x);
131   return;
132 }
133 
134 int
135 main (int argc, char *argv[])
136 {
137   tests_start_mpfr ();
138 
139   special ();
140   other ();
141 
142   test_generic (2, 100, 50);
143 
144   data_check ("data/log1p", mpfr_log1p, "mpfr_log1p");
145   bad_cases (mpfr_log1p, mpfr_expm1, "mpfr_log1p", 256, -64, 40,
146              4, 128, 800, 40);
147 
148   tests_end_mpfr ();
149   return 0;
150 }
151