gmp 0.5.3 → 0.5.23

Sign up to get free protection for your applications and to get access to all the features.
data/ext/gmpq.c CHANGED
@@ -7,25 +7,9 @@
7
7
  *
8
8
  * GMP Multiple Precision Rational Number.
9
9
  *
10
- * Instances of this class can store variables of the type mpq_t. This class
11
- * also contains many methods that act as the functions for mpq_t variables,
12
- * as well as a few methods that attempt to make this library more Ruby-ish.
13
- *
14
- * The following list is just a simple checklist for me, really. A better
15
- * reference should be found in the rdocs.
16
- *
17
- * Ruby method C Extension function GMP function
18
- * to_d r_gmpq_to_d mpq_get_d
19
- * to_s r_gmpq_to_s ?
20
- * add r_gmpq_add mpq_add
21
- * sub r_gmpq_sub mpq_sub
22
- * mul r_gmpq_mul mpq_mul
23
- * div r_gmpq_div mpq_div
24
- * -@ r_gmpq_neg mpq_neg
25
- * neg r_gmpq_neg mpq_neg
26
- * neg! r_gmpq_neg_self mpq_neg
27
- * abs r_gmpq_abs mpq_abs
28
- * abs! r_gmpq_abs_self mpq_abs
10
+ * Instances of this class can store variables of the type mpq_t. This class also
11
+ * contains many methods that act as the functions for mpq_t variables, as well as a few
12
+ * methods that attempt to make this library more Ruby-ish.
29
13
  */
30
14
 
31
15
  /**********************************************************************
@@ -53,6 +37,30 @@ static VALUE r_gmpq_##fname(VALUE self) \
53
37
  return res; \
54
38
  }
55
39
 
40
+ /*
41
+ * DEFUN_RAT2RAT defines two functions. The first takes a GMP::Q as self, calls mpq_fname
42
+ * on the contained mpq_t, whose arguments are exactly (0) the return argument and (1)
43
+ * self. The second is the same destructive method.
44
+ */
45
+ #define DEFUN_RAT2RAT(fname,mpq_fname) \
46
+ static VALUE r_gmpq_##fname(VALUE self) \
47
+ { \
48
+ MP_RAT *self_val, *res_val; \
49
+ VALUE res; \
50
+ mpq_get_struct(self, self_val); \
51
+ mpq_make_struct_init(res, res_val); \
52
+ mpq_fname(res_val, self_val); \
53
+ return res; \
54
+ } \
55
+ \
56
+ static VALUE r_gmpq_##fname##_self(VALUE self) \
57
+ { \
58
+ MP_RAT *self_val; \
59
+ mpq_get_struct(self, self_val); \
60
+ mpq_fname(self_val, self_val); \
61
+ return self; \
62
+ }
63
+
56
64
 
57
65
  /**********************************************************************
58
66
  * Initializing Rationals *
@@ -62,7 +70,7 @@ static VALUE r_gmpq_##fname(VALUE self) \
62
70
  * call-seq:
63
71
  * GMP::Q.new(arg)
64
72
  *
65
- * Creates a new GMP::Q rational, with arg as its value, converting where
73
+ * Creates a new GMP::Q rational, with _arg_ as its value, converting where
66
74
  * necessary.
67
75
  */
68
76
  VALUE r_gmpqsg_new(int argc, VALUE *argv, VALUE klass)
@@ -86,7 +94,7 @@ VALUE r_gmpqsg_new(int argc, VALUE *argv, VALUE klass)
86
94
  * call-seq:
87
95
  * GMP::Q(arg)
88
96
  *
89
- * A convenience method for +GMP::Q.new(arg)+.
97
+ * A convenience method for GMP::Q.new(arg).
90
98
  */
91
99
  VALUE r_gmpmod_q(int argc, VALUE *argv, VALUE module)
92
100
  {
@@ -96,9 +104,9 @@ VALUE r_gmpmod_q(int argc, VALUE *argv, VALUE module)
96
104
 
97
105
  /*
98
106
  * call-seq:
99
- * rat1.swap rat2
107
+ * p.swap(q)
100
108
  *
101
- * Efficiently swaps the contents of +rat1+ with +rat2+.
109
+ * Efficiently swaps the contents of _p_ with _q_.
102
110
  */
103
111
  VALUE r_gmpq_swap(VALUE self, VALUE arg)
104
112
  {
@@ -122,17 +130,16 @@ VALUE r_gmpq_swap(VALUE self, VALUE arg)
122
130
 
123
131
  /*
124
132
  * call-seq:
125
- * rational.to_d
133
+ * p.to_d
126
134
  *
127
- * Returns +rational+ as an Float if +rational+ fits in a Float.
135
+ * Returns _p_ as an Float if _p_ fits in a Float.
128
136
  *
129
- * Otherwise returns the least significant part of +rational+, with the same
130
- * sign as +rational+.
137
+ * Otherwise returns the least significant part of _p_, with the same sign as _p_.
131
138
  *
132
- * If the exponent from the conversion is too big or too small to fit a double
133
- * then the result is system dependent. For too big an infinity is returned
134
- * when available. For too small 0.0 is normally returned. Hardware overflow,
135
- * underflow and denorm traps may or may not occur.
139
+ * If the exponent from the conversion is too big or too small to fit a double then the
140
+ * result is system dependent. For too big an infinity is returned when available. For
141
+ * too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may
142
+ * or may not occur.
136
143
  */
137
144
  VALUE r_gmpq_to_d(VALUE self)
138
145
  {
@@ -143,12 +150,10 @@ VALUE r_gmpq_to_d(VALUE self)
143
150
  }
144
151
 
145
152
  /*
146
- * Document-method: to_s
147
- *
148
153
  * call-seq:
149
- * rational.to_s
154
+ * p.to_s
150
155
  *
151
- * Returns the decimal representation of +rational+, as a Ruby string.
156
+ * Returns the decimal representation of _p_, as a String.
152
157
  */
153
158
  VALUE r_gmpq_to_s(VALUE self)
154
159
  {
@@ -192,9 +197,9 @@ VALUE r_gmpq_to_s(VALUE self)
192
197
 
193
198
  /*
194
199
  * call-seq:
195
- * rat1 + rat2
200
+ * p + q
196
201
  *
197
- * Adds +rat1+ to +rat2+. +rat2+ can be
202
+ * Adds _p_ to _q_. _q_ must be an instance of one of:
198
203
  * * GMP::Z
199
204
  * * Fixnum
200
205
  * * GMP::Q
@@ -244,9 +249,9 @@ VALUE r_gmpq_add(VALUE self, VALUE arg)
244
249
 
245
250
  /*
246
251
  * call-seq:
247
- * rat1 - rat2
252
+ * p - q
248
253
  *
249
- * Subtracts +rat2+ from +rat1+. +rat2+ can be
254
+ * Subtracts _p_ from _q_. _q_ must be an instance of one of:
250
255
  * * GMP::Z
251
256
  * * Fixnum
252
257
  * * GMP::Q
@@ -299,9 +304,9 @@ VALUE r_gmpq_sub(VALUE self, VALUE arg)
299
304
 
300
305
  /*
301
306
  * call-seq:
302
- * rat1 * rat2
307
+ * p * q
303
308
  *
304
- * Multiplies +rat1+ with +rat2+. +rat2+ can be
309
+ * Multiplies _p_ with _q_. _q_ must be an instance of one of:
305
310
  * * GMP::Z
306
311
  * * Fixnum
307
312
  * * GMP::Q
@@ -371,9 +376,9 @@ VALUE r_gmpq_mul(VALUE self, VALUE arg)
371
376
 
372
377
  /*
373
378
  * call-seq:
374
- * rat1 / rat2
379
+ * p / q
375
380
  *
376
- * Divides +rat1+ by +rat2+. +rat2+ can be
381
+ * Divides _p_ by _q_. _q_ must be an instance of one of:
377
382
  * * GMP::Z
378
383
  * * Fixnum
379
384
  * * GMP::Q
@@ -441,81 +446,41 @@ DEFUN_RAT2INT(ceil,mpz_cdiv_q)
441
446
  * Document-method: neg
442
447
  *
443
448
  * call-seq:
444
- * -rational
445
- * rational.neg
449
+ * a.neg
450
+ * -a
446
451
  *
447
- * From the GMP Manual:
448
- *
449
- * Returns -+rational+.
452
+ * Returns -_a_.
450
453
  */
451
- VALUE r_gmpq_neg(VALUE self)
452
- {
453
- MP_RAT *self_val, *res_val;
454
- VALUE res;
455
- mpq_get_struct(self, self_val);
456
- mpq_make_struct_init(res, res_val);
457
- mpq_neg (res_val, self_val);
458
- return res;
459
- }
460
-
461
454
  /*
462
455
  * Document-method: neg!
463
456
  *
464
457
  * call-seq:
465
- * rational.neg!
458
+ * a.neg!
466
459
  *
467
- * From the GMP Manual:
468
- *
469
- * Sets +rational+ to -+rational+.
460
+ * Sets _a_ to -_a_.
470
461
  */
471
- VALUE r_gmpq_neg_self(VALUE self)
472
- {
473
- MP_RAT *self_val;
474
- mpq_get_struct(self, self_val);
475
- mpz_neg (mpq_numref(self_val), mpq_numref(self_val));
476
- return Qnil;
477
- }
462
+ DEFUN_RAT2RAT(neg, mpq_neg)
478
463
 
479
464
  /*
480
- * Document-method: abs
465
+ * call-seq:
466
+ * p.abs
481
467
  *
468
+ * Returns the absolute value of _p_.
469
+ */
470
+ /*
482
471
  * call-seq:
483
- * rational.abs
472
+ * p.abs!
484
473
  *
485
- * From the GMP Manual:
486
- *
487
- * Returns the absolute value of +rational+.
474
+ * Sets _p_ to its absolute value.
488
475
  */
489
- VALUE r_gmpq_abs(VALUE self)
490
- {
491
- MP_RAT *self_val, *res_val;
492
- VALUE res;
493
- mpq_get_struct(self, self_val);
494
- mpq_make_struct_init(res, res_val);
495
- mpz_abs (mpq_numref(res_val), mpq_numref(self_val));
496
- mpz_set (mpq_denref(res_val), mpq_denref(self_val));
497
-
498
- return res;
499
- }
476
+ DEFUN_RAT2RAT(abs, mpq_abs)
500
477
 
501
478
  /*
502
- * Document-method: abs!
503
- *
504
479
  * call-seq:
505
- * rational.abs!
480
+ * p.inv
506
481
  *
507
- * From the GMP Manual:
508
- *
509
- * Sets +rational+ to its absolute value.
482
+ * Returns 1/<i>p</i>.
510
483
  */
511
- VALUE r_gmpq_abs_self(VALUE self)
512
- {
513
- MP_RAT *self_val;
514
- mpq_get_struct(self, self_val);
515
- mpz_abs (mpq_numref(self_val), mpq_numref(self_val));
516
- return Qnil;
517
- }
518
-
519
484
  VALUE r_gmpq_inv(VALUE self)
520
485
  {
521
486
  MP_RAT *self_val, *res_val;
@@ -530,6 +495,12 @@ VALUE r_gmpq_inv(VALUE self)
530
495
  return res;
531
496
  }
532
497
 
498
+ /*
499
+ * call-seq:
500
+ * p.inv!
501
+ *
502
+ * Sets _p_ to 1/<i>p</i>.
503
+ */
533
504
  VALUE r_gmpq_inv_self(VALUE self)
534
505
  {
535
506
  MP_RAT *self_val;
@@ -704,11 +675,9 @@ static VALUE r_gmpq_cmpabs(VALUE self, VALUE arg)
704
675
 
705
676
  /*
706
677
  * call-seq:
707
- * rational.sgn
678
+ * p.sgn
708
679
  *
709
- * From the GMP Manual:
710
- *
711
- * Returns +1 if +rational+ > 0, 0 if +rational+ == 0, and -1 if +rational+ < 0.
680
+ * Returns +1 if _p_ > 0, 0 if _p_ == 0, and -1 if _p_ < 0.
712
681
  */
713
682
  VALUE r_gmpq_sgn(VALUE self)
714
683
  {
@@ -761,16 +730,17 @@ void init_gmpq()
761
730
  rb_define_method(cGMP_Q, "to_s", r_gmpq_to_s, 0);
762
731
 
763
732
  // Rational Arithmetic
764
- rb_define_method(cGMP_Q, "+", r_gmpq_add, 1);
765
- rb_define_method(cGMP_Q, "-", r_gmpq_sub, 1);
766
- rb_define_method(cGMP_Q, "*", r_gmpq_mul, 1);
767
- rb_define_method(cGMP_Q, "/", r_gmpq_div, 1);
768
- rb_define_method(cGMP_Q, "-@", r_gmpq_neg, 0);
733
+ rb_define_method(cGMP_Q, "+", r_gmpq_add, 1);
734
+ rb_define_method(cGMP_Q, "-", r_gmpq_sub, 1);
735
+ rb_define_method(cGMP_Q, "*", r_gmpq_mul, 1);
736
+ rb_define_method(cGMP_Q, "/", r_gmpq_div, 1);
737
+ rb_define_method(cGMP_Q, "-@", r_gmpq_neg, 0);
738
+ rb_define_alias( cGMP_Q, "neg", "-@");
769
739
  rb_define_method(cGMP_Q, "neg!", r_gmpq_neg_self, 0);
770
- rb_define_method(cGMP_Q, "inv", r_gmpq_inv, 0);
771
- rb_define_method(cGMP_Q, "inv!", r_gmpq_inv_self, 0);
772
- rb_define_method(cGMP_Q, "abs", r_gmpq_abs, 0);
740
+ rb_define_method(cGMP_Q, "abs", r_gmpq_abs, 0);
773
741
  rb_define_method(cGMP_Q, "abs!", r_gmpq_abs_self, 0);
742
+ rb_define_method(cGMP_Q, "inv", r_gmpq_inv, 0);
743
+ rb_define_method(cGMP_Q, "inv!", r_gmpq_inv_self, 0);
774
744
 
775
745
  // Comparing Rationals
776
746
  rb_define_method(cGMP_Q, "<=>", r_gmpq_cmp, 1);
@@ -242,6 +242,35 @@ VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg)
242
242
  return res;
243
243
  }
244
244
 
245
+ /*
246
+ * call-seq:
247
+ * rand_state.rrandomb(fixnum)
248
+ *
249
+ * From the GMP Manual:
250
+ *
251
+ * Generate a random integer with long strings of zeros and ones in the binary
252
+ * representation. Useful for testing functions and algorithms, since this kind
253
+ * of random numbers have proven to be more likely to trigger corner-case bugs.
254
+ * The random number will be in the range 0 to 2^n-1, inclusive.
255
+ */
256
+ VALUE r_gmprandstate_rrandomb(VALUE self, VALUE arg)
257
+ {
258
+ MP_RANDSTATE *self_val;
259
+ MP_INT *res_val;
260
+ VALUE res;
261
+
262
+ mprandstate_get_struct(self,self_val);
263
+
264
+ if (FIXNUM_P(arg)) {
265
+ mpz_make_struct_init(res, res_val);
266
+ mpz_rrandomb(res_val, self_val, FIX2INT(arg));
267
+ } else {
268
+ typeerror(X);
269
+ }
270
+
271
+ return res;
272
+ }
273
+
245
274
 
246
275
  #ifdef MPFR
247
276
  /**********************************************************************
@@ -307,6 +336,7 @@ void init_gmprandstate()
307
336
  // Integer Random Numbers
308
337
  rb_define_method(cGMP_RandState, "urandomb", r_gmprandstate_urandomb, 1);
309
338
  rb_define_method(cGMP_RandState, "urandomm", r_gmprandstate_urandomm, 1);
339
+ rb_define_method(cGMP_RandState, "rrandomb", r_gmprandstate_rrandomb, 1);
310
340
 
311
341
  #ifdef MPFR
312
342
  // Float Random Numbers
data/ext/gmpz.c CHANGED
@@ -10,85 +10,6 @@
10
10
  * Instances of this class can store variables of the type mpz_t. This class
11
11
  * also contains many methods that act as the functions for mpz_t variables,
12
12
  * as well as a few methods that attempt to make this library more Ruby-ish.
13
- *
14
- * The following list is just a simple checklist for me, really. A better
15
- * reference should be found in the rdocs.
16
- *
17
- * Ruby method C Extension function GMP function
18
- * to_d r_gmpz_to_d mpz_get_d
19
- * to_i r_gmpz_to_i mpz_get_i
20
- * to_s r_gmpz_to_s mpz_get_s
21
- * + r_gmpz_add mpz_add
22
- * \------------------------ mpz_add_ui
23
- * add! r_gmpz_add_self mpz_add
24
- * \----------------------------- mpz_add_ui
25
- * - r_gmpz_sub mpz_sub
26
- * \------------------------ mpz_sub_ui
27
- * sub! r_gmpz_sub_self mpz_sub
28
- * \----------------------------- mpz_sub_ui
29
- * * r_gmpz_mul mpz_mul
30
- * \------------------------ mpz_mul_si
31
- * addmul! r_gmpz_addmul_self mpz_addmul
32
- * \-------------------------------- mpz_addmul_ui
33
- * / r_gmpz_div ...
34
- * tdiv r_gmpz_tdiv mpz_tdiv_q
35
- * tmod r_gmpz_tmod mpz_tdiv_r
36
- * fdiv r_gmpz_fdiv mpz_fdiv_q
37
- * fmod r_gmpz_fmod mpz_fdiv_r
38
- * cdiv r_gmpz_cdiv mpz_cdiv_q
39
- * cmod r_gmpz_cmod mpz_cdiv_r
40
- * % r_gmpz_mod mpz_mod
41
- * \------------------------ mpz_mod_ui
42
- * -@ r_gmpz_neg mpz_neg
43
- * neg r_gmpz_neg mpz_neg
44
- * neg! r_gmpz_neg_self mpz_neg
45
- * abs r_gmpz_abs mpz_abs
46
- * abs! r_gmpz_abs_self mpz_abs
47
- * ** r_gmpz_pow mpz_pow_ui
48
- * powmod r_gmpz_powm mpz_powm
49
- * root r_gmpz_root mpz_root
50
- * sqrt r_gmpz_sqrt mpz_sqrt
51
- * sqrt! r_gmpz_sqrt_self mpz_sqrt
52
- * sqrtrem r_gmpz_sqrtrem mpz_sqrtrem
53
- * power? r_gmpz_is_power mpz_perfect_power_p
54
- * square? r_gmpz_is_square mpz_perfect_square_p
55
- * probab_prime? r_gmpz_is_probab_prime mpz_probab_prime_p
56
- * nextprime r_gmpz_nextprime mpz_nextprime
57
- * nextprime! r_gmpz_nextprime_self mpz_nextprime
58
- * gcd r_gmpz_gcd mpz_gcd
59
- * \------------------------ mpz_gcd_ui
60
- * invert r_gmpz_invert mpz_invert
61
- * jacobi r_gmpz_jacobi mpz_jacobi
62
- * #jacobi r_gmpzsg_jacobi mpz_jacobi
63
- * legendre r_gmpz_legendre mpz_legendre
64
- * remove r_gmpz_remove mpz_remove
65
- * fac r_gmpz_fac mpz_fac_ui
66
- * fib r_gmpz_fib mpz_fib_ui
67
- * <=>
68
- * <
69
- * <=
70
- * ==
71
- * >=
72
- * >
73
- * cmpabs
74
- * sgn
75
- * eql? r_gmpz_eql ------------
76
- * hash r_gmpz_hash
77
- * and
78
- * ior
79
- * xor
80
- * com r_gmpz_com mpz_com
81
- * com! r_gmpz_com_self mpz_com
82
- * popcount r_gmpz_popcount
83
- * scan0 r_gmpz_scan0 mpz_scan0
84
- * scan1 r_gmpz_scan1 mpz_scan1
85
- * []= r_gmpz_setbit mpz_setbit
86
- * [] r_gmpz_getbit mpz_tstbit
87
- * even? r_gmpz_is_even mpz_even
88
- * odd? r_gmpz_is_odd mpz_odd
89
- * sizeinbase r_gmpz_sizeinbase mpz_sizeinbase
90
- * size_in_bin r_gmpz_size_in_bin mpz_sizeinbits
91
- * size r_gmpz_size mpz_size
92
13
  */
93
14
 
94
15
  /**********************************************************************
@@ -251,16 +172,38 @@ static VALUE r_gmpzsg_##fname(VALUE klass, VALUE arg) \
251
172
  return res; \
252
173
  }
253
174
 
175
+ #define DEFUN_INT_COND_P(fname,mpz_fname) \
176
+ static VALUE r_gmpz_##fname(VALUE self) \
177
+ { \
178
+ MP_INT *self_val; \
179
+ mpz_get_struct (self, self_val); \
180
+ return mpz_fname (self_val) ? Qtrue : Qfalse; \
181
+ }
182
+
254
183
  /**********************************************************************
255
184
  * Initializing, Assigning Integers *
256
185
  **********************************************************************/
257
186
 
258
187
  /*
259
188
  * call-seq:
260
- * GMP::Z.new(arg = 0)
189
+ * GMP::Z.new(value = 0)
261
190
  *
262
- * Creates a new GMP::Z integer, with +arg+ as its value, converting where
263
- * necessary.
191
+ * Creates a new GMP::Z integer, with _value_ as its value, converting where necessary.
192
+ * _value_ must be an instance of one of the following classes:
193
+ *
194
+ * * GMP::Z
195
+ * * Fixnum
196
+ * * String
197
+ * * Bignum
198
+ *
199
+ * @example
200
+ * GMP::Z.new(5) #=> 5
201
+ * GMP::Z.new(2**32) #=> 4_294_967_296
202
+ * GMP::Z.new(2**101) #=> 2_535_301_200_456_458_802_993_406_410_752
203
+ * GMP::Z.new("20") #=> 20
204
+ * GMP::Z.new("0x20") #=> 32
205
+ * GMP::Z.new("020") #=> 16
206
+ * GMP::Z.new("0b101") #=> 5
264
207
  */
265
208
  VALUE r_gmpzsg_new(int argc, VALUE *argv, VALUE klass)
266
209
  {
@@ -293,8 +236,8 @@ VALUE r_gmpz_initialize(int argc, VALUE *argv, VALUE self)
293
236
  * call-seq:
294
237
  * a = b
295
238
  *
296
- * Sets the value of +a+ to the value of +b+. +b+ may be one of the following
297
- * classes:
239
+ * Sets the value of _a_ to the value of _b_. _b_ must be an instance of one of the
240
+ * following classes:
298
241
  *
299
242
  * * GMP::Z
300
243
  * * Fixnum
@@ -321,9 +264,9 @@ void mpz_set_value(MP_INT *target, VALUE source)
321
264
 
322
265
  /*
323
266
  * call-seq:
324
- * GMP::Z(arg)
267
+ * GMP::Z(value)
325
268
  *
326
- * A convenience method for +GMP::Z.new(arg)+.
269
+ * A convenience method for GMP::Z.new(value).
327
270
  */
328
271
  VALUE r_gmpmod_z(int argc, VALUE *argv, VALUE module)
329
272
  {
@@ -333,9 +276,11 @@ VALUE r_gmpmod_z(int argc, VALUE *argv, VALUE module)
333
276
 
334
277
  /*
335
278
  * call-seq:
336
- * int1.swap int2
279
+ * a.swap(b)
337
280
  *
338
- * Efficiently swaps the contents of +int1+ with +int2+.
281
+ * @return nil
282
+ *
283
+ * Efficiently swaps the contents of _a_ with _b_. _b_ must be an instance of GMP::Z.
339
284
  */
340
285
  VALUE r_gmpz_swap(VALUE self, VALUE arg)
341
286
  {
@@ -356,16 +301,17 @@ VALUE r_gmpz_swap(VALUE self, VALUE arg)
356
301
 
357
302
  /*
358
303
  * call-seq:
359
- * integer.to_i
304
+ * a.to_i
305
+ *
306
+ * @todo Implement mpz_fits_slong_p
360
307
  *
361
- * Returns +integer+ as an Fixnum if +integer+ fits in a Fixnum.
308
+ * Returns _a_ as an Fixnum if _a_ fits into a Fixnum.
362
309
  *
363
- * Otherwise returns the least significant part of +integer+, with the same
364
- * sign as +integer+.
310
+ * Otherwise returns the least significant part of _a_, with the same sign as _a_.
365
311
  *
366
- * If +integer+ is too big to fit in a Fixnum, the returned result is probably
367
- * not very useful. To find out if the value will fit, use the function
368
- * mpz_fits_slong_p (<b>Unimplemented</b>).
312
+ * If _a_ is too big to fit in a Fixnum, the returned result is probably not very useful.
313
+ * To find out if the value will fit, use the function +mpz_fits_slong_p+
314
+ * (<b>Unimplemented</b>).
369
315
  */
370
316
  VALUE r_gmpz_to_i(VALUE self)
371
317
  {
@@ -384,16 +330,17 @@ VALUE r_gmpz_to_i(VALUE self)
384
330
 
385
331
  /*
386
332
  * call-seq:
387
- * integer.to_d
333
+ * a.to_d
388
334
  *
389
- * Returns +integer+ as an Float if +integer+ fits in a Float.
335
+ * @todo Implement mpz_fits_slong_p
390
336
  *
391
- * Otherwise returns the least significant part of +integer+, with the same
392
- * sign as +integer+.
337
+ * Returns _a_ as a Float if _a_ fits in a Float.
393
338
  *
394
- * If +integer+ is too big to fit in a Float, the returned result is probably
395
- * not very useful. To find out if the value will fit, use the function
396
- * mpz_fits_slong_p (<b>Unimplemented</b>).
339
+ * Otherwise returns the least significant part of _a_, with the same sign as _a_.
340
+ *
341
+ * If _a_ is too big to fit in a Float, the returned result is probably not very useful.
342
+ * To find out if the value will fit, use the function +mpz_fits_slong_p+
343
+ * (<b>Unimplemented</b>).
397
344
  */
398
345
  VALUE r_gmpz_to_d(VALUE self)
399
346
  {
@@ -407,20 +354,23 @@ VALUE r_gmpz_to_d(VALUE self)
407
354
  * Document-method: to_s
408
355
  *
409
356
  * call-seq:
410
- * integer.to_s(base = 10)
357
+ * a.to_s(base = 10)
358
+ * a.to_s(:bin)
359
+ * a.to_s(:oct)
360
+ * a.to_s(:dec)
361
+ * a.to_s(:hex)
411
362
  *
412
- * Returns +integer+, as a Ruby string. If +base+ is not provided, then
413
- * the string will be the decimal representation.
363
+ * Returns _a_, as a String. If _base_ is not provided, then the decimal
364
+ * representation will be returned.
414
365
  *
415
366
  * From the GMP Manual:
416
367
  *
417
- * Convert +integer+ to a string of digits in base +base+. The +base+ argument
418
- * may vary from 2 to 62 or from -2 to -36.
368
+ * Convert _a_ to a string of digits in base _base_. The _base_ argument may vary from 2
369
+ * to 62 or from -2 to -36.
419
370
  *
420
- * For +base+ in the range 2..36, digits and lower-case letters are used; for
421
- * -2..-36, digits and upper-case letters are used; for 37..62, digits,
422
- * upper-case letters, and lower-case letters (in that significance order) are
423
- * used.
371
+ * For _base_ in the range 2..36, digits and lower-case letters are used; for -2..-36,
372
+ * digits and upper-case letters are used; for 37..62, digits, upper-case letters, and
373
+ * lower-case letters (in that significance order) are used.
424
374
  */
425
375
  VALUE r_gmpz_to_s(int argc, VALUE *argv, VALUE self)
426
376
  {
@@ -480,9 +430,9 @@ VALUE r_gmpz_to_s(int argc, VALUE *argv, VALUE self)
480
430
 
481
431
  /*
482
432
  * call-seq:
483
- * +(other)
433
+ * a + b
484
434
  *
485
- * Adds this GMP::Z to other. Other can be
435
+ * Adds _a_ to _b_. _b_ must be an instance of one of:
486
436
  * * GMP::Z
487
437
  * * Fixnum
488
438
  * * GMP::Q
@@ -527,10 +477,9 @@ VALUE r_gmpz_add(VALUE self, VALUE arg)
527
477
 
528
478
  /*
529
479
  * call-seq:
530
- * add!(other)
480
+ * a.add!(_b_)
531
481
  *
532
- * Adds this GMP::Z to other, and sets this GMP::Z's value to the result. Other
533
- * can be
482
+ * Adds _a_ to _b_ in-place, setting _a_ to the sum. _b_ must be an instance of one of:
534
483
  * * GMP::Z
535
484
  * * Fixnum
536
485
  * * GMP::Q
@@ -563,9 +512,9 @@ VALUE r_gmpz_add_self(VALUE self, VALUE arg)
563
512
 
564
513
  /*
565
514
  * call-seq:
566
- * -(other)
515
+ * a - b
567
516
  *
568
- * Subtracts other from this GMP::Z. Other can be
517
+ * Subtracts _b_ from _a_. _b_ must be an instance of one of:
569
518
  * * GMP::Z
570
519
  * * Fixnum
571
520
  * * GMP::Q
@@ -615,10 +564,10 @@ VALUE r_gmpz_sub(VALUE self, VALUE arg)
615
564
 
616
565
  /*
617
566
  * call-seq:
618
- * sub!(other)
567
+ * a.sub!(b)
619
568
  *
620
- * Subtracts other from this GMP::Z, and sets this GMP::Z's value to the
621
- * result. Other can be
569
+ * Subtracts _b_ from _a_ in-place, setting _a_ to the difference. _b_ must be an
570
+ * instance of one of:
622
571
  * * GMP::Z
623
572
  * * Fixnum
624
573
  * * GMP::Q
@@ -651,9 +600,9 @@ VALUE r_gmpz_sub_self(VALUE self, VALUE arg)
651
600
 
652
601
  /*
653
602
  * call-seq:
654
- * *(other)
603
+ * a * b
655
604
  *
656
- * Multiplies this GMP::Z with other. Other can be
605
+ * Multiplies _a_ with _b_. _a_ must be an instance of one of
657
606
  * * GMP::Z
658
607
  * * Fixnum
659
608
  * * GMP::Q
@@ -696,11 +645,14 @@ VALUE r_gmpz_mul(VALUE self, VALUE arg)
696
645
  * call-seq:
697
646
  * a.addmul!(b, c)
698
647
  *
699
- * From the GMP Manual:
700
- *
701
- * Sets +a+ to +a+ plus +b+ times +c+.
648
+ * @since 0.4.19
649
+ *
650
+ * Sets _a_ to _a_ plus _b_ times _c_. _b_ and _c_ must each be an instance of one of
651
+ * * GMP::Z
652
+ * * Fixnum
653
+ * * Bignum
702
654
  */
703
- VALUE r_gmpz_addmul_self(VALUE self, VALUE b, VALUE c)
655
+ static VALUE r_gmpz_addmul_self(VALUE self, VALUE b, VALUE c)
704
656
  {
705
657
  MP_INT *self_val, *b_val, *c_val;
706
658
  int free_b_val = 0;
@@ -743,16 +695,68 @@ VALUE r_gmpz_addmul_self(VALUE self, VALUE b, VALUE c)
743
695
  return self;
744
696
  }
745
697
 
698
+ /*
699
+ * call-seq:
700
+ * a.submul!(b, c)
701
+ *
702
+ * @since 0.5.23
703
+ *
704
+ * Sets _a_ to _a_ minus _b_ times _c_. _b_ and _c_ must each be an instance of one of
705
+ * * GMP::Z
706
+ * * Fixnum
707
+ * * Bignum
708
+ */
709
+ static VALUE r_gmpz_submul_self(VALUE self, VALUE b, VALUE c)
710
+ {
711
+ MP_INT *self_val, *b_val, *c_val;
712
+ int free_b_val = 0;
713
+
714
+ if (GMPZ_P(b)) {
715
+ mpz_get_struct(b, b_val);
716
+ } else if (FIXNUM_P(b)) {
717
+ mpz_temp_alloc(b_val);
718
+ mpz_init_set_si(b_val, FIX2NUM(b));
719
+ free_b_val = 1;
720
+ } else if (BIGNUM_P(b)) {
721
+ mpz_temp_from_bignum(b_val, b);
722
+ free_b_val = 1;
723
+ } else {
724
+ typeerror_as(ZXB, "addend");
725
+ }
726
+ mpz_get_struct(self, self_val);
727
+
728
+ if (GMPZ_P(c)) {
729
+ mpz_get_struct(c, c_val);
730
+ mpz_submul(self_val, b_val, c_val);
731
+ } else if (FIXNUM_P(c)) {
732
+ if (FIX2NUM(c) < 0)
733
+ {
734
+ if (free_b_val) { mpz_temp_free(b_val); }
735
+ rb_raise(rb_eRangeError, "multiplicand (Fixnum) must be nonnegative");
736
+ }
737
+ mpz_submul_ui(self_val, b_val, FIX2NUM(c));
738
+ } else if (BIGNUM_P(c)) {
739
+ mpz_temp_from_bignum(c_val, c);
740
+ mpz_submul(self_val, b_val, c_val);
741
+ mpz_temp_free(c_val);
742
+ } else {
743
+ if (free_b_val)
744
+ mpz_temp_free(b_val);
745
+ typeerror_as(ZXB, "multiplicand");
746
+ }
747
+ if (free_b_val)
748
+ mpz_temp_free(b_val);
749
+ return self;
750
+ }
751
+
746
752
  /*
747
753
  * Document-method: <<
748
754
  *
749
755
  * call-seq:
750
- * integer << n
756
+ * a << n
751
757
  *
752
- * From the GMP Manual:
753
- *
754
- * Returns +integer+ times 2 raised to +n+. This operation can also be defined
755
- * as a left shift by +n+ bits.
758
+ * Returns _a_ times 2 raised to _n_. This operation can also be defined as a left shift
759
+ * by _n_ bits.
756
760
  */
757
761
  DEFUN_INT_F_UL(shl,mpz_mul_2exp,"shift size")
758
762
 
@@ -760,43 +764,35 @@ DEFUN_INT_F_UL(shl,mpz_mul_2exp,"shift size")
760
764
  * Document-method: neg
761
765
  *
762
766
  * call-seq:
763
- * integer.neg
764
- * -integer
767
+ * a.neg
768
+ * -a
765
769
  *
766
- * From the GMP Manual:
767
- *
768
- * Returns -+integer+.
770
+ * Returns -_a_.
769
771
  */
770
772
  /*
771
773
  * Document-method: neg!
772
774
  *
773
775
  * call-seq:
774
- * integer.neg!
776
+ * a.neg!
775
777
  *
776
- * From the GMP Manual:
777
- *
778
- * Sets +integer+ to -+integer+.
778
+ * Sets _a_ to -_a_.
779
779
  */
780
780
  DEFUN_INT2INT(neg, mpz_neg)
781
781
  /*
782
782
  * Document-method: abs
783
783
  *
784
784
  * call-seq:
785
- * integer.abs
785
+ * a.abs
786
786
  *
787
- * From the GMP Manual:
788
- *
789
- * Returns the absolute value of +integer+.
787
+ * Returns the absolute value of _a_.
790
788
  */
791
789
  /*
792
790
  * Document-method: abs!
793
791
  *
794
792
  * call-seq:
795
- * integer.abs!
793
+ * a.abs!
796
794
  *
797
- * From the GMP Manual:
798
- *
799
- * Sets +integer+ to its absolute value.
795
+ * Sets _a_ to its absolute value.
800
796
  */
801
797
  DEFUN_INT2INT(abs, mpz_abs)
802
798
 
@@ -809,10 +805,9 @@ DEFUN_INT2INT(abs, mpz_abs)
809
805
  * call-seq:
810
806
  * a / b
811
807
  *
812
- * Divides _a_ by _b_. Combines the different GMP division functions to
813
- * provide what one is hopefully looking for. The result will either be
814
- * an instance of GMP::Q or GMP::F, depending on _b_. _b_ can be an
815
- * instance of any of the following:
808
+ * Divides _a_ by _b_. Combines the different GMP division functions to provide what one
809
+ * is hopefully looking for. The result will either be an instance of GMP::Q or GMP::F,
810
+ * depending on _b_. _b_ must be an instance of one of the following:
816
811
  * * GMP::Z
817
812
  * * Fixnum
818
813
  * * GMP::Q
@@ -881,15 +876,12 @@ VALUE r_gmpz_div(VALUE self, VALUE arg)
881
876
  * Document-method: tdiv
882
877
  *
883
878
  * call-seq:
884
- * n.tdiv d
885
- *
886
- * From the GMP Manual:
879
+ * n.tdiv(d)
887
880
  *
888
- * Divides +n+ by +d+, forming a quotient +q+. tdiv rounds +q+ towards zero.
889
- * The +t+ stands for "truncate".
881
+ * Divides _n_ by _d_, forming a quotient _q_. tdiv rounds _q_ towards zero. The _t_
882
+ * stands for "truncate".
890
883
  *
891
- * +q+ will satisfy <tt>n=q*d+r</tt>, and +r+ will satisfy
892
- * <tt>0<=abs(r)<abs(d)</tt>.
884
+ * _q_ will satisfy <i>n=q*d+r</i>, and _r_ will satisfy <i>0 <= abs( r ) < abs( d )</i>.
893
885
  *
894
886
  * This function calculates only the quotient.
895
887
  */
@@ -898,34 +890,45 @@ DEFUN_INT_DIV(tdiv, mpz_tdiv_q)
898
890
  * Document-method: tmod
899
891
  *
900
892
  * call-seq:
901
- * n.tmod d
902
- *
903
- * From the GMP Manual:
893
+ * n.tmod(d)
904
894
  *
905
- * Divides +n+ by +d+, forming a remainder +r+. +r+ will have the same sign as
906
- * +n+. The +t+ stands for truncate�.
895
+ * Divides _n_ by _d_, forming a remainder _r_. _r_ will have the same sign as _n_. The
896
+ * _t_ stands for "truncate".
907
897
  *
908
- * +r+ will satisfy <tt>n=q*d+r</tt>, and +r+ will satisfy
909
- * <tt>0<=abs(r)<abs(d)</tt>.
898
+ * _r_ will satisfy <i>n=q*d+r</i>, and _r_ will satisfy <i>0 <= abs( r ) < abs( d )</i>.
910
899
  *
911
900
  * This function calculates only the remainder.
912
901
  *
913
- * The remainder can be negative, so the return value is the absolute value of
914
- * the remainder.
902
+ * The remainder can be negative, so the return value is the absolute value of the
903
+ * remainder.
915
904
  */
916
905
  DEFUN_INT_DIV(tmod, mpz_tdiv_r)
906
+ /*
907
+ * Document-method: tshr
908
+ *
909
+ * call-seq:
910
+ * n.tshr(d)
911
+ *
912
+ * Divides _n_ by 2^<i>d</i>, forming a quotient _q_. tshr rounds _q_ towards zero. The
913
+ * _t_ stands for "truncate".
914
+ *
915
+ * _q_ will satisfy <i>n=q*d+r</i>, and _r_ will satisfy <i>0 <= abs( r ) < abs( d )</i>.
916
+ *
917
+ * This function calculates only the quotient.
918
+ */
919
+ DEFUN_INT_F_UL(tshr,mpz_tdiv_q_2exp,"shift size")
920
+ DEFUN_INT_F_UL(tshrm,mpz_tdiv_r_2exp,"mark size")
921
+
917
922
  /*
918
923
  * Document-method: fdiv
919
924
  *
920
925
  * call-seq:
921
- * n.fdiv d
926
+ * n.fdiv(d)
922
927
  *
923
- * From the GMP Manual:
924
- *
925
- * Divide n by d, forming a quotient q. fdiv rounds q down towards -infinity.
926
- * The f stands for �floor�.
928
+ * Divide _n_ by _d_, forming a quotient _q_. fdiv rounds _q_ down towards -infinity.
929
+ * The f stands for "floor".
927
930
  *
928
- * q will satisfy n=q*d+r.
931
+ * _q_ will satisfy <i>n=q*d+r</i>.
929
932
  *
930
933
  * This function calculates only the quotient.
931
934
  */
@@ -934,33 +937,32 @@ DEFUN_INT_DIV(fdiv, mpz_fdiv_q)
934
937
  * Document-method: fmod
935
938
  *
936
939
  * call-seq:
937
- * n.fmod d
940
+ * n.fmod(d)
938
941
  *
939
- * From the GMP Manual:
940
- *
941
- * Divides n by d, forming a remainder r. r will have the same sign as d. The f
942
- * stands for �floor�.
942
+ * Divides _n_ by _d_, forming a remainder _r_. _r_ will have the same sign as _d_. The
943
+ * _f_ stands for "floor".
943
944
  *
944
- * r will satisfy n=q*d+r, and r will satisfy 0<=abs(r)<abs(d).
945
+ * _r_ will satisfy <i>n=q*d+r</i>, and _r_ will satisfy <i>0 <= abs( r ) < abs( d )</i>.
945
946
  *
946
947
  * This function calculates only the remainder.
947
948
  *
948
- * The remainder can be negative, so the return value is the absolute value of
949
- * the remainder.
949
+ * The remainder can be negative, so the return value is the absolute value of the
950
+ * remainder.
950
951
  */
951
952
  DEFUN_INT_DIV(fmod, mpz_fdiv_r)
953
+ DEFUN_INT_F_UL(fshr,mpz_fdiv_q_2exp,"shift size")
954
+ DEFUN_INT_F_UL(fshrm,mpz_fdiv_r_2exp,"mark size")
955
+
952
956
  /*
953
957
  * Document-method: cdiv
954
958
  *
955
959
  * call-seq:
956
- * n.cdiv d
960
+ * n.cdiv(d)
957
961
  *
958
- * From the GMP Manual:
959
- *
960
- * Divide +n+ by +d+, forming a quotient +q+. +cdiv+ rounds +q+ up towards
961
- * <tt>+infinity</tt>. The c stands for �ceil�.
962
+ * Divide _n_ by _d_, forming a quotient _q_. cdiv rounds _q_ up towards
963
+ * _+infinity_. The c stands for "ceil".
962
964
  *
963
- * +q+ will satisfy <tt>n=q*d+r</tt>.
965
+ * _q_ will satisfy <i>n=q*d+r</i>.
964
966
  *
965
967
  * This function calculates only the quotient.
966
968
  */
@@ -969,15 +971,12 @@ DEFUN_INT_DIV(cdiv, mpz_cdiv_q)
969
971
  * Document-method: cmod
970
972
  *
971
973
  * call-seq:
972
- * n.cmod d
974
+ * n.cmod(d)
973
975
  *
974
- * From the GMP Manual:
975
- *
976
- * Divides +n+ by +d+, forming a remainder +r+. +r+ will have the opposite sign
977
- * as +d+. The c stands for �ceil�.
976
+ * Divides _n_ by _d_, forming a remainder _r_. _r_ will have the opposite sign of _d_.
977
+ * The c stands for "ceil".
978
978
  *
979
- * +r+ will satisfy <tt>n=q*d+r</tt>, and +r+ will satisfy
980
- * <tt>0<=abs(r)<abs(d)</tt>.
979
+ * _r_ will satisfy <i>n=q*d+r</i>, and _r_ will satisfy <i>0 <= abs( r ) < abs( d )</i>.
981
980
  *
982
981
  * This function calculates only the remainder.
983
982
  */
@@ -987,6 +986,8 @@ DEFUN_INT_DIV(cmod, mpz_cdiv_r)
987
986
  * call-seq:
988
987
  * a % b
989
988
  *
989
+ * @since 0.2.10
990
+ *
990
991
  * Returns _a_ mod _b_. _b_ can be an instance any of the following:
991
992
  * * GMP::Z
992
993
  * * Fixnum
@@ -1016,6 +1017,47 @@ VALUE r_gmpz_mod(VALUE self, VALUE arg)
1016
1017
  return res;
1017
1018
  }
1018
1019
 
1020
+ /*
1021
+ * call-seq:
1022
+ * a.divisible?(b)
1023
+ *
1024
+ * @since 0.5.23
1025
+ *
1026
+ * Returns true if _a_ is divisible by _b_. _b_ can be an instance any of the following:
1027
+ * * GMP::Z
1028
+ * * Fixnum
1029
+ * * Bignum
1030
+ */
1031
+ static VALUE r_gmpz_divisible(VALUE self, VALUE arg)
1032
+ {
1033
+ MP_INT *self_val, *arg_val;
1034
+ int res;
1035
+ mpz_get_struct (self, self_val);
1036
+
1037
+ if (FIXNUM_P (arg) && FIX2NUM (arg) > 0) {
1038
+ mpz_temp_alloc(arg_val);
1039
+ mpz_init_set_ui(arg_val, FIX2NUM(arg));
1040
+ res = mpz_divisible_ui_p (self_val, FIX2NUM (arg));
1041
+ mpz_temp_free(arg_val);
1042
+ } else if (FIXNUM_P (arg)) {
1043
+ mpz_temp_alloc(arg_val);
1044
+ mpz_make_struct_init (arg, arg_val);
1045
+ mpz_init_set_si(arg_val, FIX2NUM(arg));
1046
+ res = mpz_divisible_p (self_val, arg_val);
1047
+ mpz_temp_free(arg_val);
1048
+ } else if (BIGNUM_P (arg)) {
1049
+ mpz_temp_from_bignum(arg_val, arg);
1050
+ res = mpz_divisible_p (self_val, arg_val);
1051
+ mpz_temp_free(arg_val);
1052
+ } else if (GMPZ_P (arg)) {
1053
+ mpz_get_struct(arg, arg_val);
1054
+ res = mpz_divisible_p (self_val, arg_val);
1055
+ } else {
1056
+ typeerror_as (ZXB, "argument");
1057
+ }
1058
+ return (res != 0) ? Qtrue : Qfalse;
1059
+ }
1060
+
1019
1061
 
1020
1062
  /**********************************************************************
1021
1063
  * Integer Exponentiation *
@@ -1025,25 +1067,46 @@ VALUE r_gmpz_mod(VALUE self, VALUE arg)
1025
1067
  * Document-method: **
1026
1068
  *
1027
1069
  * call-seq:
1028
- * integer ** exp
1070
+ * a ** b
1029
1071
  *
1030
- * From the GMP Manual:
1031
- *
1032
- * Returns +integer+ raised to +exp+. The case 0^0 yields 1.
1072
+ * Returns _a_ raised to _b_. The case 0^0 yields 1.
1033
1073
  */
1034
1074
  DEFUN_INT_F_UL(pow,mpz_pow_ui,"exponent")
1035
1075
 
1036
1076
  /*
1077
+ * Document-method: GMP::Z.pow
1078
+ *
1037
1079
  * call-seq:
1038
- * integer.powmod(exp, mod)
1080
+ * GMP::Z.pow(a, b)
1039
1081
  *
1040
- * From the GMP Manual:
1041
- *
1042
- * Returns +integer+ raised to +exp+ modulo +mod+.
1082
+ * Returns _a_ raised to _b_. The case 0^0 yields 1.
1083
+ */
1084
+ VALUE r_gmpzsg_pow(VALUE klass, VALUE base, VALUE exp)
1085
+ {
1086
+ MP_INT *res_val;
1087
+ VALUE res;
1088
+
1089
+ if (FIXNUM_P(base) && FIXNUM_P(exp))
1090
+ {
1091
+ if (FIX2NUM(base) < 0)
1092
+ rb_raise (rb_eRangeError, "base must not be negative");
1093
+ if (FIX2NUM(exp) < 0)
1094
+ rb_raise (rb_eRangeError, "exponent must not be negative");
1095
+ mpz_make_struct_init (res, res_val);
1096
+ mpz_ui_pow_ui (res_val, FIX2NUM(base), FIX2NUM(exp));
1097
+ return res;
1098
+ }
1099
+ return r_gmpz_pow (r_gmpzsg_new(1, &base, klass), exp);
1100
+ }
1101
+
1102
+ /*
1103
+ * call-seq:
1104
+ * a.powmod(b, c)
1043
1105
  *
1044
- * Negative +exp+ is supported if an inverse <tt>integer^-1</tt> mod
1045
- * <tt>mod</tt> exists. If an inverse doesn't exist then a divide by zero is
1046
- * raised.
1106
+ * Returns _a_ raised to _b_ modulo _c_.
1107
+ *
1108
+ * Negative _b_ is supported if an inverse <i>a^-1</i> mod _c_ exists. If an inverse
1109
+ * doesn't exist then a divide by zero is raised.
1047
1110
  */
1048
1111
  VALUE r_gmpz_powm(VALUE self, VALUE exp, VALUE mod)
1049
1112
  {
@@ -1113,11 +1176,9 @@ VALUE r_gmpz_powm(VALUE self, VALUE exp, VALUE mod)
1113
1176
  * Document-method: root
1114
1177
  *
1115
1178
  * call-seq:
1116
- * GMP::Z.root(m, n)
1179
+ * a.root(b)
1117
1180
  *
1118
- * From the GMP Manual:
1119
- *
1120
- * Returns the truncated integer part of the +n+th root of +m+.
1181
+ * Returns the truncated integer part of the <i>b</i>th root of _a_.
1121
1182
  */
1122
1183
  DEFUN_INT_F_UL(root,mpz_root,"root number")
1123
1184
 
@@ -1125,35 +1186,26 @@ DEFUN_INT_F_UL(root,mpz_root,"root number")
1125
1186
  * Document-method: sqrt
1126
1187
  *
1127
1188
  * call-seq:
1128
- * integer.sqrt
1189
+ * a.sqrt
1129
1190
  *
1130
- * From the GMP Manual:
1131
- *
1132
- * Returns the truncated integer part of the square root of +integer+.
1191
+ * Returns the truncated integer part of the square root of _a_.
1133
1192
  */
1134
1193
  /*
1135
1194
  * Document-method: sqrt!
1136
1195
  *
1137
1196
  * call-seq:
1138
- * integer.sqrt!
1197
+ * a.sqrt!
1139
1198
  *
1140
- * From the GMP Manual:
1141
- *
1142
- * Sets +integer+ to the truncated integer part of its square root.
1199
+ * Sets _a_ to the truncated integer part of its square root.
1143
1200
  */
1144
1201
  DEFUN_INT2INT(sqrt, mpz_sqrt)
1145
1202
 
1146
1203
  /*
1147
- * Document-method: sqrtrem
1148
- *
1149
1204
  * call-seq:
1150
- * integer.sqrtrem #=> sqrt, rem
1205
+ * a.sqrtrem #=> s, r
1151
1206
  *
1152
- * From the GMP Manual:
1153
- *
1154
- * Returns the truncated integer part of the square root of +integer+ as
1155
- * +sqrt+ and the remainder <tt>integer - sqrt * sqrt</tt> as +rem+, which will
1156
- * be zero if +integer+ is a perfect square.
1207
+ * Returns the truncated integer part of the square root of _a_ as _s_ and the remainder
1208
+ * <i>a - s * s</i> as _r_, which will be zero if _a_ is a perfect square.
1157
1209
  */
1158
1210
  static VALUE r_gmpz_sqrtrem(VALUE self)
1159
1211
  {
@@ -1171,29 +1223,23 @@ static VALUE r_gmpz_sqrtrem(VALUE self)
1171
1223
  * Document-method: power?
1172
1224
  *
1173
1225
  * call-seq:
1174
- * integer.power?
1226
+ * p.power?
1175
1227
  *
1176
- * From the GMP Manual:
1177
- *
1178
- * Returns true if integer is a perfect power, i.e., if there exist integers a
1179
- * and b, with b>1, such that integer equals a raised to the power b.
1228
+ * Returns true if _p_ is a perfect power, i.e., if there exist integers _a_ and _b_,
1229
+ * with <i>b > 1</i>, such that _p_ equals _a_ raised to the power _b_.
1180
1230
  *
1181
- * Under this definition both 0 and 1 are considered to be perfect powers.
1182
- * Negative values of integers are accepted, but of course can only be odd
1183
- * perfect powers.
1231
+ * Under this definition both 0 and 1 are considered to be perfect powers. Negative
1232
+ * values of integers are accepted, but of course can only be odd perfect powers.
1184
1233
  */
1185
1234
  DEFUN_INT_COND_P(is_power,mpz_perfect_power_p)
1186
1235
  /*
1187
1236
  * Document-method: square?
1188
1237
  *
1189
1238
  * call-seq:
1190
- * integer.square?
1239
+ * p.square?
1191
1240
  *
1192
- * From the GMP Manual:
1193
- *
1194
- * Returns true if integer is a perfect square, i.e., if the square root of
1195
- * integer is an integer. Under this definition both 0 and 1 are considered to
1196
- * be perfect squares.
1241
+ * Returns true if _p_ is a perfect square, i.e., if the square root of _p_ is an
1242
+ * integer. Under this definition both 0 and 1 are considered to be perfect squares.
1197
1243
  */
1198
1244
  DEFUN_INT_COND_P(is_square,mpz_perfect_square_p)
1199
1245
 
@@ -1203,21 +1249,22 @@ DEFUN_INT_COND_P(is_square,mpz_perfect_square_p)
1203
1249
  **********************************************************************/
1204
1250
 
1205
1251
  /*
1206
- * call-seq: probab_prime?(reps = 5)
1252
+ * call-seq:
1253
+ * n.probab_prime?(reps = 5)
1207
1254
  *
1208
- * Determine whether +n+ is prime. Returns 2 if +n+ is definitely prime,
1209
- * returns 1 if +n+ is probably prime (without being certain), or returns 0 if
1210
- * +n+ is definitely composite.
1255
+ * Determine whether _n_ is prime. Returns 2 if _n_ is definitely prime, returns 1 if _n_
1256
+ * is probably prime (without being certain), or returns 0 if _n_ is definitely
1257
+ * composite.
1211
1258
  *
1212
- * This function does some trial divisions, then some Miller-Rabin
1213
- * probabilistic primality tests. +reps+ controls how many such tests are done,
1214
- * 5 to 10 is a reasonable number, more will reduce the chances of a composite
1215
- * being returned as "probably prime".
1259
+ * This function does some trial divisions, then some Miller-Rabin probabilistic
1260
+ * primality tests. +reps+ controls how many such tests are done, 5 to 10 is a reasonable
1261
+ * number, more will reduce the chances of a composite being returned as "probably
1262
+ * prime".
1216
1263
  *
1217
- * Miller-Rabin and similar tests can be more properly called compositeness
1218
- * tests. Numbers which fail are known to be composite but those which pass
1219
- * might be prime or might be composite. Only a few composites pass, hence
1220
- * those which pass are considered probably prime.
1264
+ * Miller-Rabin and similar tests can be more properly called compositeness tests.
1265
+ * Numbers which fail are known to be composite but those which pass might be prime or
1266
+ * might be composite. Only a few composites pass, hence those which pass are considered
1267
+ * probably prime.
1221
1268
  */
1222
1269
  VALUE r_gmpz_is_probab_prime(int argc, VALUE* argv, VALUE self)
1223
1270
  {
@@ -1241,34 +1288,37 @@ VALUE r_gmpz_is_probab_prime(int argc, VALUE* argv, VALUE self)
1241
1288
  * Document-method: nextprime
1242
1289
  *
1243
1290
  * call-seq:
1244
- * integer.nextprime
1245
- * integer.next_prime
1291
+ * n.nextprime
1292
+ * n.next_prime
1246
1293
  *
1247
- * From the GMP Manual:
1248
- *
1249
- * Returns the next prime greater than +integer+.
1294
+ * Returns the next prime greater than _n_.
1250
1295
  *
1251
- * This function uses a probabilistic algorithm to identify primes. For
1252
- * practical purposes it's adequate, the chance of a composite passing will be
1253
- * extremely small.
1296
+ * This function uses a probabilistic algorithm to identify primes. For practical
1297
+ * purposes it's adequate, the chance of a composite passing will be extremely small.
1254
1298
  */
1255
1299
  /*
1256
1300
  * Document-method: nextprime!
1257
1301
  *
1258
1302
  * call-seq:
1259
- * integer.nextprime!
1260
- * integer.next_prime!
1303
+ * n.nextprime!
1304
+ * n.next_prime!
1261
1305
  *
1262
- * From the GMP Manual:
1263
- *
1264
- * Sets +integer+ to the next prime greater than +integer+.
1306
+ * Sets _n_ to the next prime greater than _n_.
1265
1307
  *
1266
- * This function uses a probabilistic algorithm to identify primes. For
1267
- * practical purposes it's adequate, the chance of a composite passing will be
1268
- * extremely small.
1308
+ * This function uses a probabilistic algorithm to identify primes. For practical
1309
+ * purposes it's adequate, the chance of a composite passing will be extremely small.
1269
1310
  */
1270
1311
  DEFUN_INT2INT(nextprime, mpz_nextprime)
1271
1312
 
1313
+ /*
1314
+ * call-seq:
1315
+ * a.gcd(b)
1316
+ *
1317
+ * @since 0.2.11
1318
+ *
1319
+ * Returns the greatest common divisor of a and b. The result is always positive even if
1320
+ * one or both of _a_ or _b_ are negative.
1321
+ */
1272
1322
  VALUE r_gmpz_gcd(VALUE self, VALUE arg)
1273
1323
  {
1274
1324
  MP_INT *self_val, *arg_val, *res_val;
@@ -1293,6 +1343,66 @@ VALUE r_gmpz_gcd(VALUE self, VALUE arg)
1293
1343
  return res;
1294
1344
  }
1295
1345
 
1346
+ /*
1347
+ * call-seq:
1348
+ * a.gcdext(b) #=> g, s, t
1349
+ *
1350
+ * @since 0.5.23
1351
+ *
1352
+ * Returns the greatest common divisor of _a_ and _b_, in addition to _s_ and _t_, the
1353
+ * coefficients satisfying <i>a*s + b*t = g</i>. _g_ is always positive, even if one or
1354
+ * both of _a_ and _b_ are negative. _s_ and _t_ are chosen such that
1355
+ * <i>abs(s) <= abs(b)</i> and <i>abs(t) <= abs(a)</i>.
1356
+ */
1357
+ VALUE r_gmpz_gcdext(VALUE self, VALUE arg)
1358
+ {
1359
+ MP_INT *self_val, *arg_val, *res_val, *s_val, *t_val;
1360
+ VALUE res, s, t, ary;
1361
+ int free_arg_val = 0;
1362
+
1363
+ mpz_get_struct (self,self_val);
1364
+
1365
+ if (GMPZ_P (arg)) {
1366
+ mpz_make_struct_init (res, res_val);
1367
+ mpz_make_struct_init (s, s_val);
1368
+ mpz_make_struct_init (t, t_val);
1369
+ mpz_get_struct (arg, arg_val);
1370
+ mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
1371
+ } else if (FIXNUM_P (arg)) {
1372
+ mpz_make_struct_init (res, res_val);
1373
+ mpz_make_struct_init (s, s_val);
1374
+ mpz_make_struct_init (t, t_val);
1375
+ mpz_temp_alloc(arg_val);
1376
+ mpz_init_set_ui(arg_val, FIX2NUM(arg));
1377
+ free_arg_val = 1;
1378
+ mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
1379
+ } else if (BIGNUM_P (arg)) {
1380
+ mpz_make_struct_init (res, res_val);
1381
+ mpz_make_struct_init (s, s_val);
1382
+ mpz_make_struct_init (t, t_val);
1383
+ mpz_set_bignum (res_val, arg);
1384
+ mpz_gcdext (res_val, s_val, t_val, res_val, self_val);
1385
+ } else {
1386
+ typeerror (ZXB);
1387
+ }
1388
+
1389
+ if (free_arg_val)
1390
+ mpz_temp_free(arg_val);
1391
+
1392
+ ary = rb_ary_new3(3, res, s, t);
1393
+ return ary;
1394
+ }
1395
+
1396
+ /*
1397
+ * call-seq:
1398
+ * a.invert(b)
1399
+ *
1400
+ * @since 0.2.11
1401
+ *
1402
+ * Returns the inverse of _a_ modulo _b_. If the inverse exists, the return value is
1403
+ * non-zero and the result will be non-negative and less than _b_. If an inverse doesn't
1404
+ * exist, the result is undefined.
1405
+ */
1296
1406
  VALUE r_gmpz_invert(VALUE self, VALUE arg)
1297
1407
  {
1298
1408
  MP_INT *self_val, *arg_val, *res_val;
@@ -1320,15 +1430,11 @@ VALUE r_gmpz_invert(VALUE self, VALUE arg)
1320
1430
  }
1321
1431
 
1322
1432
  /*
1323
- * Document-method: jacobi
1324
- *
1325
1433
  * call-seq:
1326
1434
  * a.jacobi(b)
1327
1435
  *
1328
- * From the GMP Manual:
1329
- *
1330
- * Calculate the Jacobi symbol <tt>(a/b)</tt>. This is defined only for +b+
1331
- * odd and positive.
1436
+ * Calculate the Jacobi symbol <i>(a/b)</i>. This is defined only for _b_ odd and
1437
+ * positive.
1332
1438
  */
1333
1439
  VALUE r_gmpz_jacobi(VALUE self, VALUE b)
1334
1440
  {
@@ -1345,15 +1451,11 @@ VALUE r_gmpz_jacobi(VALUE self, VALUE b)
1345
1451
  }
1346
1452
 
1347
1453
  /*
1348
- * Document-method: GMP::Z.jacobi
1349
- *
1350
1454
  * call-seq:
1351
1455
  * GMP::Z.jacobi(a, b)
1352
- *
1353
- * From the GMP Manual:
1354
1456
  *
1355
- * Calculate the Jacobi symbol <tt>(a/b)</tt>. This is defined only for +b+
1356
- * odd and positive.
1457
+ * Calculate the Jacobi symbol <i>(a/b)</i>. This is defined only for _b_ odd and
1458
+ * positive.
1357
1459
  */
1358
1460
  VALUE r_gmpzsg_jacobi(VALUE klass, VALUE a, VALUE b)
1359
1461
  {
@@ -1412,15 +1514,11 @@ VALUE r_gmpzsg_jacobi(VALUE klass, VALUE a, VALUE b)
1412
1514
  }
1413
1515
 
1414
1516
  /*
1415
- * Document-method: legendre
1416
- *
1417
1517
  * call-seq:
1418
1518
  * a.legendre(p)
1419
1519
  *
1420
- * From the GMP Manual:
1421
- *
1422
- * Calculate the Legendre symbol <tt>(a/p)</tt>. This is defined only for +p+
1423
- * an odd positive prime, and for such +p+ it's identical to the Jacobi symbol.
1520
+ * Calculate the Legendre symbol <i>(a/p)</i>. This is defined only for _p_ an odd
1521
+ * positive prime, and for such _p_ it's identical to the Jacobi symbol.
1424
1522
  */
1425
1523
  VALUE r_gmpz_legendre(VALUE self, VALUE p)
1426
1524
  {
@@ -1439,15 +1537,11 @@ VALUE r_gmpz_legendre(VALUE self, VALUE p)
1439
1537
  }
1440
1538
 
1441
1539
  /*
1442
- * Document-method: remove
1443
- *
1444
1540
  * call-seq:
1445
- * integer.remove(factor)
1541
+ * n.remove(f) #=> r, t
1446
1542
  *
1447
- * From the GMP Manual:
1448
- *
1449
- * Remove all occurrences of the factor +factor+ from +integer+. The return
1450
- * value is how many such occurrences were removed.
1543
+ * Remove all occurrences of the factor _f_ from _n_, returning the result as _r_. _t_,
1544
+ * how many such occurrences were removed, is also returned.
1451
1545
  */
1452
1546
  VALUE r_gmpz_remove(VALUE self, VALUE arg)
1453
1547
  {
@@ -1496,17 +1590,14 @@ VALUE r_gmpz_remove(VALUE self, VALUE arg)
1496
1590
  * call-seq:
1497
1591
  * GMP::Z.fac(n)
1498
1592
  *
1499
- * From the GMP Manual:
1500
- *
1501
- * Returns <tt>n!</tt>, the factorial of +n+.
1593
+ * Returns <i>n!</i>, the factorial of _n_.
1502
1594
  *
1503
1595
  * Examples:
1504
- *
1505
- * GMP::Z.fac(0) #=> 1
1506
- * GMP::Z.fac(1) #=> 1
1507
- * GMP::Z.fac(2) #=> 2
1508
- * GMP::Z.fac(3) #=> 6
1509
- * GMP::Z.fac(4) #=> 24
1596
+ * * GMP::Z.fac(0) #=> 1
1597
+ * * GMP::Z.fac(1) #=> 1
1598
+ * * GMP::Z.fac(2) #=> 2
1599
+ * * GMP::Z.fac(3) #=> 6
1600
+ * * GMP::Z.fac(4) #=> 24
1510
1601
  */
1511
1602
  DEFUN_INT_SINGLETON_UI(fac, mpz_fac_ui)
1512
1603
  /*
@@ -1515,19 +1606,16 @@ DEFUN_INT_SINGLETON_UI(fac, mpz_fac_ui)
1515
1606
  * call-seq:
1516
1607
  * GMP::Z.fib(n)
1517
1608
  *
1518
- * From the GMP Manual:
1519
- *
1520
- * Returns <tt>F[n]</tt>, the +n+th Fibonacci number.
1609
+ * Returns <i>F[n]</i>, the <i>n</i>th Fibonacci number.
1521
1610
  *
1522
1611
  * Examples:
1523
- *
1524
- * GMP::Z.fib(1) #=> 1
1525
- * GMP::Z.fib(2) #=> 1
1526
- * GMP::Z.fib(3) #=> 2
1527
- * GMP::Z.fac(4) #=> 3
1528
- * GMP::Z.fac(5) #=> 5
1529
- * GMP::Z.fac(6) #=> 8
1530
- * GMP::Z.fac(7) #=> 13
1612
+ * * GMP::Z.fib(1) #=> 1
1613
+ * * GMP::Z.fib(2) #=> 1
1614
+ * * GMP::Z.fib(3) #=> 2
1615
+ * * GMP::Z.fac(4) #=> 3
1616
+ * * GMP::Z.fac(5) #=> 5
1617
+ * * GMP::Z.fac(6) #=> 8
1618
+ * * GMP::Z.fac(7) #=> 13
1531
1619
  */
1532
1620
  DEFUN_INT_SINGLETON_UI(fib, mpz_fib_ui)
1533
1621
 
@@ -1536,6 +1624,12 @@ DEFUN_INT_SINGLETON_UI(fib, mpz_fib_ui)
1536
1624
  * Integer Comparisons *
1537
1625
  **********************************************************************/
1538
1626
 
1627
+ /*
1628
+ * call-seq:
1629
+ * a == b
1630
+ *
1631
+ * Returns true if _a_ is equal to _b_, and false otherwise.
1632
+ */
1539
1633
  VALUE r_gmpz_eq(VALUE self, VALUE arg)
1540
1634
  {
1541
1635
  MP_INT *self_val, *arg_val_z;
@@ -1597,6 +1691,16 @@ int mpz_cmp_value(MP_INT *OP, VALUE arg)
1597
1691
  }
1598
1692
  }
1599
1693
 
1694
+ /*
1695
+ * call-seq:
1696
+ * a <=> b
1697
+ *
1698
+ * Returns negative if _a_ is less than _b_.
1699
+ *
1700
+ * Returns 0 if _a_ is equal to _b_.
1701
+ *
1702
+ * Returns positive if _a_ is greater than _b_.
1703
+ */
1600
1704
  VALUE r_gmpz_cmp(VALUE self, VALUE arg)
1601
1705
  {
1602
1706
  MP_INT *self_val;
@@ -1615,39 +1719,49 @@ VALUE r_gmpz_cmp(VALUE self, VALUE arg)
1615
1719
  * Document-method: <
1616
1720
  *
1617
1721
  * call-seq:
1618
- * int1 < int2
1722
+ * a < b
1619
1723
  *
1620
- * Returns whether +int1+ is strictly less than +int2+.
1724
+ * Returns whether _a_ is strictly less than _b_.
1621
1725
  */
1622
1726
  DEFUN_INT_CMP(lt,<)
1623
1727
  /*
1624
1728
  * Document-method: <=
1625
1729
  *
1626
1730
  * call-seq:
1627
- * int1 <= int2
1731
+ * a <= b
1628
1732
  *
1629
- * Returns whether +int1+ is less than or equal to +int2+.
1733
+ * Returns whether _a_ is less than or equal to _b_.
1630
1734
  */
1631
1735
  DEFUN_INT_CMP(le,<=)
1632
1736
  /*
1633
1737
  * Document-method: >
1634
1738
  *
1635
1739
  * call-seq:
1636
- * int1 > int2
1740
+ * a > b
1637
1741
  *
1638
- * Returns whether +int1+ is strictly greater than +int2+.
1742
+ * Returns whether _a_ is strictly greater than _b_.
1639
1743
  */
1640
1744
  DEFUN_INT_CMP(gt,>)
1641
1745
  /*
1642
1746
  * Document-method: >=
1643
1747
  *
1644
1748
  * call-seq:
1645
- * int1 >= int2
1749
+ * a >= b
1646
1750
  *
1647
- * Returns whether +int1+ is greater than or equal to +int2+.
1751
+ * Returns whether _a_ is greater than or equal to _b_.
1648
1752
  */
1649
1753
  DEFUN_INT_CMP(ge,>=)
1650
1754
 
1755
+ /*
1756
+ * call-seq:
1757
+ * a.cmpabs(b)
1758
+ *
1759
+ * Returns negative if abs(_a_) is less than abs(_b_).
1760
+ *
1761
+ * Returns 0 if abs(_a_) is equal to abs(_b_).
1762
+ *
1763
+ * Returns positive if abs(_a_) is greater than abs(_b_).
1764
+ */
1651
1765
  VALUE r_gmpz_cmpabs(VALUE self, VALUE arg)
1652
1766
  {
1653
1767
  MP_INT *arg_val_z, *self_val;
@@ -1686,11 +1800,9 @@ VALUE r_gmpz_cmpabs(VALUE self, VALUE arg)
1686
1800
 
1687
1801
  /*
1688
1802
  * call-seq:
1689
- * integer.sgn
1803
+ * a.sgn
1690
1804
  *
1691
- * From the GMP Manual:
1692
- *
1693
- * Returns +1 if +integer+ > 0, 0 if +integer+ == 0, and -1 if +integer+ < 0.
1805
+ * Returns +1 if _a_ > 0, 0 if _a_ == 0, and -1 if _a_ < 0.
1694
1806
  */
1695
1807
  VALUE r_gmpz_sgn(VALUE self)
1696
1808
  {
@@ -1699,10 +1811,15 @@ VALUE r_gmpz_sgn(VALUE self)
1699
1811
  return INT2FIX(mpz_sgn(self_val));
1700
1812
  }
1701
1813
 
1702
-
1703
1814
  /*
1704
- * eql? and hash are defined so that GMP::Z objects can be used as keys in
1705
- * hashes.
1815
+ * call-seq:
1816
+ * a.eql?(b)
1817
+ *
1818
+ * @since 0.4.7
1819
+ *
1820
+ * Returns true if _a_ is equal to _b_. _a_ and _b_ must then be equal in cardinality,
1821
+ * and both be instances of GMP::Z. Otherwise, returns false. a.eql?(b) if and only if
1822
+ * a.hash == b.hash.
1706
1823
  */
1707
1824
  VALUE r_gmpz_eql(VALUE self, VALUE arg)
1708
1825
  {
@@ -1718,6 +1835,16 @@ VALUE r_gmpz_eql(VALUE self, VALUE arg)
1718
1835
  }
1719
1836
  }
1720
1837
 
1838
+ /*
1839
+ * call-seq:
1840
+ * a.hash
1841
+ *
1842
+ * @since 0.4.7
1843
+ *
1844
+ * Returns the computed hash value of _a_. This method first converts _a_ into a String
1845
+ * (base 10), then calls String#hash on the result, returning the hash value. a.eql?(b)
1846
+ * if and only if a.hash == b.hash.
1847
+ */
1721
1848
  VALUE r_gmpz_hash(VALUE self)
1722
1849
  {
1723
1850
  ID to_s_sym = rb_intern("to_s");
@@ -1734,33 +1861,36 @@ VALUE r_gmpz_hash(VALUE self)
1734
1861
  * Document-method: &
1735
1862
  *
1736
1863
  * call-seq:
1737
- * integer & other
1864
+ * a & b
1738
1865
  *
1739
- * From the GMP Manual:
1740
- *
1741
- * Returns +integer+ bitwise-and +other+.
1866
+ * Returns _a_ bitwise-and _b_. _b_ must be an instance of one of the following:
1867
+ * * GMP::Z
1868
+ * * Fixnum
1869
+ * * Bignum
1742
1870
  */
1743
1871
  DEFUN_INT_LOGIC(and, mpz_and)
1744
1872
  /*
1745
1873
  * Document-method: |
1746
1874
  *
1747
1875
  * call-seq:
1748
- * integer | other
1876
+ * a | b
1749
1877
  *
1750
- * From the GMP Manual:
1751
- *
1752
- * Returns +integer+ bitwise inclusive-or +other+.
1878
+ * Returns _a_ bitwise inclusive-or _b_. _b_ must be an instance of one of the following:
1879
+ * * GMP::Z
1880
+ * * Fixnum
1881
+ * * Bignum
1753
1882
  */
1754
1883
  DEFUN_INT_LOGIC(or, mpz_ior)
1755
1884
  /*
1756
1885
  * Document-method: ^
1757
1886
  *
1758
1887
  * call-seq:
1759
- * integer ^ other
1888
+ * a ^ b
1760
1889
  *
1761
- * From the GMP Manual:
1762
- *
1763
- * Returns +integer+ bitwise exclusive-or +other+.
1890
+ * Returns _a_ bitwise exclusive-or _b_. _b_ must be an instance of one of the following:
1891
+ * * GMP::Z
1892
+ * * Fixnum
1893
+ * * Bignum
1764
1894
  */
1765
1895
  DEFUN_INT_LOGIC(xor, mpz_xor)
1766
1896
 
@@ -1768,21 +1898,17 @@ DEFUN_INT_LOGIC(xor, mpz_xor)
1768
1898
  * Document-method: com
1769
1899
  *
1770
1900
  * call-seq:
1771
- * integer.com
1901
+ * a.com
1772
1902
  *
1773
- * From the GMP Manual:
1774
- *
1775
- * Returns the one's complement of +integer+.
1903
+ * Returns the one's complement of _a_.
1776
1904
  */
1777
1905
  /*
1778
1906
  * Document-method: com!
1779
1907
  *
1780
1908
  * call-seq:
1781
- * integer.com!
1909
+ * a.com!
1782
1910
  *
1783
- * From the GMP Manual:
1784
- *
1785
- * Sets +integer+ to its one's complement.
1911
+ * Sets _a_ to its one's complement.
1786
1912
  */
1787
1913
  DEFUN_INT2INT(com, mpz_com)
1788
1914
 
@@ -1790,14 +1916,11 @@ DEFUN_INT2INT(com, mpz_com)
1790
1916
  * Document-method: popcount
1791
1917
  *
1792
1918
  * call-seq:
1793
- * integer.popcount
1919
+ * a.popcount
1794
1920
  *
1795
- * From the GMP Manual:
1796
- *
1797
- * If <tt>integer>=0</tt>, return the population count of <tt>integer</tt>,
1798
- * which is the number of 1 bits in the binary representation. If
1799
- * <tt>op<0</tt>, the number of 1s is infinite, and the return value is
1800
- * INT2FIX(ULONG_MAX), the largest possible unsigned long.
1921
+ * If _a_ >= 0, return the population count of _a_, which is the number of 1 bits in the
1922
+ * binary representation. If _a_ < 0, the number of 1s is infinite, and the return value
1923
+ * is INT2FIX(ULONG_MAX), the largest possible unsigned long.
1801
1924
  */
1802
1925
  VALUE r_gmpz_popcount(VALUE self)
1803
1926
  {
@@ -1808,18 +1931,16 @@ VALUE r_gmpz_popcount(VALUE self)
1808
1931
 
1809
1932
  /*
1810
1933
  * call-seq:
1811
- * integer.scan0(starting_bit)
1934
+ * a.scan0(starting_bit)
1812
1935
  *
1813
- * From the GMP Manual:
1814
- *
1815
- * Scan integer, starting from bit starting_bit, towards more significant bits,
1816
- * until the first 0 bit is found. Return the index of the found bit.
1936
+ * Scan _a_, starting from bit _starting_bit_, towards more significant bits, until the
1937
+ * first 0 bit is found. Return the index of the found bit.
1817
1938
  *
1818
- * If the bit at starting_bit is already what's sought, then starting_bit is
1939
+ * If the bit at _starting_bit_ is already what's sought, then _starting_bit_ is
1819
1940
  * returned.
1820
1941
  *
1821
- * If there's no bit found, then INT2FIX(ULONG_MAX) is returned. This will
1822
- * happen in scan0 past the end of a negative number.
1942
+ * If there's no bit found, then INT2FIX(ULONG_MAX) is returned. This will happen in
1943
+ * scan0 past the end of a negative number.
1823
1944
  */
1824
1945
  VALUE r_gmpz_scan0(VALUE self, VALUE bitnr)
1825
1946
  {
@@ -1836,18 +1957,16 @@ VALUE r_gmpz_scan0(VALUE self, VALUE bitnr)
1836
1957
 
1837
1958
  /*
1838
1959
  * call-seq:
1839
- * integer.scan1(starting_bit)
1960
+ * a.scan1(starting_bit)
1840
1961
  *
1841
- * From the GMP Manual:
1962
+ * Scan _a_, starting from bit _starting_bit_, towards more significant bits, until the
1963
+ * first 1 bit is found. Return the index of the found bit.
1842
1964
  *
1843
- * Scan integer, starting from bit starting_bit, towards more significant bits,
1844
- * until the first 1 bit is found. Return the index of the found bit.
1845
- *
1846
- * If the bit at starting_bit is already what's sought, then starting_bit is
1965
+ * If the bit at _starting_bit_ is already what's sought, then _starting_bit_ is
1847
1966
  * returned.
1848
1967
  *
1849
- * If there's no bit found, then INT2FIX(ULONG_MAX) is returned. This will
1850
- * happen in mpz_scan1 past the end of a nonnegative number.
1968
+ * If there's no bit found, then INT2FIX(ULONG_MAX) is returned. This will happen in
1969
+ * scan1 past the end of a nonnegative number.
1851
1970
  */
1852
1971
  VALUE r_gmpz_scan1(VALUE self, VALUE bitnr)
1853
1972
  {
@@ -1867,9 +1986,9 @@ VALUE r_gmpz_scan1(VALUE self, VALUE bitnr)
1867
1986
 
1868
1987
  /*
1869
1988
  * call-seq:
1870
- * integer[index] = x &rarr; nil
1989
+ * a[index] = x
1871
1990
  *
1872
- * Sets the bit at index to x.
1991
+ * Sets the bit at _index_ to _x_.
1873
1992
  */
1874
1993
  VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to)
1875
1994
  {
@@ -1896,9 +2015,9 @@ VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to)
1896
2015
 
1897
2016
  /*
1898
2017
  * call-seq:
1899
- * integer[index] &rarr; boolean
2018
+ * a[index] #=> boolean
1900
2019
  *
1901
- * Gets the bit at index, returned as either true or false.
2020
+ * Gets the bit at _index_, returned as either true or false.
1902
2021
  */
1903
2022
  VALUE r_gmpz_getbit(VALUE self, VALUE bitnr)
1904
2023
  {
@@ -1922,25 +2041,33 @@ VALUE r_gmpz_getbit(VALUE self, VALUE bitnr)
1922
2041
  * Document-method: even?
1923
2042
  *
1924
2043
  * call-seq:
1925
- * integer.even?
2044
+ * a.even?
1926
2045
  *
1927
- * From the GMP Manual:
1928
- *
1929
- * Determines whether integer is even. Returns true or false.
2046
+ * Determines whether _a_ is even. Returns true or false.
1930
2047
  */
1931
2048
  DEFUN_INT_COND_P(is_even,mpz_even_p)
1932
2049
  /*
1933
2050
  * Document-method: odd?
1934
2051
  *
1935
2052
  * call-seq:
1936
- * integer.odd?
2053
+ * a.odd?
1937
2054
  *
1938
- * From the GMP Manual:
1939
- *
1940
- * Determines whether integer is odd. Returns true or false.
2055
+ * Determines whether _a_ is odd. Returns true or false.
1941
2056
  */
1942
2057
  DEFUN_INT_COND_P(is_odd,mpz_odd_p)
1943
2058
 
2059
+ /*
2060
+ * call-seq:
2061
+ * a.sizeinbase
2062
+ * a.size_in_base
2063
+ *
2064
+ * @since 0.2.11
2065
+ *
2066
+ * Return the size of _a_ measured in number of digits in the given base. base can vary
2067
+ * from 2 to 62. The sign of _a_ is ignored, just the absolute value is used. The result
2068
+ * will be either exact or 1 too big. If base is a power of 2, the result is always
2069
+ * exact. If _a_ is zero the return value is always 1.
2070
+ */
1944
2071
  VALUE r_gmpz_sizeinbase(VALUE self, VALUE base)
1945
2072
  {
1946
2073
  MP_INT *self_val;
@@ -1950,6 +2077,15 @@ VALUE r_gmpz_sizeinbase(VALUE self, VALUE base)
1950
2077
  return INT2FIX (mpz_sizeinbase (self_val, base_val));
1951
2078
  }
1952
2079
 
2080
+ /*
2081
+ * call-seq:
2082
+ * a.size_in_bin
2083
+ *
2084
+ * @since 0.2.11
2085
+ *
2086
+ * Return the size of _a_ measured in number of digits in binary. The sign of _a_ is
2087
+ * ignored, just the absolute value is used. If _a_ is zero the return value is 1.
2088
+ */
1953
2089
  VALUE r_gmpz_size_in_bin(VALUE self)
1954
2090
  {
1955
2091
  MP_INT *self_val;
@@ -1963,15 +2099,13 @@ VALUE r_gmpz_size_in_bin(VALUE self)
1963
2099
  **********************************************************************/
1964
2100
 
1965
2101
  /*
1966
- * Document-method: size
1967
- *
1968
2102
  * call-seq:
1969
- * integer.size
2103
+ * a.size
1970
2104
  *
1971
- * From the GMP Manual:
1972
- *
1973
- * Return the size of <tt>integer</tt> measured in number of limbs. If
1974
- * <tt>integer</tt> is zero, the returned value will be zero.
2105
+ * @since 0.4.19
2106
+ *
2107
+ * Return the size of _a_ measured in number of limbs. If _a_ is zero, the returned
2108
+ * value will be zero.
1975
2109
  */
1976
2110
  VALUE r_gmpz_size(VALUE self)
1977
2111
  {
@@ -1985,28 +2119,10 @@ VALUE r_gmpz_size(VALUE self)
1985
2119
  * _unsorted_ *
1986
2120
  **********************************************************************/
1987
2121
 
1988
- DEFUN_INT_F_UL(fshr,mpz_fdiv_q_2exp,"shift size")
1989
- DEFUN_INT_F_UL(tshr,mpz_tdiv_q_2exp,"shift size")
1990
- DEFUN_INT_F_UL(fshrm,mpz_fdiv_r_2exp,"mark size")
1991
- DEFUN_INT_F_UL(tshrm,mpz_tdiv_r_2exp,"mark size")
1992
-
1993
- VALUE r_gmpzsg_pow(VALUE klass, VALUE base, VALUE exp)
1994
- {
1995
- MP_INT *res_val;
1996
- VALUE res;
1997
2122
 
1998
- if (FIXNUM_P(base) && FIXNUM_P(exp))
1999
- {
2000
- if (FIX2NUM(base) < 0)
2001
- rb_raise (rb_eRangeError, "base must not be negative");
2002
- if (FIX2NUM(exp) < 0)
2003
- rb_raise (rb_eRangeError, "exponent must not be negative");
2004
- mpz_make_struct_init (res, res_val);
2005
- mpz_ui_pow_ui (res_val, FIX2NUM(base), FIX2NUM(exp));
2006
- return res;
2007
- }
2008
- return r_gmpz_pow (r_gmpzsg_new(1, &base, klass), exp);
2009
- }
2123
+ /**********************************************************************
2124
+ * Init function *
2125
+ **********************************************************************/
2010
2126
 
2011
2127
  void init_gmpz()
2012
2128
  {
@@ -2017,8 +2133,8 @@ void init_gmpz()
2017
2133
 
2018
2134
  // Initializing, Assigning Integers
2019
2135
  rb_define_singleton_method(cGMP_Z, "new", r_gmpzsg_new, -1);
2020
- rb_define_method(cGMP_Z, "initialize", r_gmpz_initialize, -1);
2021
- rb_define_method(cGMP_Z, "swap", r_gmpz_swap, 1);
2136
+ rb_define_method(cGMP_Z, "initialize", r_gmpz_initialize, -1);
2137
+ rb_define_method(cGMP_Z, "swap", r_gmpz_swap, 1);
2022
2138
 
2023
2139
  // Converting Integers
2024
2140
  rb_define_method(cGMP_Z, "to_i", r_gmpz_to_i, 0);
@@ -2032,6 +2148,7 @@ void init_gmpz()
2032
2148
  rb_define_method(cGMP_Z, "sub!", r_gmpz_sub_self, 1);
2033
2149
  rb_define_method(cGMP_Z, "*", r_gmpz_mul, 1);
2034
2150
  rb_define_method(cGMP_Z, "addmul!", r_gmpz_addmul_self, 2);
2151
+ rb_define_method(cGMP_Z, "submul!", r_gmpz_submul_self, 2);
2035
2152
  rb_define_method(cGMP_Z, "<<", r_gmpz_shl, 1);
2036
2153
  rb_define_method(cGMP_Z, "neg", r_gmpz_neg, 0);
2037
2154
  rb_define_method(cGMP_Z, "neg!", r_gmpz_neg_self, 0);
@@ -2040,14 +2157,17 @@ void init_gmpz()
2040
2157
  rb_define_method(cGMP_Z, "abs!", r_gmpz_abs_self, 0);
2041
2158
 
2042
2159
  // Integer Division
2043
- rb_define_method(cGMP_Z, "/", r_gmpz_div, 1);
2044
- rb_define_method(cGMP_Z, "tdiv", r_gmpz_tdiv, 1);
2045
- rb_define_method(cGMP_Z, "tmod", r_gmpz_tmod, 1);
2046
- rb_define_method(cGMP_Z, "fdiv", r_gmpz_fdiv, 1);
2047
- rb_define_method(cGMP_Z, "fmod", r_gmpz_fmod, 1);
2048
- rb_define_method(cGMP_Z, "cdiv", r_gmpz_cdiv, 1);
2049
- rb_define_method(cGMP_Z, "cmod", r_gmpz_cmod, 1);
2050
- rb_define_method(cGMP_Z, "%", r_gmpz_mod, 1);
2160
+ rb_define_method(cGMP_Z, "/", r_gmpz_div, 1);
2161
+ rb_define_method(cGMP_Z, "tdiv", r_gmpz_tdiv, 1);
2162
+ rb_define_method(cGMP_Z, "tmod", r_gmpz_tmod, 1);
2163
+ rb_define_method(cGMP_Z, "tshr", r_gmpz_tshr, 1);
2164
+ rb_define_method(cGMP_Z, "lastbits_sgn", r_gmpz_tshrm, 1);
2165
+ rb_define_method(cGMP_Z, "fdiv", r_gmpz_fdiv, 1);
2166
+ rb_define_method(cGMP_Z, "fmod", r_gmpz_fmod, 1);
2167
+ rb_define_method(cGMP_Z, "cdiv", r_gmpz_cdiv, 1);
2168
+ rb_define_method(cGMP_Z, "cmod", r_gmpz_cmod, 1);
2169
+ rb_define_method(cGMP_Z, "%", r_gmpz_mod, 1);
2170
+ rb_define_method(cGMP_Z, "divisible?", r_gmpz_divisible, 1);
2051
2171
 
2052
2172
  // Integer Exponentiation
2053
2173
  rb_define_singleton_method(cGMP_Z, "pow", r_gmpzsg_pow, 2);
@@ -2069,6 +2189,7 @@ void init_gmpz()
2069
2189
  rb_define_alias( cGMP_Z, "next_prime", "nextprime");
2070
2190
  rb_define_alias( cGMP_Z, "next_prime!", "nextprime!");
2071
2191
  rb_define_method( cGMP_Z, "gcd", r_gmpz_gcd, 1);
2192
+ rb_define_method( cGMP_Z, "gcdext", r_gmpz_gcdext, 1);
2072
2193
  rb_define_method( cGMP_Z, "invert", r_gmpz_invert, 1);
2073
2194
  rb_define_method( cGMP_Z, "jacobi", r_gmpz_jacobi, 1);
2074
2195
  rb_define_singleton_method(cGMP_Z, "jacobi", r_gmpzsg_jacobi, 2);
@@ -2114,8 +2235,6 @@ void init_gmpz()
2114
2235
 
2115
2236
  // _unsorted_
2116
2237
  rb_define_method(cGMP_Z, ">>", r_gmpz_fshr, 1);
2117
- rb_define_method(cGMP_Z, "tshr", r_gmpz_tshr, 1);
2118
- rb_define_method(cGMP_Z, "lastbits_sgn", r_gmpz_tshrm, 1);
2119
2238
  rb_define_method(cGMP_Z, "lastbits_pos", r_gmpz_fshrm, 1);
2120
2239
 
2121
2240
  }