xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/ttrunc.c (revision c34236556bea94afcaca1782d7d228301edc3ea0)
1 /* Test file for mpfr_trunc, mpfr_ceil, mpfr_floor.
2 
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 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 #define SIZEX 100
29 
30 int
31 main (void)
32 {
33   int j, k;
34   mpfr_t x, y, z, t, y2, z2, t2;
35 
36   tests_start_mpfr ();
37 
38   mpfr_inits2 (SIZEX, x, y, z, t, y2, z2, t2, (mpfr_ptr) 0);
39 
40   mpfr_set_str1 (x, "0.5");
41   mpfr_ceil(y, x);
42   if (mpfr_cmp_ui (y, 1))
43     {
44       printf ("Error in mpfr_ceil for x=0.5: expected 1.0, got ");
45       mpfr_print_binary(y);
46       putchar('\n');
47       exit (1);
48     }
49 
50   mpfr_set_ui (x, 0, MPFR_RNDN);
51   mpfr_ceil(y, x);
52   if (mpfr_cmp_ui(y,0))
53     {
54       printf ("Error in mpfr_ceil for x=0.0: expected 0.0, got ");
55       mpfr_print_binary(y);
56       putchar('\n');
57       exit (1);
58     }
59 
60   mpfr_set_ui (x, 1, MPFR_RNDN);
61   mpfr_ceil(y, x);
62   if (mpfr_cmp_ui(y,1))
63     {
64       printf ("Error in mpfr_ceil for x=1.0: expected 1.0, got ");
65       mpfr_print_binary(y);
66       putchar('\n');
67       exit (1);
68     }
69 
70   for (j=0;j<1000;j++)
71     {
72       mpfr_urandomb (x, RANDS);
73       MPFR_EXP (x) = 2;
74 
75       for (k = 2; k <= SIZEX; k++)
76         {
77           mpfr_set_prec(y, k);
78           mpfr_set_prec(y2, k);
79           mpfr_set_prec(z, k);
80           mpfr_set_prec(z2, k);
81           mpfr_set_prec(t, k);
82           mpfr_set_prec(t2, k);
83 
84           mpfr_floor(y, x);
85           mpfr_set(y2, x, MPFR_RNDD);
86 
87           mpfr_trunc(z, x);
88           mpfr_set(z2, x, MPFR_RNDZ);
89 
90           mpfr_ceil(t, x);
91           mpfr_set(t2, x, MPFR_RNDU);
92 
93           if (!mpfr_eq(y, y2, k))
94             {
95               printf("Error in floor, x = "); mpfr_print_binary(x);
96               printf("\n");
97               printf("floor(x) = "); mpfr_print_binary(y);
98               printf("\n");
99               printf("round(x, RNDD) = "); mpfr_print_binary(y2);
100               printf("\n");
101               exit(1);
102             }
103 
104           if (!mpfr_eq(z, z2, k))
105             {
106               printf("Error in trunc, x = "); mpfr_print_binary(x);
107               printf("\n");
108               printf("trunc(x) = "); mpfr_print_binary(z);
109               printf("\n");
110               printf("round(x, RNDZ) = "); mpfr_print_binary(z2);
111               printf("\n");
112               exit(1);
113             }
114 
115           if (!mpfr_eq(y, y2, k))
116             {
117               printf("Error in ceil, x = "); mpfr_print_binary(x);
118               printf("\n");
119               printf("ceil(x) = "); mpfr_print_binary(t);
120               printf("\n");
121               printf("round(x, RNDU) = "); mpfr_print_binary(t2);
122               printf("\n");
123               exit(1);
124             }
125           MPFR_EXP(x)++;
126         }
127     }
128 
129   mpfr_clears (x, y, z, t, y2, z2, t2, (mpfr_ptr) 0);
130 
131   tests_end_mpfr ();
132   return 0;
133 }
134