number 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/ext/complex.c +49 -0
  2. data/ext/number.c +34 -1
  3. data/ext/real.c +3 -3
  4. metadata +2 -2
data/ext/complex.c CHANGED
@@ -1,5 +1,7 @@
1
1
  #include "number.h"
2
2
 
3
+ mpz_t tmp_complex_print_int;
4
+
3
5
  mpfr_t tmp_complex_from_float_str,
4
6
  tmp_divide_re,
5
7
  tmp_divide_im,
@@ -32,6 +34,7 @@ mpc_t tmp_divide_numerator,
32
34
 
33
35
  void init_complex_tmp_nums ()
34
36
  {
37
+ mpz_init(tmp_complex_print_int);
35
38
  mpfr_init2(tmp_complex_from_float_str, PREC);
36
39
  mpfr_init2(tmp_divide_re, PREC);
37
40
  mpfr_init2(tmp_divide_im, PREC);
@@ -179,6 +182,52 @@ void complex_print (char* buf, Complex* complex)
179
182
  }
180
183
  }
181
184
 
185
+ /*
186
+ * Puts a string representation of real as a truncated integer into the string
187
+ * pointed to by buf.
188
+ */
189
+ void complex_print_int (char* buf, Complex* complex)
190
+ {
191
+ Real* rounded;
192
+ rounded = (Real*)real_round_origin(complex->re->n);
193
+
194
+ if (!REAL(rounded))
195
+ {
196
+ sprintf(buf, "%s", NaN(complex->re->n) ? "NaN" : POS_INF(complex->re->n) ? "Infinity" : "-Infinity");
197
+ }
198
+ else if (ZERO(rounded))
199
+ {
200
+ sprintf(buf, "0");
201
+ }
202
+ else
203
+ {
204
+ mpz_set(tmp_complex_print_int, rounded->num);
205
+ num_pad(tmp_complex_print_int, rounded->exp - num_len(tmp_complex_print_int));
206
+ mpz_get_str(buf, 10, tmp_complex_print_int);
207
+ }
208
+
209
+ real_free(rounded);
210
+ }
211
+
212
+ /*
213
+ * Puts a string representation of real as a fractional decimal into the string
214
+ * pointed to by buf.
215
+ */
216
+ void complex_print_decimal (char* buf, Complex* complex)
217
+ {
218
+ char real_buf[10000];
219
+
220
+ if (REAL(complex->re->n))
221
+ {
222
+ gmp_sprintf(real_buf, "%+Zd", complex->re->n->num);
223
+ sprintf(buf, "%c0.%se%ld", *real_buf, real_buf + 1, complex->re->n->exp);
224
+ }
225
+ else
226
+ {
227
+ sprintf(buf, "%s", NaN(complex->re->n) ? "NaN" : POS_INF(complex->re->n) ? "Infinity" : "-Infinity");
228
+ }
229
+ }
230
+
182
231
  /*
183
232
  * Returns a pointer to a newly allocated Complex with the given value. The
184
233
  * given intervals will be referenced by the new complex and must be freed only
data/ext/number.c CHANGED
@@ -250,10 +250,41 @@ VALUE rb_Number_initialize_copy (VALUE number, VALUE copy)
250
250
  return Qnil; /* not reached */
251
251
  }
252
252
 
253
+ VALUE rb_Number_to_f (VALUE number)
254
+ {
255
+ if (complex_re_normal_p(DATA_PTR(number)))
256
+ {
257
+ char buf[10000];
258
+
259
+ complex_print_decimal(buf, DATA_PTR(number));
260
+
261
+ return DBL2NUM(rb_cstr_to_dbl(buf, 0));
262
+ }
263
+ else
264
+ {
265
+ return complex_re_nan_p(DATA_PTR(number)) ? DBL2NUM(NAN) : complex_re_pos_inf_p(DATA_PTR(number)) ? DBL2NUM(INFINITY) : rb_funcall(DBL2NUM(INFINITY), rb_intern("-@"), 0);
266
+ }
267
+ }
268
+
269
+ VALUE rb_Number_to_i (VALUE number)
270
+ {
271
+ if (complex_re_normal_p(DATA_PTR(number)))
272
+ {
273
+ char buf[10000];
274
+
275
+ complex_print_int(buf, DATA_PTR(number));
276
+
277
+ return rb_cstr_to_inum(buf, 10, false);
278
+ }
279
+ else
280
+ {
281
+ return complex_re_nan_p(DATA_PTR(number)) ? DBL2NUM(NAN) : complex_re_pos_inf_p(DATA_PTR(number)) ? DBL2NUM(INFINITY) : rb_funcall(DBL2NUM(INFINITY), rb_intern("-@"), 0);
282
+ }
283
+ }
284
+
253
285
  VALUE rb_Number_to_s (VALUE number)
254
286
  {
255
287
  char buf[1000];
256
- VALUE str;
257
288
 
258
289
  complex_print(buf, DATA_PTR(number));
259
290
 
@@ -1381,6 +1412,8 @@ void Init_number ()
1381
1412
  rb_define_method(rb_cNumber, "singleton_method_added", rb_Number_singleton_method_added, 1);
1382
1413
  rb_define_method(rb_cNumber, "initialize_copy", rb_Number_initialize_copy, 1);
1383
1414
 
1415
+ rb_define_method(rb_cNumber, "to_f", rb_Number_to_f, 0);
1416
+ rb_define_method(rb_cNumber, "to_i", rb_Number_to_i, 0);
1384
1417
  rb_define_method(rb_cNumber, "to_s", rb_Number_to_s, 0);
1385
1418
  rb_define_method(rb_cNumber, "to_number", rb_Number_dummy, 0);
1386
1419
  rb_define_method(rb_cNumber, "coerce", rb_Number_coerce, 1);
data/ext/real.c CHANGED
@@ -57,7 +57,7 @@ void init_real_tmp_nums ()
57
57
  *
58
58
  * num: (1234), pow: 2 => num: (123400)
59
59
  */
60
- static void num_pad (mpz_t num, int pow)
60
+ void num_pad (mpz_t num, int pow)
61
61
  {
62
62
  mpz_ui_pow_ui(tmp_num_pad, 10, pow);
63
63
  mpz_mul(num, num, tmp_num_pad);
@@ -69,7 +69,7 @@ static void num_pad (mpz_t num, int pow)
69
69
  *
70
70
  * num: (12345), overrun: 3, round_mode: ROUND_UP => num: (13)
71
71
  */
72
- static void num_round (mpz_t num, int overrun, int round_mode)
72
+ void num_round (mpz_t num, int overrun, int round_mode)
73
73
  {
74
74
  if (!overrun)
75
75
  {
@@ -109,7 +109,7 @@ static void num_round (mpz_t num, int overrun, int round_mode)
109
109
  * num: (5600) => long: 4
110
110
  * num: (-123) => long: 3
111
111
  */
112
- static long num_len (mpz_t num)
112
+ long num_len (mpz_t num)
113
113
  {
114
114
  char buf[mpz_sizeinbase(num, 10) + 2];
115
115
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: number
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.5
5
+ version: 0.9.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesse Sielaff
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-07-17 00:00:00 Z
15
+ date: 2011-07-18 00:00:00 Z
16
16
  dependencies: []
17
17
 
18
18
  description: The Number gem is intended to be a drop-in replacement for Ruby's Numeric classes when arbitrary-precision complex interval calculations are warranted. The basis of the arbitrary-precision calculations is the GNU MP, MPFR, and MPC libraries.