Class: GMP::Q

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/gmp.c', line 186

static VALUE r_gmpq_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_RAT *self_val, *arg_val;

  if (argc != 0) {
    mpq_get_struct(self, self_val);
    if (argc == 1 && GMPQ_P(argv[0])) {
      mpq_get_struct(argv[0], arg_val);
      mpq_set (self_val, arg_val);
    } else if (argc == 1 && STRING_P(argv[0])) {
      mpq_str_set (self_val, STR2CSTR(argv[0]));
    } else {
      mpz_set_value (mpq_numref(self_val), argv[0]);
      if (argc == 2) {
        mpz_set_value (mpq_denref(self_val), argv[1]);
        mpq_canonicalize(self_val);
      }
    }
  }
  return Qnil;
}

Class Method Details

.new(*args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/gmp.c', line 103

static 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

#*Object

#+Object

#-Object

#-@Object

#/Object

#<Object

#<=Object

#<=>Object

#==Object

#>Object

#>=Object

#absObject

#abs!Object

#ceilObject

#cmpabsObject

#coerce(arg) ⇒ Object



327
328
329
330
# File 'ext/gmp.c', line 327

static VALUE r_gmpq_coerce(VALUE self, VALUE arg)
{
  return rb_assoc_new(r_gmpqsg_new(1, &arg, cGMP_Q), self);
}

#denObject

#floorObject

#invObject

#inv!Object

#neg!Object

#numObject

#sgnObject

#swapObject

#to_dObject

#to_sObject

Converts this mpq_t object to a Ruby string.



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/gmp.c', line 288

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

  Data_Get_Struct(self, MP_RAT, 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