Class: GMP::Q

Inherits:
Numeric
  • Object
show all
Defined in:
ext/gmpq.c,
ext/gmp.c,
ext/gmpq.c

Overview

GMP Multiple Precision Rational Number.

Instances of this class can store variables of the type ‘mpq_t`. This class also contains many methods that act as the functions for `mpq_t` variables, as well as a few methods that attempt to make this library more Ruby-ish.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Class Method Details

.GMP::Q.new(arg) ⇒ Object .GMP::Q.new(num, den) ⇒ Object

Creates a new GMP::Q rational, with arg as its value, converting where necessary.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/gmpq.c', line 77

VALUE r_gmpqsg_new(int argc, VALUE *argv, VALUE klass)
{
  MP_RAT *res_val;
  VALUE res;

  (void)klass;

  if (argc > 2)
    rb_raise (rb_eArgError, "wrong number of arguments (%d for 0, 1 or 2)", argc);

  mpq_make_struct (res, res_val);
  mpq_init (res_val);
  rb_obj_call_init (res, argc, argv);

  return res;
}

Instance Method Details

#*(q) ⇒ Object

Multiplies p with q. q must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'ext/gmpq.c', line 392

VALUE r_gmpq_mul(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val_q, *res_val;
  MP_INT *arg_val_z, *tmp_z;
  VALUE res;
#if GMP >= 4
  unsigned long tmp_ui;
#endif

  mpq_get_struct(self, self_val);
  mpq_make_struct_init(res, res_val);

  if (GMPQ_P(arg)) {
    mpq_get_struct(arg,arg_val_q);
    mpq_mul(res_val, self_val, arg_val_q);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val_z);
    mpz_temp_init(tmp_z);
    mpz_gcd(tmp_z, mpq_denref(self_val), arg_val_z);
    mpz_divexact(mpq_denref(res_val), mpq_denref(self_val), tmp_z);
    mpz_divexact(mpq_numref(res_val), arg_val_z, tmp_z);
    mpz_mul(mpq_numref(res_val), mpq_numref(res_val), mpq_numref(self_val));
    mpz_temp_free(tmp_z);
  } else if (FIXNUM_P(arg)) {
#if GMP >= 4
    if (FIX2NUM(arg) > 0) {
      tmp_ui = mpz_gcd_ui(0, mpq_denref(self_val), FIX2NUM(arg));
    } else if (FIX2NUM(arg) < 0) {
      tmp_ui = mpz_gcd_ui(0, mpq_denref(self_val), -FIX2NUM(arg));
    } else {
      mpz_set_ui(mpq_numref(res_val), 0);
      mpz_set_ui(mpq_denref(res_val), 1);
      return res;
    }
    mpz_divexact_ui(mpq_denref(res_val), mpq_denref(self_val), tmp_ui);
    mpz_mul_ui(mpq_numref(res_val), mpq_numref(self_val), FIX2NUM(arg)/tmp_ui);
#else
    mpz_set(mpq_denref(res_val), mpq_denref(self_val));
    mpz_mul_si(mpq_numref(res_val), mpq_numref(self_val), FIX2NUM(arg));
    mpq_canonicalize(res_val);
#endif
  } else if (GMPF_P(arg)) {
#ifndef MPFR
    return r_gmpf_mul(arg, self);
#else
    return rb_funcall(arg, rb_intern("*"), 1, self);
#endif
  } else if (BIGNUM_P(arg)) {
    mpz_temp_alloc(tmp_z);
    mpz_set_bignum(tmp_z, arg);
    mpz_gcd(mpq_denref(res_val), mpq_denref(self_val), tmp_z);
    mpz_divexact(mpq_numref(res_val), tmp_z, mpq_denref(res_val));
    mpz_divexact(mpq_denref(res_val), mpq_denref(self_val), mpq_denref(res_val));
    mpz_mul(mpq_numref(res_val), mpq_numref(res_val), mpq_numref(self_val));
    mpz_temp_free(tmp_z);
  } else {
    typeerror(ZQFXB);
  }
  return res;
}

#+(q) ⇒ Object

Adds p to q. q must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'ext/gmpq.c', line 281

VALUE r_gmpq_add(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val_q, *res_val;
  MP_INT *arg_val_z, *res_val_num;
  VALUE res;

  mpq_get_struct (self, self_val);
  mpq_make_struct_init (res, res_val);

  if (GMPQ_P (arg)) {
    mpq_get_struct (arg,arg_val_q);
    mpq_add (res_val, self_val, arg_val_q);
  } else if (GMPZ_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_get_struct (arg, arg_val_z);
    mpz_mul (res_val_num, mpq_denref (self_val), arg_val_z);
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else if (FIXNUM_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_mul_si (res_val_num, mpq_denref (self_val), FIX2NUM (arg));
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else if (GMPF_P (arg)) {
#ifndef MPFR
    return r_gmpf_add (arg,self);
#else
    return rb_funcall (arg, rb_intern ("+"), 1, self);
#endif
  } else if (BIGNUM_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_set_bignum (res_val_num, arg);
    mpz_mul (res_val_num, res_val_num, mpq_denref (self_val));
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else {
    typeerror (ZQFXB);
  }
  return res;
}

#-(q) ⇒ Object

Subtracts p from q. q must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'ext/gmpq.c', line 335

VALUE r_gmpq_sub(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val_q, *res_val;
  MP_INT *arg_val_z, *res_val_num;
  MP_FLOAT *arg_val_f, *res_val_f;
  VALUE res;
  mpfr_prec_t prec;

  mpq_get_struct (self, self_val);
  mpq_make_struct_init (res, res_val);

  if (GMPQ_P (arg)) {
    mpq_get_struct (arg,arg_val_q);
    mpq_sub (res_val, self_val, arg_val_q);
  } else if (GMPZ_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_get_struct (arg, arg_val_z);
    mpz_mul (res_val_num, mpq_denref (self_val), arg_val_z);
    mpz_neg (res_val_num, res_val_num);
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else if (FIXNUM_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_mul_si (res_val_num, mpq_denref (self_val), -FIX2NUM (arg));
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else if (GMPF_P (arg)) {
    mpf_get_struct_prec (arg, arg_val_f, prec);
    mpf_make_struct_init (res, res_val_f, prec);
    mpf_set_q (res_val_f, self_val);
    mpf_sub (res_val_f, res_val_f, arg_val_f);
  } else if (BIGNUM_P (arg)) {
    res_val_num = mpq_numref (res_val);
    mpz_set (mpq_denref (res_val), mpq_denref (self_val));
    mpz_set_bignum (res_val_num, arg);
    mpz_mul (res_val_num, res_val_num, mpq_denref (self_val));
    mpz_neg (res_val_num, res_val_num);
    mpz_add (res_val_num, res_val_num, mpq_numref (self_val));
  } else {
    typeerror (ZQFXB);
  }
  return res;
}

#-@Object Also known as: neg

TODO rb_define_method(cGMP_Q, “div_2exp”, r_gmpq_div_2exp, 1);

#/(q) ⇒ Object

Divides p by q. q must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'ext/gmpq.c', line 466

VALUE r_gmpq_div(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val_q, *res_val;
  MP_INT *arg_val_z, *tmp_z;
  MP_FLOAT *arg_val_f, *res_val_f;
  VALUE res;
  unsigned long tmp_ui, prec;

  mpq_get_struct(self, self_val);
  mpq_make_struct_init(res, res_val);

  if (GMPQ_P(arg)) {
    mpq_get_struct(arg,arg_val_q);
    if (mpz_sgn(mpq_numref(arg_val_q)) == 0)
      rb_raise(rb_eZeroDivError, "divided by 0");
    mpq_div(res_val, self_val, arg_val_q);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val_z);
    mpz_temp_init(tmp_z);
    mpz_gcd(tmp_z, mpq_numref(self_val), arg_val_z);
    mpz_divexact(mpq_numref(res_val), mpq_numref(self_val), tmp_z);
    mpz_divexact(mpq_denref(res_val), arg_val_z, tmp_z);
    mpz_mul(mpq_denref(res_val), mpq_denref(res_val), mpq_denref(self_val));
    mpz_temp_free(tmp_z);
  } else if (FIXNUM_P(arg)) {
    if (FIX2NUM(arg) == 0)
      rb_raise(rb_eZeroDivError, "divided by 0");
    if (FIX2NUM(arg) > 0) {
      tmp_ui = mpz_gcd_ui(0, mpq_numref(self_val), FIX2NUM(arg));
    } else {
      tmp_ui = mpz_gcd_ui(0, mpq_numref(self_val), -FIX2NUM(arg));
    }
    mpz_divexact_ui(mpq_numref(res_val), mpq_numref(self_val), tmp_ui);
    mpz_mul_ui(mpq_denref(res_val), mpq_denref(self_val), FIX2NUM(arg)/tmp_ui);
  } else if (GMPF_P(arg)) {
    mpf_get_struct_prec(arg, arg_val_f, prec);
    mpf_make_struct_init(res, res_val_f, prec);
    mpf_set_q(res_val_f, self_val);
    mpf_div(res_val_f, res_val_f, arg_val_f);
  } else if (BIGNUM_P(arg)) {
    mpz_temp_alloc(tmp_z);
    mpz_set_bignum(tmp_z, arg);
    mpz_gcd(mpq_numref(res_val), mpq_numref(self_val), tmp_z);
    mpz_divexact(mpq_denref(res_val), tmp_z, mpq_numref(res_val));
    mpz_divexact(mpq_numref(res_val), mpq_numref(self_val), mpq_numref(res_val));
    mpz_mul(mpq_denref(res_val), mpq_denref(res_val), mpq_denref(self_val));
    mpz_temp_free(tmp_z);
  } else {
    typeerror(ZQFXB);
  }
  return res;
}

#<(b) ⇒ Object

Returns whether a is strictly less than b.

#<=(b) ⇒ Object

Returns whether a is less than or equal to b.

#<=>(b) ⇒ Object

Returns negative if a is less than b.

Returns 0 if a is equal to b.

Returns positive if a is greater than b.



690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'ext/gmpq.c', line 690

VALUE r_gmpq_cmp(VALUE self, VALUE arg)
{
  MP_RAT *self_val;
  int res;
  mpq_get_struct (self,self_val);
  res = mpq_cmp_value(self_val, arg);
  if (res > 0)
    return INT2FIX(1);
  else if (res == 0)
    return INT2FIX(0);
  else
    return INT2FIX(-1);
}

#==(b) ⇒ Object

Returns true if a is equal to b, and false otherwise.



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'ext/gmpq.c', line 645

VALUE r_gmpq_eq(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val_q;
  MP_INT *arg_val_z;

  mpq_get_struct(self,self_val);
  if (GMPQ_P(arg)) {
    mpq_get_struct(arg,arg_val_q);
    return mpq_equal(self_val,arg_val_q)?Qtrue:Qfalse;
  } else if (GMPZ_P(arg)) {
    if (mpz_cmp_ui(mpq_denref(self_val), 1) != 0)
      return Qfalse;
    mpz_get_struct (arg, arg_val_z);
    return (mpz_cmp(mpq_numref(self_val),arg_val_z)==0)?Qtrue:Qfalse;
  } else if (FIXNUM_P(arg)) {
    if (mpz_cmp_ui(mpq_denref(self_val), 1) != 0)
      return Qfalse;
    return (mpz_cmp_si (mpq_numref (self_val), FIX2INT (arg)) == 0) ? Qtrue : Qfalse;
  } else if (BIGNUM_P(arg)) {
    if (mpz_cmp_ui(mpq_denref(self_val), 1) != 0)
      return Qfalse;
    mpz_temp_from_bignum(arg_val_z, arg);
    if (mpz_cmp (mpq_numref(self_val),arg_val_z) == 0) {
      mpz_temp_free (arg_val_z);
      return Qtrue;
    } else {
      mpz_temp_free (arg_val_z);
      return Qfalse;
    }
  } else {
    return Qfalse;
  }
}

#>(b) ⇒ Object

Returns whether a is strictly greater than b.

#>=(b) ⇒ Object

Returns whether a is greater than or equal to b.

#absObject

Returns the absolute value of p.

#abs!Object

Sets p to its absolute value.

#ceilObject

#cmpabs(b) ⇒ Object

Returns negative if _abs(a)_ is less than _abs(b)_.

Returns 0 if _abs(a)_ is equal to _abs(b)_.

Returns positive if _abs(a)_ is greater than _abs(b)_.

#coerce(arg) ⇒ Object



28
29
30
31
32
33
34
# File 'ext/gmp.c', line 28

static VALUE r_gmpq_coerce(VALUE self, VALUE arg)
{
  if (FLOAT_P(arg))
    return rb_assoc_new(r_gmpfsg_new(1, &arg, cGMP_F), r_gmpfsg_new(1, &self, cGMP_F));
  else
    return rb_assoc_new(r_gmpqsg_new(1, &arg, cGMP_Q), self);
}

#denObject

Returns the denominator of a.



908
909
910
911
912
913
914
915
916
917
# File 'ext/gmpq.c', line 908

VALUE r_gmpq_get_den(VALUE self)
{
  MP_RAT *self_val;
  MP_INT *res_val;
  VALUE res;
  mpq_get_struct(self,self_val);
  mpz_make_struct(res, res_val);
  mpz_init_set (res_val, mpq_denref (self_val));
  return res;
}

#eql?(b) ⇒ Boolean

Returns whether if a is equal to b. a and b must be equal in cardinality, and both be instances of GMP::Q to return true. ‘a.eql?(b)` if and only if `b.class == GMP::Q`, and `a.hash == b.hash`.

Returns:

  • (Boolean)

Since:

  • 0.5.47



845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'ext/gmpq.c', line 845

VALUE r_gmpq_eql(VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val;
  mpq_get_struct(self,self_val);
  
  if (GMPQ_P(arg)) {
    mpq_get_struct(arg, arg_val);
    return (mpq_cmp (self_val, arg_val) == 0) ? Qtrue : Qfalse;
  }
  else {
    return Qfalse;
  }
}

#floorObject

unsorted

#hashObject

Returns the computed hash value of a. This method first converts a into a String (base 10), then calls String#hash on the result, returning the hash value. ‘a.eql?(b)` if and only if `b.class == GMP::Q`, and `a.hash == b.hash`.

Since:

  • 0.5.47



871
872
873
874
875
876
# File 'ext/gmpq.c', line 871

VALUE r_gmpq_hash(VALUE self)
{
  ID to_s_sym = rb_intern("to_s");
  ID hash_sym = rb_intern("hash");
  return rb_funcall(rb_funcall(self, to_s_sym, 0), hash_sym, 0);
}

#invObject

Returns 1/p.

#inv!Object

Sets p to 1/p.



584
585
586
587
588
589
590
591
592
# File 'ext/gmpq.c', line 584

VALUE r_gmpq_inv_self(VALUE self)
{
  MP_RAT *self_val;
  mpq_get_struct(self, self_val);
  if (mpq_sgn(self_val) == 0)
    rb_raise (rb_eZeroDivError, "divided by 0");
  mpq_inv (self_val, self_val);
  return Qnil;
}

#neg!Object

Sets a to -a.

#numObject

Returns the numerator of a.



889
890
891
892
893
894
895
896
897
898
# File 'ext/gmpq.c', line 889

VALUE r_gmpq_get_num(VALUE self)
{
  MP_RAT *self_val;
  MP_INT *res_val;
  VALUE res;
  mpq_get_struct(self,self_val);
  mpz_make_struct(res, res_val);
  mpz_init_set (res_val, mpq_numref (self_val));
  return res;
}

#sgnObject

Returns +1 if p > 0, 0 if p == 0, and -1 if p < 0.



827
828
829
830
831
832
# File 'ext/gmpq.c', line 827

VALUE r_gmpq_sgn(VALUE self)
{
  MP_RAT *self_val;
  mpq_get_struct(self, self_val);
  return INT2FIX(mpq_sgn(self_val));
}

#swap(q) ⇒ Object

Efficiently swaps the contents of p with q.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/gmpq.c', line 179

VALUE r_gmpq_swap (VALUE self, VALUE arg)
{
  MP_RAT *self_val, *arg_val;

  if (!GMPQ_P (arg)) {
    rb_raise (rb_eTypeError, "Can't swap GMP::Q with object of other class");
  }

  mpq_get_struct (self, self_val);
  mpq_get_struct (arg, arg_val);
  mpq_swap (self_val,arg_val);

  return Qnil;
}

#to_dObject Also known as: to_f

Returns p as an Float if p fits in a Float.

Otherwise returns the least significant part of p, with the same sign as p.

If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur.



214
215
216
217
218
219
220
# File 'ext/gmpq.c', line 214

VALUE r_gmpq_to_d(VALUE self)
{
  MP_RAT *self_val;
  mpq_get_struct (self, self_val);

  return rb_float_new (mpq_get_d (self_val));
}

#to_sObject Also known as: inspect

Returns the decimal representation of p, as a String.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/gmpq.c', line 229

VALUE r_gmpq_to_s(VALUE self)
{
  MP_RAT *self_val;
  MP_INT *self_val_num, *self_val_den;
  char *str;
  VALUE res;
  size_t sizeinbase;
  size_t offset;

  mpq_get_struct (self, self_val)

  if (mpz_cmp_ui (mpq_denref (self_val), 1) == 0) {
    str = mpz_get_str (NULL, 10, mpq_numref (self_val));
    res = rb_str_new2 (str);
    free (str);
    return res;
  }

  self_val_num = mpq_numref (self_val);
  self_val_den = mpq_denref (self_val);

  sizeinbase = mpz_sizeinbase (self_val_num, 10) + mpz_sizeinbase (self_val_den, 10) + 3;
  str = malloc (sizeinbase);

  mpz_get_str (str, 10, self_val_num);
  offset = strlen (str);
  str[offset] = '/';
  mpz_get_str (str + offset + 1, 10, self_val_den);
  res = rb_str_new2 (str);
  free (str);

  return res;
}

#truncObject