decimal 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/inum192.h DELETED
@@ -1,332 +0,0 @@
1
- /*
2
- * Ruby's Integer part from ruby_1_9_2, r28085.
3
- *
4
- * These are hand copies (with few modifications) taken from original
5
- * Ruby's code in "numeric.c" and "bignum.c," so the copyrights are
6
- * held by matz and other contributors:
7
- *
8
- * Copyright (C) 1993-2010 Yukihiro Matsumoto
9
- *
10
- */
11
-
12
- /*
13
- * copied from bignum.c
14
- */
15
-
16
- #define BDIGITS(x) (RBIGNUM_DIGITS(x))
17
-
18
- #ifndef HAVE_RB_BIGZERO_P
19
- #error Ruby 1.9.2 should have rb_bigzero_p()!
20
- #endif
21
-
22
- static VALUE
23
- rb_big_uminus(VALUE x)
24
- {
25
- VALUE z = rb_big_clone(x);
26
-
27
- RBIGNUM_SET_SIGN(z, !RBIGNUM_SIGN(x));
28
-
29
- return rb_big_norm(z); /* modified to use exported one */
30
- }
31
-
32
- static VALUE
33
- rb_big_hash(VALUE x)
34
- {
35
- st_index_t hash;
36
-
37
- hash = rb_memhash(BDIGITS(x), sizeof(BDIGIT)*RBIGNUM_LEN(x)) ^ RBIGNUM_SIGN(x);
38
- return INT2FIX(hash);
39
- }
40
-
41
- static VALUE
42
- rb_big_odd_p(VALUE num)
43
- {
44
- if (BDIGITS(num)[0] & 1) {
45
- return Qtrue;
46
- }
47
- return Qfalse;
48
- }
49
-
50
- /*
51
- * copied from numeric.c
52
- */
53
-
54
- static VALUE
55
- flo_to_s(VALUE flt)
56
- {
57
- char buf[32];
58
- double value = RFLOAT_VALUE(flt);
59
- char *p, *e;
60
-
61
- if (isinf(value))
62
- return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
63
- else if(isnan(value))
64
- return rb_usascii_str_new2("NaN");
65
-
66
- snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
67
- if (!(e = strchr(buf, 'e'))) {
68
- e = buf + strlen(buf);
69
- }
70
- if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
71
- snprintf(buf, sizeof(buf), "%#.14e", value);
72
- if (!(e = strchr(buf, 'e'))) {
73
- e = buf + strlen(buf);
74
- }
75
- }
76
- p = e;
77
- while (p[-1]=='0' && ISDIGIT(p[-2]))
78
- p--;
79
- memmove(p, e, strlen(e)+1);
80
- return rb_usascii_str_new2(buf);
81
- }
82
-
83
- static VALUE
84
- fix_plus(VALUE x, VALUE y)
85
- {
86
- if (FIXNUM_P(y)) {
87
- long a, b, c;
88
- VALUE r;
89
-
90
- a = FIX2LONG(x);
91
- b = FIX2LONG(y);
92
- c = a + b;
93
- r = LONG2NUM(c);
94
-
95
- return r;
96
- }
97
- return rb_big_plus(y, x); /* modified */
98
- }
99
-
100
- static VALUE
101
- fix_minus(VALUE x, VALUE y)
102
- {
103
- if (FIXNUM_P(y)) {
104
- long a, b, c;
105
- VALUE r;
106
-
107
- a = FIX2LONG(x);
108
- b = FIX2LONG(y);
109
- c = a - b;
110
- r = LONG2NUM(c);
111
-
112
- return r;
113
- }
114
- /* modified */
115
- x = rb_int2big(FIX2LONG(x));
116
- return rb_big_minus(x, y);
117
- }
118
-
119
- #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
120
- /*tests if N*N would overflow*/
121
- #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
122
-
123
- static VALUE
124
- fix_mul(VALUE x, VALUE y)
125
- {
126
- if (FIXNUM_P(y)) {
127
- #ifdef __HP_cc
128
- /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
129
- volatile
130
- #endif
131
- long a, b;
132
- #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
133
- LONG_LONG d;
134
- #else
135
- long c;
136
- VALUE r;
137
- #endif
138
-
139
- a = FIX2LONG(x);
140
- b = FIX2LONG(y);
141
-
142
- #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
143
- d = (LONG_LONG)a * b;
144
- if (FIXABLE(d)) return LONG2FIX(d);
145
- return rb_ll2inum(d);
146
- #else
147
- if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b))
148
- return LONG2FIX(a*b);
149
- c = a * b;
150
- r = LONG2FIX(c);
151
-
152
- if (a == 0) return x;
153
- if (FIX2LONG(r) != c || c/a != b) {
154
- r = rb_big_mul(rb_int2big(a), rb_int2big(b));
155
- }
156
- return r;
157
- #endif
158
- }
159
- /* modified */
160
- return rb_big_mul(y, x);
161
- }
162
-
163
- static void
164
- fixdivmod(long x, long y, long *divp, long *modp)
165
- {
166
- long div, mod;
167
-
168
- if (y == 0) rb_bug("fixdivmod(): not reached"); /* modified */
169
- if (y < 0) {
170
- if (x < 0)
171
- div = -x / -y;
172
- else
173
- div = - (x / -y);
174
- }
175
- else {
176
- if (x < 0)
177
- div = - (-x / y);
178
- else
179
- div = x / y;
180
- }
181
- mod = x - div*y;
182
- if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
183
- mod += y;
184
- div -= 1;
185
- }
186
- if (divp) *divp = div;
187
- if (modp) *modp = mod;
188
- }
189
-
190
- /* extracted from fix_divide() */
191
- static VALUE
192
- fix_div(VALUE x, VALUE y)
193
- {
194
- if (FIXNUM_P(y)) {
195
- long div;
196
-
197
- fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
198
- return LONG2NUM(div);
199
- }
200
- /* modified */
201
- x = rb_int2big(FIX2LONG(x));
202
- return rb_big_div(x, y);
203
- }
204
-
205
- static VALUE
206
- fix_divmod(VALUE x, VALUE y)
207
- {
208
- if (FIXNUM_P(y)) {
209
- long div, mod;
210
-
211
- fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
212
-
213
- return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
214
- }
215
- /* modified */
216
- x = rb_int2big(FIX2LONG(x));
217
- return rb_big_divmod(x, y);
218
- }
219
-
220
- static VALUE
221
- int_pow(long x, unsigned long y)
222
- {
223
- int neg = x < 0;
224
- long z = 1;
225
-
226
- if (neg) x = -x;
227
- if (y & 1)
228
- z = x;
229
- else
230
- neg = 0;
231
- y &= ~1;
232
- do {
233
- while (y % 2 == 0) {
234
- if (!FIT_SQRT_LONG(x)) {
235
- VALUE v;
236
- bignum:
237
- v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
238
- if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
239
- return v;
240
- }
241
- x = x * x;
242
- y >>= 1;
243
- }
244
- {
245
- long xz = x * z;
246
- if (!POSFIXABLE(xz) || xz / x != z) {
247
- goto bignum;
248
- }
249
- z = xz;
250
- }
251
- } while (--y);
252
- if (neg) z = -z;
253
- return LONG2NUM(z);
254
- }
255
-
256
- static VALUE fix_odd_p(VALUE num);
257
-
258
- static VALUE
259
- fix_pow(VALUE x, VALUE y)
260
- {
261
- /* static const double zero = 0.0; */
262
- long a = FIX2LONG(x);
263
-
264
- if (FIXNUM_P(y)) {
265
- long b = FIX2LONG(y);
266
-
267
- if (b < 0)
268
- return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
269
-
270
- if (b == 0) return INT2FIX(1);
271
- if (b == 1) return x;
272
- if (a == 0) {
273
- if (b > 0) return INT2FIX(0);
274
- /* modified */
275
- rb_bug("fix_pow(): infinity returned");
276
- return Qnil;
277
- }
278
- if (a == 1) return INT2FIX(1);
279
- if (a == -1) {
280
- if (b % 2 == 0)
281
- return INT2FIX(1);
282
- else
283
- return INT2FIX(-1);
284
- }
285
- return int_pow(a, b);
286
- }
287
- /* modified */
288
- if (rb_funcall(y, '<', 1, INT2FIX(0)))
289
- return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
290
-
291
- if (a == 0) return INT2FIX(0);
292
- if (a == 1) return INT2FIX(1);
293
- if (a == -1) {
294
- /* modified */
295
- #define int_even_p(x) \
296
- (FIXNUM_P(x) ? !fix_odd_p(x) : !rb_big_odd_p(x))
297
- if (int_even_p(y)) return INT2FIX(1);
298
- #undef int_even_p
299
- else return INT2FIX(-1);
300
- }
301
- x = rb_int2big(FIX2LONG(x));
302
- return rb_big_pow(x, y);
303
- }
304
-
305
- static VALUE
306
- fix_equal(VALUE x, VALUE y)
307
- {
308
- if (x == y) return Qtrue;
309
- if (FIXNUM_P(y)) return Qfalse;
310
- return rb_big_eq(y, x); /* modified */
311
- }
312
-
313
- static VALUE
314
- fix_cmp(VALUE x, VALUE y)
315
- {
316
- if (x == y) return INT2FIX(0);
317
- if (FIXNUM_P(y)) {
318
- if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
319
- return INT2FIX(-1);
320
- }
321
- /* modified */
322
- return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
323
- }
324
-
325
- static VALUE
326
- fix_odd_p(VALUE num)
327
- {
328
- if (num & 2) {
329
- return Qtrue;
330
- }
331
- return Qfalse;
332
- }