Class: GMP::F

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

Overview

GMP Multiple Precision floating point numbers.

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

The following list is just a simple checklist for me, really. A better reference should be found in the rdocs.

Ruby method    C Extension function    GMP function
to_d           r_gmpf_to_d             mpf_get_d
to_s           r_gmpf_to_s             mpf_get_s
+              r_gmpf_add              mpf_add
-              r_gmpf_sub              mpf_sub
*              r_gmpf_mul              mpf_mul
/              r_gmpf_div              mpf_div

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/gmpf.c', line 85

VALUE r_gmpf_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_FLOAT *self_val, *arg_val_f;
  unsigned long prec = 0;
  VALUE arg;

  mpf_get_struct (self, self_val);

  if (argc==0) {
    mpf_init(self_val);
    mpf_set_si(self_val, 0);
    return Qnil;
  }

  arg = argv[0];

  if (argc == 2) {
    if (FIXNUM_P(argv[1])) {
      if (FIX2INT(argv[1]) >= 0)
        prec = FIX2INT(argv[1]);
      else
        rb_raise(rb_eRangeError, "prec must be non-negative");
    } else {
      rb_raise(rb_eTypeError, "prec must be a Fixnum");
    }
  } else if (GMPF_P(arg)) {
    mpf_get_struct (arg, arg_val_f);
    prec = mpf_get_prec (arg_val_f);
  }
  if (prec == 0)
    mpf_init (self_val);
  else
    mpf_init2 (self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct (arg, arg_val_f);
    mpf_set(self_val, arg_val_f);
  } else {
    mpf_set_value(self_val, arg);
  }

  return Qnil;
}

Class Method Details

.const_catalanObject

.const_eulerObject

.const_log2Object

.const_piObject

.default_precObject



104
105
106
107
108
# File 'ext/gmp.c', line 104

static VALUE r_gmpfsg_get_default_prec(VALUE klass)
{
  (void)klass;
  return INT2NUM(mpf_get_default_prec());
}

.default_prec=(arg) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/gmp.c', line 110

static VALUE r_gmpfsg_set_default_prec(VALUE klass, VALUE arg)
{
  (void)klass;
  if (FIXNUM_P(arg)) {
    if (FIX2INT(arg) <= 0) {
      rb_raise(rb_eRangeError, "prec must be positive");
    }
    mpf_set_default_prec (FIX2INT(arg));
  } else {
    rb_raise(rb_eTypeError, "prec must be FixNum");
  }
  return Qnil;
}

.default_rounding_modeObject



125
126
127
128
129
130
131
132
133
134
135
# File 'ext/gmp.c', line 125

static VALUE r_gmpfsg_get_default_rounding_mode(VALUE klass)
{
  (void)klass;
  const char *rounding_string_val;
  VALUE rounding_string;
  ID method_to_sym = rb_intern("to_sym");
  rounding_string_val = mpfr_print_rnd_mode (mpfr_get_default_rounding_mode ());
  rounding_string = rb_str_new2 (rounding_string_val);
  /*return rb_const_get (mGMP, rb_intern (rounding_string));*/
  return rb_funcall (rounding_string, method_to_sym, 0);
}

.default_rounding_mode=(arg) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/gmp.c', line 137

static VALUE r_gmpfsg_set_default_rounding_mode(VALUE klass, VALUE arg)
{
  (void)klass;
  int arg_val;
  if (FIXNUM_P(arg)) {
    arg_val = FIX2INT(arg);
    if (arg_val < 0 || arg_val > 3) {
      rb_raise(rb_eRangeError, "rounding mode must be one of the rounding mode constants.");
    }
  } else {
    rb_raise(rb_eTypeError, "rounding mode must be one of the rounding mode constants.");
  }
  
  switch (arg_val) {
    case 0:
      mpfr_set_default_rounding_mode (GMP_RNDN);
      break;
    case 1:
      mpfr_set_default_rounding_mode (GMP_RNDZ);
      break;
    case 2:
      mpfr_set_default_rounding_mode (GMP_RNDU);
      break;
    case 3:
      mpfr_set_default_rounding_mode (GMP_RNDD);
      break;
  }
  
  return Qnil;
}

.GMP::R.new(arg) ⇒ Object

Creates a new GMP::R float, with arg as its value, converting where necessary.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/gmpf.c', line 69

VALUE r_gmpfsg_new(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res_val;
  VALUE res;

  (void)klass;

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

  mpf_make_struct (res, res_val);
  rb_obj_call_init(res, argc, argv);

  return res;
}

Instance Method Details

#*(other) ⇒ Object

Returns the product of float and other. other can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



324
325
326
327
328
329
330
331
332
333
334
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
# File 'ext/gmpf.c', line 324

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

  mpf_get_struct_prec (self, self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct(arg, arg_val_f);
    prec_max(prec, arg_val_f);
    mpf_make_struct_init(res, res_val, prec);
    mpf_mul(res_val, self_val, arg_val_f);
  } else if (GMPQ_P(arg)) {
    mpq_get_struct(arg, arg_val_q);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_q(res_val, arg_val_q);
    mpf_mul(res_val, self_val, res_val);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val_z);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_mul(res_val, self_val, res_val);
  } else if (FLOAT_P(arg)) {
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_d(res_val, NUM2DBL(arg));
    mpf_mul(res_val, self_val, res_val);
  } else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si(res_val, FIX2INT(arg));
    mpf_mul(res_val, self_val, res_val);
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val_z, arg);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_mul(res_val, res_val, self_val);
    mpz_temp_free(arg_val_z);
  } else {
    typeerror(ZQFXBD);
  }

  return res;
}

#**(arg) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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
# File 'ext/gmpf.c', line 630

static VALUE r_gmpfr_pow(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val, *res_val, *arg_val_f;
  MP_RAT *arg_val_q;
  MP_INT *arg_val_z;
  unsigned long prec;
  VALUE res;

  mpf_get_struct_prec(self, self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct(arg, arg_val_f);
    prec_max(prec, arg_val_f);
    mpf_make_struct_init(res, res_val, prec);
    mpfr_pow(res_val, self_val, arg_val_f, __gmp_default_rounding_mode);
  } else {
    mpf_make_struct_init(res, res_val, prec);

    if (GMPZ_P(arg)) {
      mpz_get_struct(arg, arg_val_z);
      mpf_set_z(res_val, arg_val_z);
      mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
    } else if (GMPQ_P(arg)) {
      mpq_get_struct(arg, arg_val_q);
      mpf_set_q(res_val, arg_val_q);
      mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
    } else if (FLOAT_P(arg)) {
      mpf_set_d(res_val, NUM2DBL(arg));
      mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
    } else if (FIXNUM_P(arg)) {
      mpfr_pow_si(res_val, self_val, FIX2INT(arg), __gmp_default_rounding_mode);
    } else if (BIGNUM_P(arg)) {
      mpz_temp_from_bignum(arg_val_z, arg);
      mpf_set_z(res_val, arg_val_z);
      mpz_temp_free(arg_val_z);
      mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
    } else {
      typeerror(ZQFXBD);
    }

  }

  return res;
}

#+(other) ⇒ Object

Returns the sum of float and other. other can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/gmpf.c', line 208

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

  mpf_get_struct_prec (self, self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct (arg, arg_val_f);
    prec_max(prec, arg_val_f);
    mpf_make_struct_init(res, res_val, prec);
    mpf_add(res_val, self_val, arg_val_f);
  } else if (GMPQ_P(arg)) {
    mpq_get_struct (arg, arg_val_q);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_q (res_val, arg_val_q);
    mpf_add (res_val, res_val, self_val);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct (arg, arg_val_z);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z (res_val, arg_val_z);
    mpf_add (res_val, res_val, self_val);
  } else if (FLOAT_P(arg)) {
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_d (res_val, NUM2DBL(arg));
    mpf_add (res_val, res_val, self_val);
  } else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si (res_val, FIX2INT(arg));
    mpf_add (res_val, res_val, self_val);
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val_z, arg);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z (res_val, arg_val_z);
    mpf_add (res_val, res_val, self_val);
    mpz_temp_free(arg_val_z);
  } else {
    typeerror(ZQFXBD);
  }

  return res;
}

#-(float2) ⇒ Object

Subtracts float2 from float1. float2 can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'ext/gmpf.c', line 266

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

  mpf_get_struct_prec (self, self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct(arg, arg_val_f);
    prec_max(prec, arg_val_f);
    mpf_make_struct_init(res, res_val, prec);
    mpf_sub(res_val, self_val, arg_val_f);
  } else if (GMPQ_P(arg)) {
    mpq_get_struct(arg, arg_val_q);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_q(res_val, arg_val_q);
    mpf_sub(res_val, self_val, res_val);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val_z);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_sub(res_val, self_val, res_val);
  } else if (FLOAT_P(arg)) {
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_d(res_val, NUM2DBL(arg));
    mpf_sub(res_val, self_val, res_val);
  } else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si(res_val, FIX2INT(arg));
    mpf_sub(res_val, self_val, res_val);
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val_z, arg);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_sub(res_val, self_val, res_val);
    mpz_temp_free(arg_val_z);
  } else {
    typeerror(ZQFXBD);
  }

  return res;
}

#-@Object

#/(float2) ⇒ Object

Divides float1 by float2. float2 can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
419
420
421
422
423
424
425
426
# File 'ext/gmpf.c', line 382

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

  mpf_get_struct_prec (self, self_val, prec);

  if (GMPF_P(arg)) {
    mpf_get_struct(arg, arg_val_f);
    prec_max(prec, arg_val_f);
    mpf_make_struct_init(res, res_val, prec);
    mpf_div(res_val, self_val, arg_val_f);
  } else if (GMPQ_P(arg)) {
    mpq_get_struct(arg, arg_val_q);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_q(res_val, arg_val_q);
    mpf_div(res_val, self_val, res_val);
  } else if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val_z);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_div(res_val, self_val, res_val);
  } else if (FLOAT_P(arg)) {
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_d(res_val, NUM2DBL(arg));
    mpf_div(res_val, self_val, res_val);
  } else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si(res_val, FIX2INT(arg));
    mpf_div(res_val, self_val, res_val);
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val_z, arg);
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_z(res_val, arg_val_z);
    mpf_div(res_val, self_val, res_val);	
    mpz_temp_free(arg_val_z);
  } else {
    typeerror(ZQFXBD);
  }

  return res;
}

#<Object

#<=Object

#<=>(arg) ⇒ Object

Float Comparison



498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'ext/gmpf.c', line 498

VALUE r_gmpf_cmp(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val;
  int res;
  mpf_get_struct(self,self_val);
  res = mpf_cmp_value(self_val, arg);
  if (res > 0)
    return INT2FIX(1);
  else if (res == 0)
    return INT2FIX(0);
  else
    return INT2FIX(-1);
}

#==(arg) ⇒ Object

what does really “equal” mean ? it’s not obvious



491
492
493
494
495
496
# File 'ext/gmpf.c', line 491

VALUE r_gmpf_eq(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val;
  mpf_get_struct (self,self_val);
  return (mpf_cmp_value(self_val, arg) == 0) ? Qtrue : Qfalse;
}

#>Object

#>=Object

#absObject

call-seq:

float.abs

From the GMP Manual:

Returns the absolute value of float.

#abs!Object

call-seq:

float.abs!

Sets float to the absolute value of float.

#acosObject

#acoshObject

#asinObject

#asinhObject

#atanObject

#atanhObject

#ceilObject

#ceil!Object

#coerce(arg) ⇒ Object

new method - testing



99
100
101
102
# File 'ext/gmp.c', line 99

static VALUE r_gmpf_coerce(VALUE self, VALUE arg)
{
  return rb_assoc_new(r_gmpfsg_new(1, &arg, cGMP_F), self);
}

#cosObject

#coshObject

#cotObject

#cscObject

#expObject

#expm1Object

#finite?Boolean

Returns:

  • (Boolean)


607
608
609
610
611
612
613
614
615
# File 'ext/gmpf.c', line 607

static VALUE r_gmpfr_fin_p(VALUE self)
{
  if (r_gmpfr_inf_p(self)) {
    return Qfalse;
  }
  else {
    return Qtrue;
  }
}

#floorObject

unsorted

#floor!Object

#infinite?Boolean

Returns:

  • (Boolean)


594
595
596
597
598
599
600
601
602
603
604
605
# File 'ext/gmpf.c', line 594

static VALUE r_gmpfr_inf_p(VALUE self)
{
  MP_FLOAT *self_val;

  mpf_get_struct(self, self_val);
  if (mpfr_inf_p(self_val)) {
    return Qtrue;
  }
  else {
    return Qfalse;
  }
}

#logObject

Special Functions

#log10Object

#log1pObject

#log2Object

#nan?Boolean

Comparison Functions

Returns:

  • (Boolean)

#neg!Object

call-seq:

float.neg!

Sets float to -float.

#number?Boolean

Returns:

  • (Boolean)


617
618
619
620
621
622
623
624
625
626
627
628
# File 'ext/gmpf.c', line 617

static VALUE r_gmpfr_number_p(VALUE self)
{
  MP_FLOAT *self_val;

  mpf_get_struct(self, self_val);
  if (mpfr_number_p(self_val)) {
    return Qtrue;
  }
  else {
    return Qfalse;
  }
}

#precObject



701
702
703
704
705
706
# File 'ext/gmpf.c', line 701

VALUE r_gmpf_get_prec(VALUE self)
{
  MP_FLOAT *self_val;
  mpf_get_struct(self, self_val);
  return INT2NUM(mpf_get_prec(self_val));
}

#secObject

#sgnObject

#sinObject

#sinhObject

#sqrtObject

Basic Arithmetic Functions

#tanObject

#tanhObject

#to_dObject Also known as: to_f

********************************************************************

Converting Floats                                               *

********************************************************************



146
147
148
149
150
151
152
# File 'ext/gmpf.c', line 146

VALUE r_gmpf_to_d(VALUE self)
{
  MP_FLOAT *self_val;
  mpf_get_struct(self, self_val);

  return rb_float_new(mpf_get_d(self_val));
}

#to_sObject

call-seq:

float.to_s

Returns the decimal representation of float, as a string.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/gmpf.c', line 162

VALUE r_gmpf_to_s(VALUE self)
{
  MP_FLOAT *self_val;
  char *str, *str2;
  VALUE res;
  mp_exp_t exponent;

  mpf_get_struct(self, self_val);

  str = mpf_get_str(NULL, &exponent, 10, 0, self_val);
  if ((strcmp(str,  "NaN") == 0) ||
      (strcmp(str,  "Inf") == 0) ||
      (strcmp(str, "-Inf") == 0))
  {
    res = rb_str_new2(str);
  }
  else
  {
    if (str[0] == '-')
      __gmp_asprintf(&str2, "-0.%se%+ld", str+1, exponent);
    else
      __gmp_asprintf(&str2, "0.%se%+ld", str, exponent);
    res = rb_str_new2(str2);
    free(str2);
  }
  free(str);
  return res;
}

#truncObject

#trunc!Object