summaryrefslogtreecommitdiff
path: root/tests/ttrunc.c
blob: e932ba12afaa42b9f0405925d82afba5b2ba56ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Test file for mpfr_trunc, mpfr_ceil, mpfr_floor.

Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "mpfr.h"
#include "mpfr-impl.h"

#define SIZEX 100

int
main (void)
{
  int j, k; mpfr_t x, y, z, t, y2, z2, t2; 

  mpfr_init2(x, SIZEX); 
  mpfr_init2(y, SIZEX); 
  mpfr_init2(z, SIZEX); 
  mpfr_init2(t, SIZEX); 
  mpfr_init2(y2, SIZEX); 
  mpfr_init2(z2, SIZEX); 
  mpfr_init2(t2, SIZEX);

  mpfr_set_d(x, 0.5, GMP_RNDN);
  mpfr_ceil(y, x);
  if (mpfr_get_d1 (y) != 1.0) {
    fprintf(stderr, "Error in mpfr_ceil for x=0.5: expected 1.0, got %f\n",
	    mpfr_get_d1 (y)); exit(1); 
  }

  mpfr_set_d(x, 0.0, GMP_RNDN);
  mpfr_ceil(y, x);
  if (mpfr_get_d1 (y) != 0.0) {
    fprintf(stderr, "Error in mpfr_ceil for x=0.0: expected 0.0, got %f\n",
	    mpfr_get_d1 (y)); exit(1); 
  }

  mpfr_set_d(x, 1.0, GMP_RNDN);
  mpfr_ceil(y, x);
  if (mpfr_get_d1 (y) != 1.0) {
    fprintf(stderr, "Error in mpfr_ceil for x=1.0: expected 1.0, got %f\n",
	    mpfr_get_d1 (y)); exit(1); 
  }

  for (j=0;j<1000;j++) {

    mpfr_random(x); 
    MPFR_EXP(x) = 2;
        
    for (k = 2; k <= SIZEX; k++) 
      {
	mpfr_set_prec(y, k); 
	mpfr_set_prec(y2, k); 
	mpfr_set_prec(z, k); 
	mpfr_set_prec(z2, k); 
	mpfr_set_prec(t, k); 
	mpfr_set_prec(t2, k);  

	mpfr_floor(y, x); 
	mpfr_set(y2, x, GMP_RNDD); 

	mpfr_trunc(z, x); 
	mpfr_set(z2, x, GMP_RNDZ); 

	mpfr_ceil(t, x); 
	mpfr_set(t2, x, GMP_RNDU); 

	if (!mpfr_eq(y, y2, k))
	  {
	    printf("Error in floor, x = "); mpfr_print_binary(x); printf("\n"); 
	    printf("floor(x) = "); mpfr_print_binary(y); printf("\n"); 
	    printf("round(x, RNDD) = "); mpfr_print_binary(y2); printf("\n"); 
	    mpfr_clear(x); 
	    mpfr_clear(y); 
	    mpfr_clear(y2); 
	    mpfr_clear(z); 
	    mpfr_clear(z2); 
	    mpfr_clear(t); 
	    mpfr_clear(t2); 
	    exit(-1); 
	  }

	if (!mpfr_eq(z, z2, k))
	  {
	    printf("Error in trunc, x = "); mpfr_print_binary(x); printf("\n"); 
	    printf("trunc(x) = "); mpfr_print_binary(z); printf("\n"); 
	    printf("round(x, RNDZ) = "); mpfr_print_binary(z2); printf("\n"); 
	    mpfr_clear(x); 
	    mpfr_clear(y); 
	    mpfr_clear(y2); 
	    mpfr_clear(z); 
	    mpfr_clear(z2); 
	    mpfr_clear(t); 
	    mpfr_clear(t2); 
	    exit(-1); 
	  }

	if (!mpfr_eq(y, y2, k))
	  {
	    printf("Error in ceil, x = "); mpfr_print_binary(x); printf("\n"); 
	    printf("ceil(x) = "); mpfr_print_binary(t); printf("\n"); 
	    printf("round(x, RNDU) = "); mpfr_print_binary(t2); printf("\n"); 
	    mpfr_clear(x); 
	    mpfr_clear(y); 
	    mpfr_clear(y2); 
	    mpfr_clear(z); 
	    mpfr_clear(z2); 
	    mpfr_clear(t); 
	    mpfr_clear(t2); 
	    exit(-1); 
	  }
	MPFR_EXP(x)++; 
      }
  }

  mpfr_clear(x); 
  mpfr_clear(y); 
  mpfr_clear(y2); 
  mpfr_clear(z); 
  mpfr_clear(z2); 
  mpfr_clear(t); 
  mpfr_clear(t2); 

  return 0;
}