Class: GMP::Q
- Inherits:
-
Numeric
- Object
- Numeric
- GMP::Q
- 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
-
.new(*args) ⇒ Object
Creates a new GMP::Q rational, with arg as its value, converting where necessary.
Instance Method Summary collapse
-
#*(q) ⇒ Object
Multiplies p with q.
-
#+(q) ⇒ Object
Adds p to q.
-
#-(q) ⇒ Object
Subtracts p from q.
- #-@ ⇒ Object (also: #neg)
-
#/(q) ⇒ Object
Divides p by q.
- #< ⇒ Object
- #<= ⇒ Object
-
#<=>(arg) ⇒ Object
Comparing Rationals.
- #==(arg) ⇒ Object
- #> ⇒ Object
- #>= ⇒ Object
- #abs ⇒ Object
- #abs! ⇒ Object
- #ceil ⇒ Object
- #cmpabs ⇒ Object
- #coerce(arg) ⇒ Object
- #den ⇒ Object
-
#eql?(b) ⇒ Boolean
Returns true if a is equal to b.
-
#floor ⇒ Object
unsorted.
-
#hash ⇒ Object
Returns the computed hash value of a.
- #initialize ⇒ Object constructor
- #inv ⇒ Object
-
#inv! ⇒ Object
Sets p to 1/p.
-
#neg! ⇒ Object
call-seq: a.neg!.
- #num ⇒ Object
-
#sgn ⇒ Object
Returns +1 if p > 0, 0 if p == 0, and -1 if p < 0.
-
#swap(q) ⇒ Object
Efficiently swaps the contents of p with q.
-
#to_d ⇒ Object
(also: #to_f)
Returns p as an Float if p fits in a Float.
-
#to_s ⇒ Object
(also: #inspect)
Returns the decimal representation of p, as a String.
- #trunc ⇒ Object
Constructor Details
#initialize ⇒ Object
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 # 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
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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 |
# File 'ext/gmpq.c', line 359
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
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'ext/gmpq.c', line 252
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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'ext/gmpq.c', line 304
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
#/(q) ⇒ Object
Divides p by q. q must be an instance of one of:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'ext/gmpq.c', line 431
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;
}
|
#< ⇒ Object
#<= ⇒ Object
#<=>(arg) ⇒ Object
Comparing Rationals
633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'ext/gmpq.c', line 633
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);
}
|
#==(arg) ⇒ Object
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'ext/gmpq.c', line 599
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_ui(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;
}
}
|
#> ⇒ Object
#>= ⇒ Object
#abs ⇒ Object
#abs! ⇒ Object
#ceil ⇒ Object
#cmpabs ⇒ Object
#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);
}
|
#den ⇒ Object
#eql?(b) ⇒ Boolean
Returns true if a is equal to b. a and b must then be equal in cardinality, and both be instances of GMP::Q. Otherwise, returns false. a.eql?(b) if and only if b.class == GMP::Q, and a.hash == b.hash.
742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'ext/gmpq.c', line 742
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;
}
}
|
#floor ⇒ Object
unsorted
#hash ⇒ Object
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.
766 767 768 769 770 771 |
# File 'ext/gmpq.c', line 766
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);
}
|
#inv ⇒ Object
#inv! ⇒ Object
Sets p to 1/p.
547 548 549 550 551 552 553 554 555 |
# File 'ext/gmpq.c', line 547
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
call-seq:
a.neg!
Sets a to -a.
#num ⇒ Object
#sgn ⇒ Object
Returns +1 if p > 0, 0 if p == 0, and -1 if p < 0.
725 726 727 728 729 730 |
# File 'ext/gmpq.c', line 725
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.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'ext/gmpq.c', line 154
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_d ⇒ Object 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.
187 188 189 190 191 192 193 |
# File 'ext/gmpq.c', line 187
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_s ⇒ Object Also known as: inspect
Returns the decimal representation of p, as a String.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'ext/gmpq.c', line 201
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;
//Data_Get_Struct (self, MP_RAT, self_val);
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;
}
|