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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
128
129
130
131
132
133
134
135
136
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/gmpf.c', line 96

VALUE r_gmpf_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_FLOAT *self_val, *arg_val_f;
  unsigned long prec = 0;
  VALUE arg;
#ifdef MPFR
  mp_rnd_t rnd_mode_val;
#endif
  int base = 10;

  mpf_get_struct (self, self_val);

  if (argc==0) {
#ifdef MPFR
    mpfr_init (self_val);
    mpfr_set_si (self_val, 0, __gmp_default_rounding_mode);
#else
    r_mpf_init (self_val);
    mpf_set_si (self_val, 0);
#endif
    return Qnil;
  }

  arg = argv[0];

  /* argc >= 2 ==> argv[0] is value, argv[1] is prec */
  if (argc >= 2) {
    if (FIXNUM_P(argv[1])) {
      if (FIX2INT(argv[1]) >= 0)
        prec = FIX2INT(argv[1]);
      else {
        r_mpf_init (self_val);
        rb_raise(rb_eRangeError, "precision must be non-negative");
      }
    } else {
      r_mpf_init (self_val);
      rb_raise(rb_eTypeError, "precision must be a Fixnum");
    }
  } else if (GMPF_P(arg)) {
    mpf_get_struct (arg, arg_val_f);
    prec = mpf_get_prec (arg_val_f);
  }
#ifdef MPFR
  rnd_mode_val = __gmp_default_rounding_mode;

  if (prec == 0)
    mpfr_init (self_val);
  else
    mpfr_init2 (self_val, prec);

  if (STRING_P (argv[0])) {
    if (argc >= 3) {
      if (! FIXNUM_P (argv[2]))
        rb_raise(rb_eTypeError, "base must be a Fixnum");

      if (FIX2INT (argv[2]) >= 2 && FIX2INT (argv[2]) <= 36)
        base = FIX2INT (argv[2]);
      else
        rb_raise (rb_eRangeError, "base must be between 2 and 36");

      if (argc == 4)
        rnd_mode_val = r_get_rounding_mode (argv[3]);
      else
        rnd_mode_val = __gmp_default_rounding_mode;
    }


    mpf_set_value2 (self_val, arg, base, rnd_mode_val);
    return Qnil;
  } else {  /* not STRING_P(argv[0]) */
    if (argc == 3)
      rnd_mode_val = r_get_rounding_mode (argv[2]);
  }

  if (GMPF_P (arg)) {
    mpf_get_struct (arg, arg_val_f);
    mpfr_set (self_val, arg_val_f, rnd_mode_val);
  } else {
    mpfr_set_value (self_val, arg, rnd_mode_val);
  }

#else  /* not MPFR */
  (void)base;

  if (prec == 0)
    r_mpf_init (self_val);
  else
    r_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);
  }
#endif /* MPFR */

  return Qnil;
}

Class Method Details

.const_catalanObject

.const_eulerObject

.const_log2Object

.const_piObject

.default_precObject



41
42
43
44
45
# File 'ext/gmp.c', line 41

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

.default_prec=(arg) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'ext/gmp.c', line 47

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

.GMP::F.default_rounding_modeObject

Get the default rounding mode.



1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
# File 'ext/gmpf.c', line 1626

VALUE r_gmpfsg_get_default_rounding_mode(VALUE klass)
{
  const char *rounding_string_val;
  (void)klass;
  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));
  }
}

.GMP::F.default_rounding_mode=(rnd) ⇒ Object

Set the default rounding mode to rnd. The default rounding mode is to nearest initially.



1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
# File 'ext/gmpf.c', line 1646

VALUE r_gmpfsg_set_default_rounding_mode(VALUE klass, VALUE arg)
{
  VALUE mode = 0;
  (void)klass;

  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;
#if MPFR_VERSION_MAJOR>2
    case 4:
      mpfr_set_default_rounding_mode (MPFR_RNDA); break;
#endif
  }

  return Qnil;
}

.GMP::F.emaxObject

Return the (current) largest exponent allowed for a floating-point variable. The largest floating-point value has the form (1 - epsilon) times 2 raised to the largest exponent, where epsilon depends on the precision of the considered variable.

Since:

  • 0.7.19

.GMP::F.emax=(exp) ⇒ Object

Set the largest exponent allowed for a floating-point variable.

Since:

  • 0.7.19



1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
# File 'ext/gmpf.c', line 1905

VALUE r_gmpfrsg_set_emax(VALUE klass, VALUE arg_val)
{
  (void)klass;

  if (! FIXNUM_P (arg_val))
    typeerror_as (X, "exp");

  mpfr_set_emax (FIX2NUM (arg_val));
  /* TODO: figure out a way to generate this RangeError:
    if (success != 0)
      rb_raise(rb_eRangeError, "exp must be in-range");*/

  return Qnil;
}

.GMP::F.emax_maxObject

Return the maximum exponent allowed for GMP::F.emax=()

Since:

  • 0.7.19

.GMP::F.emax_minObject

Return the minimum exponent allowed for GMP::F.emax=()

Since:

  • 0.7.19

.GMP::F.eminObject

Return the (current) smallest exponent allowed for a floating-point variable. The smallest positive value of a floating-point variable is one half times 2 raised to the smallest exponent.

Since:

  • 0.7.19

.GMP::F.emin=(exp) ⇒ Object

Set the smallest exponent allowed for a floating-point variable.

Since:

  • 0.7.19

.GMP::F.emin_maxObject

Return the maximum exponent allowed for GMP::F.emin=()

Since:

  • 0.7.19

.GMP::F.emin_minObject

Return the minimum exponent allowed for GMP::F.emin=()

Since:

  • 0.7.19

.facObject

.GMP::F.infObject .GMP::F.inf(sign) ⇒ Object

‘Inf` (positive infinity), an instance of GMP::F, or `-Inf` (negative infinity), if a negative Fixnum sign is passed

Since:

  • 0.6.47



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/gmpf.c', line 363

VALUE r_gmpfsg_inf(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res;
  VALUE sign_val, res_val;
  int sign = 0;
  (void)klass;

  rb_scan_args (argc, argv, "01", &sign_val);

  if (NIL_P (sign_val))         { sign = 1; }
  else if (FIXNUM_P (sign_val)) { sign = FIX2INT (sign_val); }
  else                          { typeerror_as (X, "sign"); }
  mpf_make_struct_init (res_val, res, mpfr_get_default_prec());
  mpfr_set_inf (res, sign);

  return res_val;
}

.GMP::F.mpfr_buildopt_decimal_pObject

Return a non-zero value if MPFR was compiled with decimal float support (that is, MPFR was built with the –enable-decimal-float configure option), return zero otherwise.



1726
1727
1728
1729
1730
# File 'ext/gmpf.c', line 1726

VALUE r_gmpfsg_mpfr_buildopt_decimal_p(VALUE klass)
{
  (void)klass;
  return INT2FIX (mpfr_buildopt_decimal_p());
}

.GMP::F.mpfr_buildopt_tls_pObject

Return a non-zero value if MPFR was compiled as thread safe using compiler-level Thread Local Storage (that is, MPFR was built with the –enable-thread-safe configure option, see INSTALL file), return zero

otherwise.


1712
1713
1714
1715
1716
# File 'ext/gmpf.c', line 1712

VALUE r_gmpfsg_mpfr_buildopt_tls_p(VALUE klass)
{
  (void)klass;
  return INT2FIX (mpfr_buildopt_tls_p());
}

.GMP::F.nanObject

‘NaN`, an instance of GMP::F

Since:

  • 0.6.47



340
341
342
343
344
345
346
347
348
349
350
# File 'ext/gmpf.c', line 340

VALUE r_gmpfsg_nan(VALUE klass)
{
  MP_FLOAT *res;
  VALUE res_val;
  (void)klass;

  mpf_make_struct_init (res_val, res, mpfr_get_default_prec());
  mpfr_set_nan (res);

  return res_val;
}

.GMP::F.new(value) ⇒ Object .GMP::F.new(value, precision) ⇒ Object .GMP::F.new(value, precision, rounding_mode) ⇒ Object .GMP::F.new(string_value, precision, base) ⇒ Object

Creates a new GMP::F floating-point number, with value as its value, converting where necessary. value must be an instance of one of the following classes:

  • Fixnum

  • Bignum

  • GMP::Z

  • Float

  • GMP::Q

  • GMP::F

  • String

Examples:

GMP::F.new(5)                    #=> 5
GMP::F(3**41)                    #=> 0.36472996377170788e+20
GMP::F(3**41, 32)                #=> 0.36472996375e+20
GMP::F(3**41, 32, GMP::GMP_RNDU) #=> 0.36472996384e+20
GMP::F.new("20")                 #=> 20
GMP::F.new("0x20")               #=> 32
GMP::F("111", 16)                #=> 111
GMP::F("111", 16, 2)             #=> 7
GMP::F("111", 16, 16)            #=> 273


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

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

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

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

  return res;
}

.new_2exp(*args) ⇒ Object



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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'ext/gmpf.c', line 276

VALUE r_gmpfsg_new_2exp(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res;
  MP_INT *arg_z;
  VALUE res_val, arg_val, exp_val, prec_val, rnd_mode_val;
  mp_rnd_t rnd_mode;
  (void)klass;

  rb_scan_args (argc, argv, "22", &arg_val, &exp_val, &prec_val, &rnd_mode_val);
  mpf_make_struct (res_val, res);

  if (!FIXNUM_P (exp_val)) {
    mpfr_init (res);
    rb_raise(rb_eTypeError, "exp must be a Fixnum");
  }

  if (NIL_P (prec_val))
    mpfr_init (res);
  else if (FIXNUM_P (prec_val)) {
    if (FIX2INT (prec_val) >= 0) {
      mpfr_init2 (res, FIX2INT (prec_val));
    } else {
      mpfr_init (res);
      rb_raise(rb_eRangeError, "precision must be non-negative");
    }
  } else {
    mpfr_init (res);
    rb_raise(rb_eTypeError, "precision must be a Fixnum");
  }

  if (NIL_P (rnd_mode_val))
    rnd_mode = __gmp_default_rounding_mode;
  else
    rnd_mode = r_get_rounding_mode (rnd_mode_val);

  if (GMPZ_P (arg_val)) {
    mpz_get_struct (arg_val, arg_z);
    mpfr_set_z_2exp (res, arg_z, FIX2NUM (exp_val), rnd_mode);
  } else if (FIXNUM_P (arg_val)) {
    mpfr_set_si_2exp (res, FIX2NUM (arg_val), FIX2NUM (exp_val), rnd_mode);
  } else if (BIGNUM_P (arg_val)) {
#if 1 /* GMP3 code */
    mpz_temp_from_bignum (arg_z, arg_val);
    mpfr_set_z_2exp (res, arg_z, FIX2NUM (exp_val), rnd_mode);
    mpz_temp_free (arg_z);
#endif /* GMP3 code */
  } else {
    rb_raise (rb_eTypeError, "Don't know how to convert %s into GMP::F", rb_class2name (rb_class_of (arg_val)));
    typeerror_as (ZXB, "value");
  }

  return res_val;
}

.sprintf2(format, arg) ⇒ Object

rb_scan_args (argc, argv, “1*”, &format, &list);



1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/gmpf.c', line 1525

VALUE r_gmpfrsg_sprintf2(VALUE klass, VALUE format, VALUE arg) {
  VALUE res;
  char *buffer;
  char *format_str;
  MP_INT *arg_val_z;
  MP_FLOAT *arg_val_f;
  (void)klass;
  format_str = StringValuePtr (format);
  if (GMPZ_P (arg)) {
    mpz_get_struct (arg, arg_val_z);
    mpfr_asprintf (&buffer, format_str, arg_val_z);
  } else if (GMPF_P (arg)) {
    mpf_get_struct (arg, arg_val_f);
    mpfr_asprintf (&buffer, format_str, arg_val_f);
  }

  res = rb_str_new2 (buffer);
  free (buffer);
  return res;
}

.GMP::F.zeroObject .GMP::F.zero(sign) ⇒ Object

zero or negative zero, an instance of GMP::F, depending on sign, a Fixnum

Since:

  • 0.6.47



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/gmpf.c', line 392

VALUE r_gmpfsg_zero(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res;
  VALUE sign_val, res_val;
  int sign = 0;
  (void)klass;

  rb_scan_args (argc, argv, "01", &sign_val);

  if (NIL_P (sign_val))         { sign = 1; }
  else if (FIXNUM_P (sign_val)) { sign = FIX2INT (sign_val); }
  else                          { typeerror_as (X, "sign"); }
  mpf_make_struct_init (res_val, res, mpfr_get_default_prec());
  mpfr_set_zero (res, sign);

  return res_val;
}

Instance Method Details

#*(y) ⇒ Object

Returns the product of x and y. y can be one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
751
752
753
754
# File 'ext/gmpf.c', line 710

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 = 0;
  mpfr_prec_t 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, FIX2NUM(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;
}

#**(y) ⇒ Object Also known as: pow

Returns x raised to the y power. y must be

  • an instance of Fixnum or Bignum

  • non-negative



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'ext/gmpf.c', line 766

VALUE r_gmpf_pow(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val, *res_val;
  VALUE res = 0;

  //unsigned long prec;
  mpfr_prec_t prec;

  mpf_get_struct_prec (self, self_val, prec);

  if (FIXNUM_P(arg)) {
    if (FIX2NUM(arg) >= 0) {
      mpf_make_struct_init(res, res_val, prec);
      mpf_pow_ui(res_val, self_val, FIX2NUM(arg));
    } else {
      rb_raise(rb_eRangeError, "power must be non-negative");
    }
  } else {
    typeerror(X);
  }

  return res;
}

#+(y) ⇒ Object

Returns the sum of x and y. y must be an instance of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/gmpf.c', line 522

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;
  mpfr_prec_t 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)) { /* TODO: _ui with sign control instead */
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si (res_val, FIX2NUM(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;
}

#-(y) ⇒ Object

Subtracts y from x. y must be an instance of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float

#-@Object

#/(y) ⇒ Object Also known as: divide

Divides x by y. y can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'ext/gmpf.c', line 804

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 = 0;
  mpfr_prec_t 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)) { /* TODO: _ui with sign control instead */
    mpf_make_struct_init(res, res_val, prec);
    mpf_set_si(res_val, FIX2NUM(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_val) ⇒ Object

Float Comparison



976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'ext/gmpf.c', line 976

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

#==(arg_val) ⇒ Object

what does really “equal” mean ? it’s not obvious Is this a note that I, srawlins, put in here? It is not obvious to me…



969
970
971
972
973
974
# File 'ext/gmpf.c', line 969

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

#>Object

#>=Object

#absObject

Returns the absolute value of x.

#abs!Object

Sets x to the absolute value of x.

#acosObject

#acoshObject

#agmObject

TODO “fms”, r_gmpfr_fms

#asinObject

#asinhObject

#atanObject

#atan2Object

#atanhObject

#can_round?(err, rnd1, rnd2, prec) ⇒ Boolean

Returns:

  • (Boolean)


1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'ext/gmpf.c', line 1678

VALUE r_gmpf_can_round(VALUE self, VALUE err, VALUE rnd1, VALUE rnd2, VALUE prec)
{
  MP_FLOAT *self_val;
  mp_exp_t err_val = 0;
  mpfr_rnd_t rnd1_val, rnd2_val;
  mpfr_prec_t prec_val;

  mpf_get_struct (self, self_val);
  if (FIXNUM_P (err)) { err_val = FIX2INT (err); }
  else { typeerror_as (X, "err"); }
  rnd1_val = r_get_rounding_mode (rnd1);
  rnd2_val = r_get_rounding_mode (rnd2);
  prec_val = FIX2INT (prec);

  if (mpfr_can_round (self_val, err_val, rnd1_val, rnd2_val, prec_val))
    return Qtrue;
  else
    return Qfalse;
}

#cbrtObject #cbrt(rounding_mode) ⇒ Object #cbrt(rounding_mode, precision) ⇒ Object

Calculate the cubic root of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#ceilObject

Miscellaneous Functions

#ceil!Object

#coerce(arg) ⇒ Object

new method - testing



36
37
38
39
# File 'ext/gmp.c', line 36

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

#cosObject #cos(rounding_mode) ⇒ Object #cos(rounding_mode, precision) ⇒ Object

Calculate the cosine of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#coshObject

#cotObject #cot(rounding_mode) ⇒ Object #cot(rounding_mode, precision) ⇒ Object

Calculate the cotangent of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#cothObject

#cscObject #csc(rounding_mode) ⇒ Object #csc(rounding_mode, precision) ⇒ Object

Calculate the cosecant of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#cschObject

#digammaObject

#eintObject

#erfObject

#erfcObject

#expObject #exp(rounding_mode) ⇒ Object #exp(rounding_mode, precision) ⇒ Object

Calculate the exponential of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#exp10Object #exp10(rounding_mode) ⇒ Object #exp10(rounding_mode, precision) ⇒ Object

Calculate the 10 power of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#exp2Object #exp2(rounding_mode) ⇒ Object #exp2(rounding_mode, precision) ⇒ Object

Calculate the 2 power of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#expm1Object

#finite?Boolean

Returns:

  • (Boolean)

#floorObject

#floor!Object

#expObject

Set exp and y such that 0.5 <= _abs(y)_ < 1 and y times 2 raised to exp equals x rounded to prec, or the precision of x, using the given rounding mode. If x is zero, then y is set to a zero of the same sign and exp is set to 0. If x is ‘NaN` or an infinity, then y is set to the same value and exp is undefined.

Since:

  • 0.6.47

#gammaObject

#hypotObject

#infinite?Boolean

Returns:

  • (Boolean)

#integer?Boolean

Integer and Remainder Related Functions

Returns:

  • (Boolean)

#j0Object

#j1Object

#jnObject

#lessgreater?(y) ⇒ Boolean

Return true if x < y or x > y, false otherwise

Returns:

  • (Boolean)

Since:

  • 0.6.47



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'ext/gmpf.c', line 1020

VALUE r_gmpfr_lessgreater_p(VALUE self_val, VALUE arg_val)
{
  MP_FLOAT *self, *arg;

  if (!GMPF_P (arg_val))
    typeerror_as (F, "arg");

  mpf_get_struct (self_val, self);
  mpf_get_struct (arg_val, arg);
  return (mpfr_lessgreater_p (self, arg) != 0) ? Qtrue : Qfalse;
}

#li2Object

#lngammaObject

#logObject #log(rounding_mode) ⇒ Object #log(rounding_mode, precision) ⇒ Object

Calculate the natural logarithm of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#log10Object #log10(rounding_mode) ⇒ Object #log10(rounding_mode, precision) ⇒ Object

Calculate the logarithm base 10 of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#log1pObject

#log2Object #log2(rounding_mode) ⇒ Object #log2(rounding_mode, precision) ⇒ Object

Calculate the logarithm base 2 of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#nan?Boolean

Comparison Functions

Returns:

  • (Boolean)

#neg!Object

Sets x to -x.

#number?Boolean

Returns:

  • (Boolean)

#precObject

Return the precision of x, i.e. the number of bits used to store its significand.



1747
1748
1749
1750
1751
1752
# File 'ext/gmpf.c', line 1747

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

#prec=(p) ⇒ Object

Reset the precision of x to be exactly p bits, and set its value to NaN. The previous value stored in x is lost. The precision prec can be any integer between ‘MPFR_PREC_MIN` and `MPFR_PREC_MAX`.



1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
# File 'ext/gmpf.c', line 1764

VALUE r_gmpf_set_prec(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val;
  if (FIXNUM_P(arg)) {
    mpf_get_struct (self, self_val);
    mpf_set_prec (self_val, FIX2NUM (arg));
    return Qnil;
  } else {
    typeerror(X);
  }

  return Qnil;  /* should never get here */
}

#prec_raw=(arg) ⇒ Object

should never get here



1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
# File 'ext/gmpf.c', line 1778

VALUE r_gmpf_set_prec_raw(VALUE self, VALUE arg)
{
  MP_FLOAT *self_val;
  if (FIXNUM_P(arg)) {
    mpf_get_struct (self, self_val);
    mpf_set_prec_raw (self_val, FIX2NUM (arg));
    return Qnil;
  } else {
    typeerror(X);
  }

  return Qnil;  /* should never get here */
}

#rec_sqrtObject #rec_sqrt(rounding_mode) ⇒ Object #rec_sqrt(rounding_mode, precision) ⇒ Object

Calculate the reciprocal square root of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#regular?Boolean

Returns:

  • (Boolean)

#secObject

#sechObject

#sgnObject

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

#sinObject #sin(rounding_mode) ⇒ Object #sin(rounding_mode, precision) ⇒ Object

Calculate the sine of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#sin_cosObject #sin_cos(rounding_mode) ⇒ Object #sin_cos(rounding_mode, precision) ⇒ Object

Simultaneously calculate the sine and cosine of x, rounding according to ‘rounding_mode`, returning both numbers. The resultant GMP::F floats have the same precision that x has, if `precision` was not passed in.

#sinhObject

#sinh_coshObject

#sqrtObject #sqrt(rounding_mode) ⇒ Object #sqrt(rounding_mode, precision) ⇒ Object

Calculate the square root of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#tanObject #tan(rounding_mode) ⇒ Object #tan(rounding_mode, precision) ⇒ Object

Calculate the tangent of x, rounding according to ‘rounding_mode`. The resultant GMP::F float has the same precision that x has, if `precision` was not passed in.

#tanhObject

#to_dObject Also known as: to_f

Returns x as a Float.



436
437
438
439
440
441
442
# File 'ext/gmpf.c', line 436

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_s(base = 10) ⇒ Object Also known as: inspect

Returns a representation of x, as a String. By default, the String will be the decimal representation. Any valid GMP base can be passed.



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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'ext/gmpf.c', line 452

VALUE r_gmpf_to_s(int argc, VALUE *argv, VALUE self_val)
{
  MP_FLOAT *self;
  char *str, *str2;
  VALUE res_val;
  mp_exp_t exponent;
  VALUE base_val;
  int base = 10;

  mpf_get_struct (self_val, self);

  /* TODO: accept a second optional argument, n_digits */
  rb_scan_args (argc, argv, "01", &base_val);
  if (NIL_P (base_val)) { base = 10; }                /* default value */
  else { base = get_base (base_val); }

#ifndef MPFR
  str = mpf_get_str (NULL, &exponent, base, 0, self);
#else
  str = mpfr_get_str (NULL, &exponent, base, 0, self, __gmp_default_rounding_mode);
#endif
  if ((strcmp (str,  "NaN") == 0) ||
      (strcmp (str,  "Inf") == 0) ||
      (strcmp (str, "-Inf") == 0))
  {
    res_val = 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_val = rb_str_new2 (str2);
#ifndef MPFR
    free (str2);
#else
    mpfr_free_str (str2);
#endif
  }

#ifndef MPFR
  free (str);
#else
  mpfr_free_str (str);
#endif
  return res_val;
}

#truncObject

#trunc!Object

#unordered?(y) ⇒ Boolean

Return true if x or y is a ‘NaN`, false otherwise

Returns:

  • (Boolean)

Since:

  • 0.6.47



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'ext/gmpf.c', line 1041

VALUE r_gmpfr_unordered_p(VALUE self_val, VALUE arg_val)
{
  MP_FLOAT *self, *arg;

  if (!GMPF_P (arg_val))
    typeerror_as (F, "arg");

  mpf_get_struct (self_val, self);
  mpf_get_struct (arg_val, arg);
  return (mpfr_unordered_p (self, arg) != 0) ? Qtrue : Qfalse;
}

#y0Object

#y1Object

#ynObject

#zero?Boolean

Returns:

  • (Boolean)

#zetaObject