aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/base/force_R.c
blob: c67c0f39e21e1f1a191fa0c73aeb2ca5b5547d78 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2000, Imperial College
 *
 * This file is part of the Imperial College Exact Real Arithmetic Library.
 * See the copyright notice included in the distribution for conditions
 * of use.
 */

#include <stdio.h>
#include "real.h"
#include "real-impl.h"
#include <math.h>

/*
 * This is the toplevel function called by the user to force information
 * from a real stream.
 */
void
force_R_Digs(Real x, int digitsNeeded)
{
	DigsX *digsX;
	void runStack();
	void force_To_Alt_Entry();

	if (x->gen.tag.type == ALT) {
		if (x->alt.redirect == NULL) {
			PUSH_2(force_To_Alt_Entry, x);
			runStack();
		}
		force_R_Digs(x->alt.redirect, digitsNeeded);
		return;
	}

	if (x->gen.tag.type == CLOSURE) {
		if (x->cls.redirect == NULL) {
			PUSH_2(x->cls.force, x);
			runStack();
		}
		force_R_Digs(x->cls.redirect, digitsNeeded);
		return;
	}

	x = makeStream(x);

	/*
	 * So now we know that x is a real stream. If x is signed and
	 * the sign has not been determined, we force it.
	 */
	if (x->gen.tag.type == SIGNX) {
		if (x->signX.tag.value == SIGN_UNKN) {
			PUSH_2(x->signX.force, x);
			runStack();
		}
		digsX = (DigsX *) x->signX.x;
	}
	else
		digsX = (DigsX *) x;

	if (digsX->tag.type != DIGSX) 
		Error(FATAL, E_INT, "force_R_Digs", "badly formed real");

	/*
	 * Now, if there is not enough information available, we force
	 * the number of digits needed.
	 */
	if (digsX->count < (unsigned int)digitsNeeded) {
		PUSH_3(digsX->force, digsX, digitsNeeded - digsX->count);
		runStack();
	}
}

/* force_R_Dec
 * July 2000, Marko Krznaric
 *
 * force_R_Digs(x,digitsNeeded) is a function which forces an
 * emission of digitsNeeded digit matrices from x (of the
 * type Real).
 *
 * On the other hand, force_R_Dec(x,decimalPrecision) guarantees
 * that enough digit matrices, say digitsNeeded, will be emitted
 * from a real number x in order to have required absolute
 * decimal precision, i.e. it guarantees that the result will be
 * accurate within 10^(-decimalPrecision).
 *
 * NOTES:
 * - initGuess = an initial guess (minimum number) of digit
 *   matrices which has to be emitted from a real number x.
 * - digitsNeeded = number of digit matrices required.
 * - e = number of 'bad' digits.
 * - using function retrieveInfo, we can extract the sign (=sign),
 *   the number of digits emitted so far (=count) and the
 *   compressed digits (=digits) for a Real x.
 * - 3.322 = an upper bound for log2(10).
 *
 *
 * PROBLEMS:
 * - should decimalPrecision & digitsNeeded be of type long int?
 *
 */
void
force_R_Dec(Real x, int decimalPrecision)
{
	int initGuess = ceil(3.322 * decimalPrecision) + 2;
	int digitsNeeded;
	int e = 0;

	mpz_t digits;
	Sign sign;
	int count;

	mpz_init(digits);

	digitsNeeded = initGuess;
	force_R_Digs(x, digitsNeeded);
	retrieveInfo(x, &sign, &count, digits);
	
	switch (sign) {
	case SZERO:
		/*
		 * SZERO: every digit matrix will half the interval
		 * (starting with [-1,1]). Easy to determine digitsNeeded.
		 */
		digitsNeeded = initGuess - 1;
		break;

	case SPOS:
	case SNEG:
		/*
		 * SPOS, SNEG: e, the number of 'bad' digits is actually
		 * the number of leading 1s in the binary representation
		 * of digits (the value of compressed digits).
		 */
		while (digitsNeeded < forceDecUpperBound) {
			force_R_Digs(x, digitsNeeded);
			retrieveInfo(x, &sign, &count, digits);
			if (sign == SNEG)
				mpz_neg(digits, digits);
			e = leadingOnes (digits);
			digitsNeeded = 2 * e + 2 + initGuess;
			if (count > e)
				/* not all of the extracted digit matrices are 'bad',
				 * i.e. the interval is bounded. Therefore, we can
				 * leave the loop.
				 */
				break;
		}
		if (digitsNeeded >= forceDecUpperBound)
			Error(FATAL, E_INT, "force_R_Dec",
						"forceDecUpperBound reached (1)");
		break;

	case SINF:
		/* SINF: we are still dealing with the unbounded interval,
		 * i.e. the interval contains infinity, as far as digits
		 * (the value of compressed digits matrices) is either
		 * -1, 0 or 1. As soon as we get something greater (in
		 * absolute value) than 1, the interval doesn't contain
		 * the infinity, and we can calculate digitsNeeded.
		 */
		while (digitsNeeded < forceDecUpperBound) {
			force_R_Digs(x, digitsNeeded);
			retrieveInfo(x, &sign, &count, digits);
			if (mpz_cmpabs_ui(digits, 1) > 0) {
				e = count - mpz_sizeinbase(digits, 2);
				digitsNeeded = 2 * e + 2 + initGuess;
				break;
			}
			else {
				e = count;
				digitsNeeded = 2 * e + 2+ initGuess;
			}
		}
		if (digitsNeeded >= forceDecUpperBound)
			Error(FATAL, E_INT, "force_R_Dec",
						"forceDecUpperBound reached (2)");
		break;

	case SIGN_UNKN:
		Error(FATAL, E_INT, "force_R_Dec", "argument is signed");
		break;
	
	default:
		Error(FATAL, E_INT, "force_R_Dec", "bad sign");
		break;
	}

	force_R_Digs(x, digitsNeeded);
	mpz_clear(digits);
}

/* leadingOnes
 * July 2000, Marko Krznaric
 *
 * For input variable c (of type mpz_t) check how many 1s
 * are leading the binary representation of c.
 *
 * We use this function as an auxiliary function to
 * force_R_Dec. We have to check how many D+ (D-) digit
 * matrices follow S+ (S-). This seems to be the fastest
 * way to do it.
 *
 * If c is zero or negative - result = 0.
 * If there are no zeros at all - result = binary size of c.
 * Otherwise, we scan from left-most digit and count until
 *   we reach 0.
 *
 * NOTES:
 * - The index of the right-most digit is 0, while the index
 *   of the left-most digit is (size-1).
 */ 
int
leadingOnes(mpz_t c)
{
	int size = mpz_sizeinbase(c,2);
	int i;
	int count = 0;

	if (mpz_sgn(c) <= 0)
		return 0;

	if (mpz_scan0(c, 0) == (long unsigned int)size)
		return size;

	for (i = size - 1; i >= 0; i--) {
	  if (mpz_scan1(c, i) == (long unsigned int)i)
	    count++;
	  else
	    return count;
	}
	return 0;
}