-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.cpp
More file actions
184 lines (141 loc) · 3.25 KB
/
number.cpp
File metadata and controls
184 lines (141 loc) · 3.25 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "number.h"
using namespace BankOfEuler;
static FILE *urfp = NULL;
Number Number::v = 0;
void Number::urandomb(unsigned int bits) {
if (!urfp)
assert(urfp = fopen("/dev/urandom", "r"));
unsigned int bytes = (bits + 7) >> 3;
assert(bytes);
unsigned char buf[bytes];
size_t bytes2 = fread(buf, 1, bytes, urfp);
assert(bytes == bytes2);
mpz_import(get_mpz_t(), bytes, 1, 1, 0, 0, buf);
if (unsigned int trim = bytes * 8 - bits) {
*this >>= trim;
}
}
void Number::urandomm(const Number &m) {
// 128 extra bits to hide bias
unsigned int bits = 128 + m.sizeinbase(2);
urandomb(bits);
*this %= m;
}
void Number::set_str(const char *p) {
bool is_file = false;
FILE *fp;
if (!strncmp(p, "file:", 5)) {
p += 5;
assert(fp = fopen(p, "r"));
is_file = true;
} else if (!isdigit(*p) && *p != '$' && *p != '-') {
fp = fopen(p, "r");
if (fp)
is_file = true;
}
if (is_file) {
if (getc(fp) == 0) {
fseek(fp, 0, 0);
read(fp);
fclose(fp);
return;
}
assert(0 == fseek(fp, 0, 2));
unsigned int bufn = ftell(fp);
assert(0 == fseek(fp, 0, 0));
char buf[bufn + 1];
assert(bufn == fread(buf, 1, bufn, fp) || !"can't read file");
buf[bufn] = 0;
if (char *p = strchr(buf, '\n'))
*p = 0;
fclose(fp);
_set_str_nf(buf);
return;
}
_set_str_nf(p);
}
void Number::_set_str_nf(const char *p) {
if (p[0] == '$') {
mpf_class f(p + 1, 1024);
Number big;
big.pow(10, 128);
f *= big;
Number z = f;
z *= Number::v;
z /= big;
*this = z;
#if 0
double f = exp2(64);
double d = f * strtod(p + 1, NULL);
*this = (d * v) / f;
#endif
return;
}
if (p[0] == '-' || p[0] >= '0' && p[0] <= '9') { // dec, hex, oct
assert(0 == mpz_set_str(get_mpz_t(), p, 0));
// *(mpz_class *)this = p;
return;
}
assert(!"can't understand number");
}
void NumberVector::set_str(const char *p) {
clear();
if (!strncmp(p, "file:", 5)) {
FILE *fp;
assert(fp = fopen(p + 5, "r"));
if (getc(fp) == 0) {
fseek(fp, 0, 0);
read(fp);
fclose(fp);
return;
}
assert(0 == fseek(fp, 0, 2));
unsigned int bufn = ftell(fp);
assert(0 == fseek(fp, 0, 0));
char buf[bufn + 1];
assert(bufn == fread(buf, 1, sizeof(buf), fp) || !"can't read file");
buf[bufn] = 0;
fclose(fp);
_set_str_nf(buf);
return;
}
if (*p == '(')
++p;
_set_str_nf(p);
}
void NumberVector::_set_str_nf(const char *p) {
const char *q;
do {
while (*p && isspace(*p))
++p;
if (!*p)
break;
q = p;
while (*q && !isspace(*q) && *q != ')')
++q;
char buf[1 + q - p];
memcpy(buf, p, q - p);
buf[q - p] = 0;
push_back(0);
back().set_str(buf);
p = q + 1;
} while (*q);
}
namespace BankOfEuler {
void write_int32(unsigned int x, FILE *fp) {
unsigned int nx = htonl(x);
assert('\1' == fputc('\1', fp));
assert(1 == fwrite(&nx, sizeof(nx), 1, fp));
assert('\n' == fputc('\n', fp));
}
unsigned int read_int32(FILE *fp) {
unsigned int nx;
assert('\1' == fgetc(fp));
assert(1 == fread(&nx, sizeof(nx), 1, fp));
assert('\n' == fgetc(fp));
return ntohl(nx);
}
}