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, "prec must be non-negative");
      }
    } else {
      r_mpf_init (self_val);
      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);
  }
#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])) {
        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");
      }
      else {
        rb_raise(rb_eTypeError, "base must be a Fixnum");
      }
    }

    if (argc == 4) {
      // TODO: FIGURE IT OUT. ACCEPT A ROUNDING MODE!
    }

    mpf_set_value2 (self_val, arg, base);
    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

  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, "prec must be positive");
    }
    mpf_set_default_prec (FIX2NUM(arg));
  } else {
    rb_raise(rb_eTypeError, "prec must be FixNum");
  }
  return Qnil;
}

.default_rounding_modeObject

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

Rounding Related Functions                                      *

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



1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
# File 'ext/gmpf.c', line 1330

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

.default_rounding_mode=(arg) ⇒ Object



1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
# File 'ext/gmpf.c', line 1343

VALUE r_gmpfsg_set_default_rounding_mode(VALUE klass, VALUE arg)
{
  VALUE mode;
  (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;
}

.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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/gmpf.c', line 302

VALUE r_gmpfsg_inf(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res;
  VALUE sign_val, res_val;
  int sign;
  (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;
}

.mpfr_buildopt_decimal_pObject



1408
1409
1410
1411
1412
# File 'ext/gmpf.c', line 1408

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

.mpfr_buildopt_tls_pObject

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

Rounding Related Functions                                      *

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



1402
1403
1404
1405
1406
# File 'ext/gmpf.c', line 1402

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



282
283
284
285
286
287
288
289
290
291
292
# File 'ext/gmpf.c', line 282

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

.sprintf2(format, arg) ⇒ Object

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



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'ext/gmpf.c', line 1237

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



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'ext/gmpf.c', line 328

VALUE r_gmpfsg_zero(int argc, VALUE *argv, VALUE klass)
{
  MP_FLOAT *res;
  VALUE sign_val, res_val;
  int sign;
  (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

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
674
675
676
677
678
679
680
# File 'ext/gmpf.c', line 636

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



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'ext/gmpf.c', line 690

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

  //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



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

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)) { // _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;
}

#-Object

Float Arithmetic

#-@Object

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

Divides x by y. y can be

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum

  • Float



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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'ext/gmpf.c', line 726

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;
  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)) { // _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) ⇒ Object

Float Comparison



899
900
901
902
903
904
905
906
907
908
909
910
911
# File 'ext/gmpf.c', line 899

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 Is this a note that I, srawlins, put in here? It is not obvious to me…



892
893
894
895
896
897
# File 'ext/gmpf.c', line 892

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:

x.abs

Returns the absolute value of x.

#abs!Object

call-seq:

x.abs!

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)


1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
# File 'ext/gmpf.c', line 1374

VALUE r_gmpf_can_round(VALUE self, VALUE err, VALUE rnd1, VALUE rnd2, VALUE prec)
{
  MP_FLOAT *self_val;
  mp_exp_t err_val;
  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

#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

#coshObject

#cotObject

#cothObject

#cscObject

#cschObject

#digammaObject

#eintObject

#erfObject

#erfcObject

#expObject

#exp10Object

#exp2Object

#expm1Object

#finite?Boolean

Returns:

  • (Boolean)

#floorObject

#floor!Object

#frexpObject

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


939
940
941
942
943
944
945
946
947
948
949
# File 'ext/gmpf.c', line 939

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

Special Functions

#log10Object

#log1pObject

#log2Object

#nan?Boolean

Comparison Functions

Returns:

  • (Boolean)

#neg!Object

call-seq:

x.neg!

Sets x to -x.

#number?Boolean

Returns:

  • (Boolean)

#precObject

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

_unsorted_                                                      *

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



1421
1422
1423
1424
1425
1426
# File 'ext/gmpf.c', line 1421

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=(arg) ⇒ Object



1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
# File 'ext/gmpf.c', line 1428

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

#prec_raw=(arg) ⇒ Object



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'ext/gmpf.c', line 1440

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

#rec_sqrtObject

#regular?Boolean

Returns:

  • (Boolean)

#secObject

#sechObject

#sgnObject

#sinObject

#sin_cosObject

#sinhObject

#sinh_coshObject

#sqrtObject

Basic Arithmetic Functions

#tanObject

#tanhObject

#to_dObject Also known as: to_f

Returns x as a Float.



371
372
373
374
375
376
377
# File 'ext/gmpf.c', line 371

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.



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
427
428
429
430
431
432
433
434
# File 'ext/gmpf.c', line 386

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)


957
958
959
960
961
962
963
964
965
966
967
# File 'ext/gmpf.c', line 957

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