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



84
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
# File 'ext/gmpf.c', line 84

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



76
77
78
79
80
# File 'ext/gmp.c', line 76

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

.default_prec=(arg) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/gmp.c', line 82

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/gmp.c', line 97

static VALUE r_gmpfsg_get_default_rounding_mode(VALUE klass)
{
  (void)klass;
  const char *rounding_string_val;
  rounding_string_val = mpfr_print_rnd_mode (mpfr_get_default_rounding_mode ());
  if ( rounding_string_val == NULL ) {
    return Qnil;
  }
  else {
    return rb_const_get (mGMP, rb_intern (rounding_string_val));
  }
}

.default_rounding_mode=(arg) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/gmp.c', line 110

static VALUE r_gmpfsg_set_default_rounding_mode(VALUE klass, VALUE arg)
{
  (void)klass;
  VALUE mode;
  if (GMPRND_P(arg)) {
    mode = rb_funcall (arg, rb_intern("mode"), 0);
    if (FIX2INT(mode) < 0 || FIX2INT(mode) > 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 (FIX2INT(mode)) {
    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
# 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



354
355
356
357
358
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
# File 'ext/gmpf.c', line 354

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



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'ext/gmpf.c', line 707

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



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

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



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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'ext/gmpf.c', line 296

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



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
452
453
454
455
456
# File 'ext/gmpf.c', line 412

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



528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/gmpf.c', line 528

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



521
522
523
524
525
526
# File 'ext/gmpf.c', line 521

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



71
72
73
74
# File 'ext/gmp.c', line 71

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

#cosObject

#coshObject

#cotObject

#cothObject

#cscObject

#cschObject

#eintObject

#erfObject

#erfcObject

#expObject

#exp10Object

#exp2Object

#expm1Object

#finite?Boolean

Returns:

  • (Boolean)


684
685
686
687
688
689
690
691
692
# File 'ext/gmpf.c', line 684

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

#floorObject

unsorted

#floor!Object

#gammaObject

#infinite?Boolean

Returns:

  • (Boolean)


671
672
673
674
675
676
677
678
679
680
681
682
# File 'ext/gmpf.c', line 671

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;
  }
}

#j0Object

#j1Object

#jnObject

#li2Object

#lngammaObject

#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)


694
695
696
697
698
699
700
701
702
703
704
705
# File 'ext/gmpf.c', line 694

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



778
779
780
781
782
783
# File 'ext/gmpf.c', line 778

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

#sechObject

#sgnObject

#sinObject

#sinhObject

#sqrtObject

Basic Arithmetic Functions

#tanObject

#tanhObject

#to_dObject Also known as: to_f

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

Converting Floats                                               *

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



176
177
178
179
180
181
182
# File 'ext/gmpf.c', line 176

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.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/gmpf.c', line 192

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

#y0Object

#y1Object

#ynObject

#zetaObject

rb_define_method(cGMP_F, “lgamma”, r_gmpfr_lgamma, -1);