Class: MPFR

Inherits:
Numeric
  • Object
show all
Defined in:
ext/mpfr_rb.c,
lib/sollya.rb,
ext/mpfr_rb.c

Overview

MPFR methods take as last argument a rounding mode, and have a return value of type integer, called the ternary value. The value stored in the receiver is correctly rounded, i.e., MPFR behaves as if it computed the result with an infinite precision, then rounded it to the precision of this variable. The input variables are regarded as exact (in particular, their precision does not affect the result).

As a consequence, in case of a non-zero real rounded result, the error on the result is less than or equal to 1/2 ulp (unit in the last place) of that result in the rounding to nearest mode, and less than 1 ulp of that result in the directed rounding modes (a ulp is the weight of the least significant represented bit of the result after rounding).

Unless documented otherwise, methods returning an integer return a ternary value. If the ternary value is zero, it means that the value stored in the receiver is the exact result of the corresponding mathematical function. If the ternary value is positive (resp. negative), it means the value stored in the receiver is greater (resp. lower) than the exact result. For example with the :round_up rounding mode, the ternary value is usually positive, except when the result is exact, in which case it is zero. In the case of an infinite result, it is considered as inexact when it was obtained by overflow, and exact otherwise. A NaN result (Not-a-Number) always corresponds to an exact return value. The opposite of a returned ternary value is guaranteed to be representable in an integer.

Constant Summary collapse

PREC_MIN =

The minimum precision that can be set for a MPFR object.

Returns:

INT2NUM(MPFR_PREC_MIN)
PREC_MAX =

The maximum precision that can be set for a MPFR object.

Returns:

ULL2NUM(MPFR_PREC_MAX)
EMIN_MIN =

The minimum of the exponents allowed for emin=. This value is implementation dependent, thus a program using MPFR.emin = EMIN_MIN may not be portable.

Returns:

LL2NUM(mpfr_get_emin_min())
EMIN_MAX =

The maximum of the exponents allowed for emin=.

Returns:

LL2NUM(mpfr_get_emin_max())
EMAX_MIN =

The minimum of the exponents allowed for emax=.

Returns:

LL2NUM(mpfr_get_emin_min())
EMAX_MAX =

The maximum of the exponents allowed for emax=. This value is implementation dependent, thus a program using MPFR.emax = EMAX_MAX may not be portable.

Returns:

LL2NUM(mpfr_get_emin_max())

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(value = Float::NAN, round: MPFR.default_rounding, prec: MPFR.default_prec) ⇒ MPFR

Create a new MPFR object of a given precision (number of bits), and optionaly initializes it with the given value in the rounding direction.

Parameters:

  • value (Float, Integer, Rational, String, MPFR) (defaults to: Float::NAN)
  • round (Symbol) (defaults to: MPFR.default_rounding)

    rounding direction, used only if value is given

  • prec (Integer) (defaults to: MPFR.default_prec)

    the precision of the MPFR object, that is the size in bits of the significand



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
435
436
437
438
439
440
441
# File 'ext/mpfr_rb.c', line 403

static VALUE mpfrrb_initialize(int argc, VALUE *argv, VALUE self)
{
  if (argc == 0) {
    mpfr_init(mpfrrb_rb2ref(self));
    return self;
  }

  VALUE valrb;
  VALUE kw;
  const ID kwkeys[2] = {id_prec, id_round};
  VALUE kwvalues[2] = {Qundef, Qundef};
  mpfr_rnd_t rnd = mpfrrb_default_rounding;
  mpfr_prec_t prec = mpfr_get_default_prec();
  rb_scan_args(argc, argv, "01:", &valrb, &kw);
  if (!NIL_P(kw)) {
    rb_get_kwargs(kw, kwkeys, 0, 2, kwvalues);
    if (kwvalues[0] != Qundef) {
      if (!rb_integer_type_p(kwvalues[0])) {
        rb_raise(rb_eTypeError, "precision must be an integer, not a %s", rb_obj_classname(kwvalues[0]));
      }
      prec = NUM2ULL(kwvalues[0]);
      if (prec < MPFR_PREC_MIN || prec > MPFR_PREC_MAX) {
        rb_raise(rb_eRangeError, "precision must be between %d and %ld, %ld is invalid", MPFR_PREC_MIN, MPFR_PREC_MAX, prec);
      }
    }
    if (kwvalues[1] != Qundef) {
      rnd = mpfrrb_sym2rnd(kwvalues[1]);
    }
  }

  mpfr_ptr mp = mpfrrb_rb2ref(self);
  mpfr_init2(mp, prec);

  if (!NIL_P(valrb)) {
    mpfrrb_set_internal(mp, rnd, valrb, self);
  }
  
  return self;
}

Class Method Details

.default_precInteger

Return the current default MPFR precision in bits.

Returns:

See Also:



143
144
145
146
147
# File 'ext/mpfr_rb.c', line 143

static VALUE mpfrrb_get_default_prec(VALUE self)
{
  (void)self;
  return LL2NUM(mpfr_get_default_prec());
}

.default_prec=(prec) ⇒ Integer

Set the default precision to be exactly prec bits, where prec can be any integer between PREC_MIN and PREC_MAX. The precision of a variable means the number of bits used to store its significand. All subsequent calls to MPFR.new will use this precision, but previously initialized variables are unaffected. The default precision is set to 53 bits initially.

Returns:



128
129
130
131
132
133
134
135
136
# File 'ext/mpfr_rb.c', line 128

static VALUE mpfrrb_set_default_prec(VALUE self, VALUE prec)
{
  mpfr_prec_t p = NUM2LL(prec);
  if (p < MPFR_PREC_MIN || p > MPFR_PREC_MAX) {
    rb_raise(rb_eRangeError, "precision must be between %d and %ld, %ld is invalid", MPFR_PREC_MIN, MPFR_PREC_MAX, p);
  }
  mpfr_set_default_prec(p);
  return prec;
}

.default_roundingSymbol

Return the default rounding used when the :round keyword is not set.

Returns:

See Also:



296
297
298
299
300
# File 'ext/mpfr_rb.c', line 296

static VALUE mpfrrb_get_default_rounding(VALUE self)
{
  (void)self;
  return rb_id2sym(mpfrrb_default_rounding_id);
}

.default_rounding=(rounding) ⇒ Symbol

Set the default rounding used when the :round keyword is not set. The value can be:

  • :rndn, :round_nearest, :nearest : round to nearest, with the even rounding rule (roundTiesToEven in IEEE 754).
  • :rndu, :round_up, :up : round toward positive infinity (roundTowardPositive in IEEE 754). In case the number to be rounded lies exactly in the middle between two consecutive representable numbers, it is rounded to the one with an even significand; in radix 2, this means that the least significant bit is 0.
  • :rndd, :round_down, :down : round toward negative infinity (roundTowardNegative in IEEE 754).
  • :rndz, :toward_zero, :zero : round toward zero (roundTowardZero in IEEE 754).
  • :rnda, :away_from_zero, :away : round away from zero.
  • :rndf, :faithful_rounding, :faithful : faithful rounding. This feature is currently experimental.
  • :rndna, :nearest_ties_away

Parameters:

Returns:



280
281
282
283
284
285
286
287
288
289
# File 'ext/mpfr_rb.c', line 280

static VALUE mpfrrb_set_default_rounding(VALUE self, VALUE rounding)
{
  if (!RB_SYMBOL_P(rounding))
    rb_raise(rb_eTypeError, "expecting a symbol, not a %s", rb_obj_classname(rounding));

  mpfrrb_default_rounding = mpfrrb_sym2rnd(rounding);
  mpfrrb_default_rounding_id = rb_sym2id(rounding);

  return rounding;
}

.emaxInteger

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

Returns:

See Also:



3856
3857
3858
3859
3860
# File 'ext/mpfr_rb.c', line 3856

static VALUE mpfrrb_get_emax(VALUE self)
{
  (void)self;
  return LL2NUM(mpfr_get_emax());
}

.emax=(e) ⇒ Integer

Note:

If emin > emax and a floating-point value needs to be produced as output, the behavior is undefined.

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

For the subsequent operations, it is the user's responsibility to check that any floating-point value used as an input is in the new exponent range (for example using #check_range). If a floating-point value outside the new exponent range is used as an input, the default behavior is undefined.

Returns:



3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
# File 'ext/mpfr_rb.c', line 3872

static VALUE mpfrrb_set_emax(VALUE self, VALUE e)
{
  (void)self;
  mpfr_exp_t v = NUM2LL(e);
  if (v < mpfr_get_emax_min() || v > mpfr_get_emax_max()) {
    rb_raise(rb_eRangeError, "emax must be between %ld and %ld", mpfr_get_emax_min(), mpfr_get_emax_max());
  }
  mpfr_set_emax(v);
  return e;
}

.eminInteger

Return the (current) smallest exponents 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.

Returns:

See Also:



3822
3823
3824
3825
3826
# File 'ext/mpfr_rb.c', line 3822

static VALUE mpfrrb_get_emin(VALUE self)
{
  (void)self;
  return LL2NUM(mpfr_get_emin());
}

.emin=(e) ⇒ Integer

Note:

If emin > emax and a floating-point value needs to be produced as output, the behavior is undefined.

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

For the subsequent operations, it is the user's responsibility to check that any floating-point value used as an input is in the new exponent range (for example using #check_range). If a floating-point value outside the new exponent range is used as an input, the default behavior is undefined.

Returns:



3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
# File 'ext/mpfr_rb.c', line 3838

static VALUE mpfrrb_set_emin(VALUE self, VALUE e)
{
  (void)self;
  mpfr_exp_t v = NUM2LL(e);
  if (v < mpfr_get_emin_min() || v > mpfr_get_emin_max()) {
    rb_raise(rb_eRangeError, "emin must be between %ld and %ld", mpfr_get_emin_min(), mpfr_get_emin_max());
  }
  mpfr_set_emin(v);
  return e;
}

.free_cacheObject

Free all caches and pools used by MPFR internally.



3727
3728
3729
3730
3731
# File 'ext/mpfr_rb.c', line 3727

static VALUE mpfrrb_free_cache(VALUE self)
{
  mpfr_free_cache();
  return self;
}

.i_2exp(i, e) ⇒ MPFR

Return a new MPFR object from the value of i multiplied by two to the power e. The precision of the returned object is set to accomodate i exactly.

Parameters:

Returns:



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'ext/mpfr_rb.c', line 1044

static VALUE mpfrrb_i_2exp(VALUE self, VALUE i, VALUE e)
{
  (void)self;
  mpfr_exp_t ve = NUM2LL(e);
  VALUE ret;
  if (RB_FIXNUM_P(i)) {
    long int v = NUM2LL(i);
    mpfr_prec_t p;
    if (v > 0) {
      p = (mpfr_prec_t)ceil(log2((double)(v + 1)));
    } else if (v < 0) {
      p = (mpfr_prec_t)ceil(log2((double)(1 - v)));
    } else {
      p = 1;
    }
    ret = mpfrrb_alloc(c_MPFR);
    mpfr_ptr mp = mpfrrb_rb2ref(ret);
    mpfr_init2(mp, p);
    mpfr_set_si_2exp(mp, v, ve, MPFR_RNDN);
  } else if (RB_INTEGER_TYPE_P(i)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(i, mpz);
    int nlz_bits_ret;
    size_t nb_bytes = rb_absint_size(i, &nlz_bits_ret);
    mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret;
    ret = mpfrrb_alloc(c_MPFR);
    mpfr_ptr mp = mpfrrb_rb2ref(ret);
    mpfr_init2(mp, p);
    mpfr_set_z_2exp(mp, mpz, ve, MPFR_RNDN);
    mpz_clear(mpz);
  } else {
    rb_raise(rb_eTypeError, "expecting an integer, not a %s", rb_obj_classname(i));
  }

  return ret;
}

.modf(ipart, fpart, x, round: MPFR.default_rounding) ⇒ Integer

Set simultaneously ipart to the integral part of x and fpart to the fractional part of x, rounded in the direction round with the corresponding precision of ipart and fpart. It is equivalent to ipart.trunc(x, round: rnd) and fpart.frac(x, round: rnd). The variables ipart and fpart must be different. Return 0 iff both results are exact.

Parameters:

  • ipart (MPFR)

    output

  • fpart (MPFR)

    output

  • x (MPFR)

    input

Returns:

See Also:



3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
# File 'ext/mpfr_rb.c', line 3546

static VALUE mpfrrb_modf(int argc, VALUE *argv, VALUE self)
{
  VALUE ipart, fpart, x;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_c_round(argc, argv, &ipart, &fpart, &x);
  mpfr_ptr iop = mpfrrb_rb2ref(ipart);
  mpfr_ptr fop = mpfrrb_rb2ref(fpart);
  mpfr_ptr  op = mpfrrb_rb2ref(x);
  int r = mpfr_modf(iop, fop, op, rnd);
  return INT2NUM(r);
}

.patchesString

Return a string containing the ids of the patches applied to the MPFR library (contents of the PATCHES file), separated by spaces.

Returns:



116
117
118
119
# File 'ext/mpfr_rb.c', line 116

static VALUE mpfrrb_get_patches(VALUE self)
{
  return rb_str_freeze(rb_str_new_cstr(mpfr_get_patches()));
}

.versionString

Return the MPFR version

Returns:



107
108
109
110
# File 'ext/mpfr_rb.c', line 107

static VALUE mpfrrb_get_version(VALUE self)
{
  return rb_str_freeze(rb_str_new_cstr(mpfr_get_version()));
}

Instance Method Details

#%(other) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sollya.rb', line 47

def %(other)
  return to_r % other if other.is_a?(Rational)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.fmod(self, other)
  r
end

#*(other) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/sollya.rb', line 31

def *(other)
  return to_r * other if other.is_a?(Rational)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.mul(self, other)
  r
end

#**(other) ⇒ Object



55
56
57
58
59
60
# File 'lib/sollya.rb', line 55

def **(other)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.pow(self, other)
  r
end

#+(other) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/sollya.rb', line 15

def +(other)
  return to_r + other if other.is_a?(Rational)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.add(self, other)
  r
end

#+@Object



5
6
7
# File 'lib/sollya.rb', line 5

def +@
  self
end

#-(other) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/sollya.rb', line 23

def -(other)
  return to_r - other if other.is_a?(Rational)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.sub(self, other)
  r
end

#-@Object



9
10
11
12
13
# File 'lib/sollya.rb', line 9

def -@
  r = MPFR.new(prec: prec)
  r.neg(self)
  r
end

#/(other) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sollya.rb', line 39

def /(other)
  return to_r / other if other.is_a?(Rational)
  other = other.to_mpfr
  r = MPFR.new(prec: [prec, other.prec].max)
  r.div(self, other)
  r
end

#<=>(other) ⇒ Integer?

Return a positive value if self > other, zero if self = other, and a negative value if self < other. Returns nil if other is not comparable with self.

Returns:



1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
# File 'ext/mpfr_rb.c', line 1993

static VALUE mpfrrb_cmp(VALUE self, VALUE other)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r;

  if (mpfr_nan_p(mp)) {
    return Qnil;
  }
  if (RB_FLOAT_TYPE_P(other)) {
    r = mpfr_cmp_d(mp, NUM2DBL(other));
  } else if (RB_FIXNUM_P(other)) {
    r = mpfr_cmp_si(mp, NUM2LL(other));
  } else if (RB_TYPE_P(other, T_BIGNUM)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(other, mpz);
    r = mpfr_cmp_z(mp, mpz);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(other, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(other, mpq);
    r = mpfr_cmp_q(mp, mpq);
    mpq_clear(mpq);
  }  else if (rb_obj_is_kind_of(other, c_MPFR)) {
    r = mpfr_cmp(mp, mpfrrb_rb2ref(other));
  } else {
    return Qnil;
  }
  return INT2NUM(r);
}

#[](index) ⇒ Integer?

Returns bits from the significand, ignoring the sign or the exponent.

The most significand bit is indexed at 0, the least significand bit is indexed at 1 - prec.

In case self is not finite (an infinite or NaN), nil is retured.

Parameters:

Returns:



4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
# File 'ext/mpfr_rb.c', line 4080

static VALUE mpfrrb_index(VALUE self, VALUE index)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_zero_p(mp)) {
    return INT2NUM(0);
  } else if (!mpfr_regular_p(mp)) {
    return Qnil;
  }
  if (RB_FIXNUM_P(index)) {
    return mpfrrb_index_from_integer(mp, NUM2INT(index));
  } else if (rb_class_of(index) == rb_cRange) {
    VALUE begrb, endrb;
    int excl;
    rb_range_values(index, &begrb, &endrb, &excl);
    int beg = NUM2INT(begrb);
    int end = NUM2INT(endrb);
    int len = (end > beg ? end - beg : beg - end) + (excl ? 0 : 1);
    if (len < 1) {
      return Qnil;
    }
    if (beg < end) {
      end = beg + len - 1; 
    } else {
      end = beg - len + 1;
      int t = end;
      end = beg;
      beg = t;
    }
    return mpfrrb_index_from_range(mp, beg, end);
  } else {
    rb_raise(rb_eTypeError, "expecging an integer or a range, got a %s", rb_obj_classname(index));
  }
}

#abs(a, round: MPFR.default_rounding) ⇒ Integer

Set self to the absolute value of a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'ext/mpfr_rb.c', line 1675

static VALUE mpfrrb_abs(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_abs(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_abs(mpr, mpa, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_abs(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#abs!Integer

Set self to the absolute value of self.

Returns:



1700
1701
1702
1703
1704
1705
# File 'ext/mpfr_rb.c', line 1700

static VALUE mpfrrb_abs_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_abs(mp, mp, MPFR_RNDN);
  return INT2NUM(r);
}

#acos(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-cosine of v, rounded in the direction round. Note that since acos(-1) returns the floating-point number closest to Pi according to the given rounding mode, this number might not be in the output range 0 ≤ self < Pi of the arc-cosine function; still, the result lies in the image of the output range by the rounding function. The same holds for asin(-1), asin(1), atan(-Inf), atan(+Inf) or for atan(v) with large v and small precision of self.

Returns:



2860
2861
2862
2863
# File 'ext/mpfr_rb.c', line 2860

static VALUE mpfrrb_acos(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(acos);
}

#acosh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the inverse hyperbolic cossine of v, rounded in the direction round.

Returns:



3078
3079
3080
3081
# File 'ext/mpfr_rb.c', line 3078

static VALUE mpfrrb_acosh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(acosh);
}

#acos(v, round: MPFR.default_rounding) ⇒ Integer

Set self to acos(v) divided by Pi, rounded in the direction round.

Returns:



2932
2933
2934
2935
# File 'ext/mpfr_rb.c', line 2932

static VALUE mpfrrb_acospi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(acospi);
}

#acosu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-cosine(x) multiplied by u and divided by 2 Pi. For example, if u equals 360, #acosu yields the arc-cosine in degrees.

Parameters:

Returns:



2898
2899
2900
2901
# File 'ext/mpfr_rb.c', line 2898

static VALUE mpfrrb_acosu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(acosu);
}

#add(a, b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to a + b rounded in the direction round.

Parameters:

Returns:



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'ext/mpfr_rb.c', line 1089

static VALUE mpfrrb_add(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  a = mpfrrb_object_to_mpfr(a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(a);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_add_d(mpr, mpa, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_add_si(mpr, mpa, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_add_z(mpr, mpa, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_add_q(mpr, mpa, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_add(mpr, mpa, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#add!(b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to self + b rounded in the direction round.

Parameters:

Returns:



1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'ext/mpfr_rb.c', line 1126

static VALUE mpfrrb_add_B(int argc, VALUE *argv, VALUE self)
{
  VALUE b;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_add_d(mpr, mpr, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_add_si(mpr, mpr, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_add_z(mpr, mpr, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_add_q(mpr, mpr, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_add(mpr, mpr, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#agm(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self to the arithmetic-geometric mean of x and y, rounded in the direction round. The arithmetic-geometric mean is the common limit of the sequences u_n and v_n, where u_0 = x, v_0 = y, u_(n+1) is the arithmetic mean of u_n and v_n, and v_(n+1) is the geometric mean of u_n and v_n.

If any operand is negative and the other one is not zero, set self to NaN.

If any operand is zero and the other one is finite (resp. infinite), set self to +0 (resp. NaN).

Returns:



3311
3312
3313
3314
# File 'ext/mpfr_rb.c', line 3311

static VALUE mpfrrb_agm(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(agm);
}

#ai(x, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the Airy function Ai on x, rounded in the direction round.

When x is NaN, self is always set to NaN. When x is +Inf or -Inf, self is set to +0.

The current implementation is not intended to be used with large arguments. It works with abs(x) typically smaller than 500. For larger arguments, other methods should be used and will be implemented in a future version.

Returns:



3328
3329
3330
3331
# File 'ext/mpfr_rb.c', line 3328

static VALUE mpfrrb_ai(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(ai);
}

#asin(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-sine of v, rounded in the direction round. Note that since acos(-1) returns the floating-point number closest to Pi according to the given rounding mode, this number might not be in the output range 0 ≤ self < Pi of the arc-cosine function; still, the result lies in the image of the output range by the rounding function. The same holds for asin(-1), asin(1), atan(-Inf), atan(+Inf) or for atan(v) with large v and small precision of self.

Returns:



2873
2874
2875
2876
# File 'ext/mpfr_rb.c', line 2873

static VALUE mpfrrb_asin(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(asin);
}

#asinh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the inverse hyperbolic sine of v, rounded in the direction round.

Returns:



3088
3089
3090
3091
# File 'ext/mpfr_rb.c', line 3088

static VALUE mpfrrb_asinh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(asinh);
}

#asin(v, round: MPFR.default_rounding) ⇒ Integer

Set self to asin(v) divided by Pi, rounded in the direction round.

Returns:



2942
2943
2944
2945
# File 'ext/mpfr_rb.c', line 2942

static VALUE mpfrrb_asinpi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(asinpi);
}

#asinu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-sine(x) multiplied by u and divided by 2 Pi. For example, if u equals 360, #asinu yields the arc-sine in degrees.

Parameters:

Returns:



2910
2911
2912
2913
# File 'ext/mpfr_rb.c', line 2910

static VALUE mpfrrb_asinu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(asinu);
}

#atan(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-tangent of v, rounded in the direction round. Note that since acos(-1) returns the floating-point number closest to Pi according to the given rounding mode, this number might not be in the output range 0 ≤ self < Pi of the arc-cosine function; still, the result lies in the image of the output range by the rounding function. The same holds for asin(-1), asin(1), atan(-Inf), atan(+Inf) or for atan(v) with large v and small precision of self.

Returns:



2886
2887
2888
2889
# File 'ext/mpfr_rb.c', line 2886

static VALUE mpfrrb_atan(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(atan);
}

#atan2(y, x, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-tangent2 of y and x, rounded in the direction round. If x > 0, then atan2(y, x) returns atan(y/x); if x < 0, then atan2(y, x) returns the sign of y multiplied by Pi - atan(abs(y/x)), thus a number from -Pi to Pi. As for #atan, in case the exact mathematical result is +Pi or -Pi, its rounded result might be outside the function output range.

Special values are handled as described in the ISO C99 and IEEE 754 standards for the atan2 function:

  • atan2(+0, -0) returns +Pi.
  • atan2(-0, -0) returns -Pi.
  • atan2(+0, +0) returns +0.
  • atan2(-0, +0) returns -0.
  • atan2(+0, x) returns +Pi for x < 0.
  • atan2(-0, x) returns -Pi for x < 0.
  • atan2(+0, x) returns +0 for x > 0.
  • atan2(-0, x) returns -0 for x > 0.
  • atan2(y, 0) returns -Pi/2 for y < 0.
  • atan2(y, 0) returns +Pi/2 for y > 0.
  • atan2(+Inf, -Inf) returns +3*Pi/4.
  • atan2(-Inf, -Inf) returns -3*Pi/4.
  • atan2(+Inf, +Inf) returns +Pi/4.
  • atan2(-Inf, +Inf) returns -Pi/4.
  • atan2(+Inf, x) returns +Pi/2 for finite x.
  • atan2(-Inf, x) returns -Pi/2 for finite x.
  • atan2(y, -Inf) returns +Pi for finite y > 0.
  • atan2(y, -Inf) returns -Pi for finite y < 0.
  • atan2(y, +Inf) returns +0 for finite y > 0.
  • atan2(y, +Inf) returns -0 for finite y < 0.

Returns:



2987
2988
2989
2990
# File 'ext/mpfr_rb.c', line 2987

static VALUE mpfrrb_atan2(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(atan2);
}

#atan2pi(y, x, round: MPFR.default_rounding) ⇒ Integer

The same as #atan2u with u = 2.

Returns:



3008
3009
3010
3011
# File 'ext/mpfr_rb.c', line 3008

static VALUE mpfrrb_atan2pi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(atan2pi);
}

#atan2u(y, x, u, round: MPFR.default_rounding) ⇒ Integer

Behaves similarly to #atan2 except the result is multiplied by u and divided by 2 Pi.

Parameters:

Returns:



2998
2999
3000
3001
# File 'ext/mpfr_rb.c', line 2998

static VALUE mpfrrb_atan2u(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_UL_FUNC_BODY(atan2u);
}

#atanh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the inverse hyperbolic tangent of v, rounded in the direction round.

Returns:



3098
3099
3100
3101
# File 'ext/mpfr_rb.c', line 3098

static VALUE mpfrrb_atanh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(atanh);
}

#atanpi(v, round: MPFR.default_rounding) ⇒ Integer

Set self to atan(v) divided by Pi, rounded in the direction round.

Returns:



2952
2953
2954
2955
# File 'ext/mpfr_rb.c', line 2952

static VALUE mpfrrb_atanpi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(atanpi);
}

#atanu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the arc-tangent(x) multiplied by u and divided by 2 Pi. For example, if u equals 360, #asinu yields the arc-tangent in degrees.

Parameters:

Returns:



2922
2923
2924
2925
# File 'ext/mpfr_rb.c', line 2922

static VALUE mpfrrb_atanu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(atanu);
}

#beta(x, y, round: MPFR.default_rounding) ⇒ Integer

Note:

The current code does not try to avoid internal overflow or underflow, and might use a huge internal precision in some cases.

Set self to the value of the Beta function at arguments x and x.

Returns:



3182
3183
3184
3185
# File 'ext/mpfr_rb.c', line 3182

static VALUE mpfrrb_beta(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(beta);
}

#cbrt(a, round: MPFR.default_rounding) ⇒ Integer

Set self to the cubic root of a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'ext/mpfr_rb.c', line 1547

static VALUE mpfrrb_cbrt(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_cbrt(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_cbrt(mpr, mpa, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_cbrt(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#cbrt!(round: MPFR.default_rounding) ⇒ Integer

Set self to the cubic root of self, rounded int the direction round.

Parameters:

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1574
1575
1576
1577
1578
1579
1580
# File 'ext/mpfr_rb.c', line 1574

static VALUE mpfrrb_cbrt_B(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_cbrt(mp, mp, rnd);
  return INT2NUM(r);
}

#ceil(op) ⇒ Integer

Set self to op rounded to the next higher or equal representable integer.

Returns:



3347
3348
3349
3350
# File 'ext/mpfr_rb.c', line 3347

static VALUE mpfrrb_ceil(VALUE self, VALUE op)
{
  MPFRRB_ONE_ARG_NO_RND_FUNC_BODY(ceil);
}

#ceil!Integer

Set self to self rounded to the next higher or equal representable integer.

Returns:



3404
3405
3406
3407
3408
# File 'ext/mpfr_rb.c', line 3404

static VALUE mpfrrb_ceil_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_ceil(mp, mp));
}

#coerce(num) ⇒ Array<MPFR,Rational>

Returns an array with both num and self represented as MPFR objects. If num is a Rational, the returned array contains both num and self as rationals.

Returns:



2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
# File 'ext/mpfr_rb.c', line 2030

static VALUE mpfrrb_coerce(VALUE self, VALUE num)
{
  if (RB_FLOAT_TYPE_P(num)) {
    return rb_ary_new_from_args(2, mpfrrb_Float_to_mpfr(num), self);
  } else if (RB_INTEGER_TYPE_P(num)) {
    return rb_ary_new_from_args(2, mpfrrb_Integer_to_mpfr(num), self);
  } else if (RB_TYPE_P(num, T_RATIONAL)) {
    return rb_ary_new_from_args(2, num, mpfrrb_to_r(self));
  } else {
    rb_raise(rb_eArgError, "invalid value: %" PRIsVALUE, num);
  }
}

#compound(x, n, round: MPFR.default_rounding) ⇒ Integer

Set self to the power n of one plus x, following IEEE 754 for the special cases and exceptions. In particular:

  • When x < -1, self is set to NaN.
  • When n is zero and x is NaN (like any value greater or equal to -1), self is set to 1.
  • When x = -1, self is set to +Inf for n < 0, and to +0 for n > 0. The other special cases follow the usual rules.

Parameters:

Returns:



2708
2709
2710
2711
# File 'ext/mpfr_rb.c', line 2708

static VALUE mpfrrb_compound(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_SL_FUNC_BODY(compound_si);
}

#cos(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the cosine of v, rounded in the direction round.

Returns:



2718
2719
2720
2721
# File 'ext/mpfr_rb.c', line 2718

static VALUE mpfrrb_cos(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(cos);
}

#cosh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic cosine of v, rounded in the direction round.

Returns:



3018
3019
3020
3021
# File 'ext/mpfr_rb.c', line 3018

static VALUE mpfrrb_cosh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(cosh);
}

#cospi(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the cosine of v multiplied by Pi, rounded in the direction round. See the description of #cosu for special values.

Returns:



2795
2796
2797
2798
# File 'ext/mpfr_rb.c', line 2795

static VALUE mpfrrb_cospi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(cospi);
}

#cosu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the cosine of x, multiplied by 2 Pi and divided by u, rounded in the direction round.

For example, if u equals 360, one gets the cosine for x in degrees.

When op multiplied by 2 and divided by u is a half-integer, the result is +0, following IEEE 754 (cosPi), so that the function is even.

Parameters:

Returns:



2753
2754
2755
2756
# File 'ext/mpfr_rb.c', line 2753

static VALUE mpfrrb_cosu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(cosu);
}

#cot(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the cotangent of v, rounded in the direction round.

Returns:



2847
2848
2849
2850
# File 'ext/mpfr_rb.c', line 2847

static VALUE mpfrrb_cot(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(cot);
}

#coth(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic cotangent of v, rounded in the direction round.

Returns:



3068
3069
3070
3071
# File 'ext/mpfr_rb.c', line 3068

static VALUE mpfrrb_coth(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(coth);
}

#cot(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the cosecant of v, rounded in the direction round.

Returns:



2837
2838
2839
2840
# File 'ext/mpfr_rb.c', line 2837

static VALUE mpfrrb_csc(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(csc);
}

#csch(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic cosecant of v, rounded in the direction round.

Returns:



3058
3059
3060
3061
# File 'ext/mpfr_rb.c', line 3058

static VALUE mpfrrb_csch(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(csch);
}

#digamma(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the Digamma (sometimes also called Psi) function on v, rounded in the directionround. When v is a negative integer, set self to NaN.

Returns:



3171
3172
3173
3174
# File 'ext/mpfr_rb.c', line 3171

static VALUE mpfrrb_digamma(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(digamma);
}

#dim(a, b, round: MPFR.default_rounding) ⇒ Integer

Set self to the positive difference of a and b. That is: a - b rounded in the direction round if a > b, +0 if ab, and NaN if a or b is NaN.

Parameters:

  • a (MPFR)
  • b (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'ext/mpfr_rb.c', line 1716

static VALUE mpfrrb_dim(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  a = mpfrrb_object_to_mpfr(a);
  b = mpfrrb_object_to_mpfr(b);
  int r = mpfr_dim(mpfrrb_rb2ref(self), mpfrrb_rb2ref(a), mpfrrb_rb2ref(b), rnd);
  return INT2NUM(r);
}

#div(a, b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to a / b rounded in the direction round.

Parameters:

Returns:



1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
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
1373
# File 'ext/mpfr_rb.c', line 1329

static VALUE mpfrrb_div(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  a = mpfrrb_object_to_mpfr(a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (rb_obj_is_kind_of(a, c_MPFR)) {
    mpfr_ptr mpa = mpfrrb_rb2ref(a);
    if (RB_FLOAT_TYPE_P(b)) {
      r = mpfr_div_d(mpr, mpa, NUM2DBL(b), rnd);
    } else if (RB_FIXNUM_P(b)) {
      r = mpfr_div_si(mpr, mpa, NUM2LL(b), rnd);
    } else if (RB_INTEGER_TYPE_P(b)) {
      mpz_t mpz;
      mpz_init(mpz);
      mpfrrb_bignum_to_mpz(b, mpz);
      r = mpfr_div_z(mpr, mpa, mpz, rnd);
      mpz_clear(mpz);
    } else if (RB_TYPE_P(b, T_RATIONAL)) {
      mpq_t mpq;
      mpq_init(mpq);
      mpfrrb_rational_to_mpq(b, mpq);
      r = mpfr_div_q(mpr, mpa, mpq, rnd);
      mpq_clear(mpq);
    } else {
      b = mpfrrb_object_to_mpfr(b);
      r = mpfr_div(mpr, mpa, mpfrrb_rb2ref(b), rnd);
    }
  } else {
    b = mpfrrb_object_to_mpfr(b);
    mpfr_ptr mpb = mpfrrb_rb2ref(b);
    if (RB_FLOAT_TYPE_P(a)) {
      r = mpfr_d_div(mpr, NUM2DBL(a), mpb, rnd);
    } else if (RB_FIXNUM_P(a)) {
      r = mpfr_si_div(mpr, NUM2LL(a), mpb, rnd);
    } else {
      a = mpfrrb_object_to_mpfr(a);
      r = mpfr_div(mpr, mpfrrb_rb2ref(a), mpb, rnd);
    }

  }

  return INT2NUM(r);
}

#div!(b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to self / b rounded in the direction round.

Parameters:

Returns:



1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
# File 'ext/mpfr_rb.c', line 1381

static VALUE mpfrrb_div_B(int argc, VALUE *argv, VALUE self)
{
  VALUE b;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_div_d(mpr, mpr, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_div_si(mpr, mpr, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_div_z(mpr, mpr, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_div_q(mpr, mpr, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_div(mpr, mpr, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#div_2i(a, n, round: MPFR.default_rounding) ⇒ Integer

Set self to a divided by 2 raised to n, rounded int the direction round.

Parameters:

Returns:



1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
# File 'ext/mpfr_rb.c', line 1781

static VALUE mpfrrb_div_2i(int argc, VALUE *argv, VALUE self)
{
  VALUE a, n;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_div_2si(mpr, mpa, i, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_div_2si(mpr, mpa, i, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_div_2si(mpr, mpfrrb_rb2ref(a), i, rnd);
  }
  return INT2NUM(r);
}

#div_2i!(n, round: MPFR.default_rounding) ⇒ Integer

Set self to self divided by 2 raised to n, rounded int the direction round.

Parameters:

  • n (Integer)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1810
1811
1812
1813
1814
1815
1816
1817
1818
# File 'ext/mpfr_rb.c', line 1810

static VALUE mpfrrb_div_2i_B(int argc, VALUE *argv, VALUE self)
{
  VALUE n;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_div_2si(mp, mp, i, rnd);
  return INT2NUM(r);
}

#eint(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the exponential integral of v, rounded in the directionround. This is the sum of Euler’s constant, of the logarithm of the absolute value of v, and of the sum for k from 1 to infinity of v to the power k, divided by k and the factorial of k. For positive v, it corresponds to the Ei function at v (see formula 5.1.10 from the Handbook of Mathematical Functions from Abramowitz and Stegun), and for negative v, to the opposite of the E1 function (sometimes called eint1) at -v (formula 5.1.1 from the same reference).

Returns:



3111
3112
3113
3114
# File 'ext/mpfr_rb.c', line 3111

static VALUE mpfrrb_eint(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(eint);
}

#erandom(round: MPFR.default_rounding) ⇒ Integer

Generate a random floating-point number according to an exponential distribution, with mean one. Other characteristics are identical to #nrandom.

Returns:



3979
3980
3981
3982
3983
3984
3985
# File 'ext/mpfr_rb.c', line 3979

static VALUE mpfrrb_erandom(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_erandom(mp, mpfrrb_gmp_randstate, rnd);
  return INT2NUM(r);
}

#erf(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the error function on 'v', rounded in the direction round.

Returns:



3202
3203
3204
3205
# File 'ext/mpfr_rb.c', line 3202

static VALUE mpfrrb_erf(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(erf);
}

#erfc(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the complementary error function on 'v', rounded in the direction round.

Returns:



3212
3213
3214
3215
# File 'ext/mpfr_rb.c', line 3212

static VALUE mpfrrb_erfc(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(erfc);
}

#exp(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the exponential of v, rounded in the direction round.

Returns:



2496
2497
2498
2499
# File 'ext/mpfr_rb.c', line 2496

static VALUE mpfrrb_exp(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(exp);
}

#exp10(v, round: MPFR.default_rounding) ⇒ Integer

Set self to 10 power of v, rounded in the direction round.

Returns:



2516
2517
2518
2519
# File 'ext/mpfr_rb.c', line 2516

static VALUE mpfrrb_exp10(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(exp10);
}

#exp10m1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to 10 power of v followed by a subtraction by one, rounded in the direction round.

Returns:



2546
2547
2548
2549
# File 'ext/mpfr_rb.c', line 2546

static VALUE mpfrrb_exp10m1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(exp10m1);
}

#exp2(v, round: MPFR.default_rounding) ⇒ Integer

Set self to 2 power of v, rounded in the direction round.

Returns:



2506
2507
2508
2509
# File 'ext/mpfr_rb.c', line 2506

static VALUE mpfrrb_exp2(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(exp2);
}

#exp2m1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to 2 power of v followed by a subtraction by one, rounded in the direction round.

Returns:



2536
2537
2538
2539
# File 'ext/mpfr_rb.c', line 2536

static VALUE mpfrrb_exp2m1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(exp2m1);
}

#expm1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the exponential of v followed by a subtraction by one, rounded in the direction round.

Returns:



2526
2527
2528
2529
# File 'ext/mpfr_rb.c', line 2526

static VALUE mpfrrb_expm1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(expm1);
}

#exponentInteger?

Return the exponent of self, assuming that self is a non-zero ordinary number and the significand is considered in [1, 2).

Returns:



726
727
728
729
730
731
732
733
734
735
# File 'ext/mpfr_rb.c', line 726

static VALUE mpfrrb_get_exponent(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp) || mpfr_inf_p(mp)) {
    return Qnil;
  } else if (mpfr_zero_p(mp)) {
    return INT2NUM(0);
  }
  return LL2NUM(mpfr_get_exp(mp) - 1ll);
}

#exponent=(e) ⇒ Object

Set the exponent of self to e, assuming the significand in [1, 2). self must be an ordinary number (not nan, infinity nor zero).

Parameters:



742
743
744
745
746
747
748
749
750
751
752
# File 'ext/mpfr_rb.c', line 742

static VALUE mpfrrb_set_exponent(VALUE self, VALUE e)
{
  mpfr_exp_t p = NUM2LONG(e);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (!mpfr_regular_p(mp)) {
    rb_raise(rb_eRuntimeError, "trying to set the exponent of a non-regular MPFR object");
  }
  p += 1;
  mpfr_set_exp(mp, p);
  return e;
}

#fac(n, round: MPFR.default_rounding) ⇒ Integer

Set self to the factorial of n, rounded int the direction round.

Parameters:

  • n (Integer)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
# File 'ext/mpfr_rb.c', line 1827

static VALUE mpfrrb_fac(int argc, VALUE *argv, VALUE self)
{
  VALUE n;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (i < 0) {
    rb_raise(rb_eRangeError, "factorial of negative value");
  }
  int r = mpfr_fac_ui(mp, i, rnd);
  return INT2NUM(r);
}

#finite?Boolean

Tels if self is neither an infinity nor NaN.

Returns:

  • (Boolean)


785
786
787
788
789
# File 'ext/mpfr_rb.c', line 785

static VALUE mpfrrb_is_finite(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return mpfr_number_p(mp) ? Qtrue : Qfalse;
}

#floor(op) ⇒ Integer

Set self to op rounded to the next lower or equal representable integer.

Returns:



3356
3357
3358
3359
# File 'ext/mpfr_rb.c', line 3356

static VALUE mpfrrb_floor(VALUE self, VALUE op)
{
  MPFRRB_ONE_ARG_NO_RND_FUNC_BODY(floor);
}

#floor!Integer

Set self to self rounded to the next lower or equal representable integer.

Returns:



3414
3415
3416
3417
3418
# File 'ext/mpfr_rb.c', line 3414

static VALUE mpfrrb_floor_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_floor(mp, mp));
}

#fma(a, b, c, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) plus c, rounded in the direction round. Concerning special values (signed zeros, infinities, NaN), these functions behave like a multiplication followed by a separate addition or subtraction. That is, the fused operation matters only for rounding.

Parameters:

  • a,

    b, c [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
# File 'ext/mpfr_rb.c', line 1850

static VALUE mpfrrb_fma(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b, c;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_c_round(argc, argv, &a, &b, &c);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(b));
  mpfr_ptr mpc = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(c));
  int r = mpfr_fma(mpr, mpa, mpb, mpc, rnd);
  return INT2NUM(r);
}

#fma!(a, b, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) plus self, rounded in the direction round. Concerning special values (signed zeros, infinities, NaN), these functions behave like a multiplication followed by a separate addition or subtraction. That is, the fused operation matters only for rounding.

Parameters:

  • a,

    b [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
# File 'ext/mpfr_rb.c', line 1872

static VALUE mpfrrb_fma_B(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(b));
  int r = mpfr_fma(mpr, mpa, mpb, mpr, rnd);
  return INT2NUM(r);
}

#fmma(a, b, c, d, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) plus (c times d), rounded in the direction round. In case the computation of a times b overflows or underflows (or that of c times d), the result is computed as if the two intermediate products were computed with rounding toward zero.

Parameters:

  • a,

    b, c, d [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
# File 'ext/mpfr_rb.c', line 1935

static VALUE mpfrrb_fmma(int argc, VALUE *argv, VALUE self)
{
  VALUE v[4];
  mpfr_rnd_t rnd = mpfrrb_get_4_args_and_round(argc, argv, v);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[0]));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[1]));
  mpfr_ptr mpc = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[2]));
  mpfr_ptr mpd = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[3]));
  int r = mpfr_fmma(mpr, mpa, mpb, mpc, mpd, rnd);
  return INT2NUM(r);
}

#fmms(a, b, c, d, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) minus (c times d), rounded in the direction round. In case the computation of a times b overflows or underflows (or that of c times d), the result is computed as if the two intermediate products were computed with rounding toward zero.

Parameters:

  • a,

    b, c, d [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
# File 'ext/mpfr_rb.c', line 1957

static VALUE mpfrrb_fmms(int argc, VALUE *argv, VALUE self)
{
  VALUE v[4];
  mpfr_rnd_t rnd = mpfrrb_get_4_args_and_round(argc, argv, v);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[0]));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[1]));
  mpfr_ptr mpc = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[2]));
  mpfr_ptr mpd = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(v[3]));
  int r = mpfr_fmms(mpr, mpa, mpb, mpc, mpd, rnd);
  return INT2NUM(r);
}

#fmod(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self the the value of x - ny, rounded according to the direction round.

n is the integer quotient of x divided by y, rounded toward zero.

Special values are handled as described in Section F.9.7.1 of the ISO C99 standard:

  • If x is infinite or y is zero, self is NaN.
  • If y is infinite and x is finite, self is x rounded to the precision of self.
  • If self is zero, it has the sign of x. The return value is the ternary value corresponding to self.

Returns:

See Also:



3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
# File 'ext/mpfr_rb.c', line 3570

static VALUE mpfrrb_fmod(int argc, VALUE *argv, VALUE self)
{
  VALUE x, y;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &x, &y);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(x)) {
    MPFR_DECL_INIT(mpx, 53);
    mpfr_set_d(mpx, NUM2DBL(x), MPFR_RNDN);
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long ly = NUM2LL(y);
      if (ly >= 0) {
        r = mpfr_fmod_ui(mpr, mpx, ly, rnd);
      } else {
        MPFR_DECL_INIT(mpy, 64);
        mpfr_set_si(mpy, NUM2LL(y), MPFR_RNDN);
        r = mpfr_fmod(mpr, mpx, mpy, rnd);
      }
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy; mpz_init(mpzy); mpfrrb_bignum_to_mpz(y, mpzy);
      int nlz_bits_ret; size_t nb_bytes = rb_absint_size(y, &nlz_bits_ret);
      mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpy; mpfr_init2(mpy, p);
      mpfr_set_z(mpy, mpzy, MPFR_RNDN); mpz_clear(mpzy);
      r = mpfr_fmod(mpr, mpx, mpy, rnd); mpfr_clear(mpy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    }
  } else if (RB_FIXNUM_P(x)) {
    MPFR_DECL_INIT(mpx, 64);
    mpfr_set_si(mpx, NUM2LL(x), MPFR_RNDN);
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long ly = NUM2LL(y);
      if (ly >= 0) {
        r = mpfr_fmod_ui(mpr, mpx, ly, rnd);
      } else {
        MPFR_DECL_INIT(mpy, 64);
        mpfr_set_si(mpy, NUM2LL(y), MPFR_RNDN);
        r = mpfr_fmod(mpr, mpx, mpy, rnd);
      }
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy; mpz_init(mpzy); mpfrrb_bignum_to_mpz(y, mpzy);
      int nlz_bits_ret; size_t nb_bytes = rb_absint_size(y, &nlz_bits_ret);
      mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpy; mpfr_init2(mpy, p);
      mpfr_set_z(mpy, mpzy, MPFR_RNDN); mpz_clear(mpzy);
      r = mpfr_fmod(mpr, mpx, mpy, rnd); mpfr_clear(mpy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    }
  } else if (RB_TYPE_P(x, T_BIGNUM)) {
    mpz_t mpz; mpz_init(mpz); mpfrrb_bignum_to_mpz(x, mpz);
    int nlz_bits_ret; size_t nb_bytes = rb_absint_size(x, &nlz_bits_ret);
    mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpx; mpfr_init2(mpx, p);
    mpfr_set_z(mpx, mpz, MPFR_RNDN); mpz_clear(mpz);
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long ly = NUM2LL(y);
      if (ly >= 0) {
        r = mpfr_fmod_ui(mpr, mpx, ly, rnd);
      } else {
        MPFR_DECL_INIT(mpy, 64);
        mpfr_set_si(mpy, NUM2LL(y), MPFR_RNDN);
        r = mpfr_fmod(mpr, mpx, mpy, rnd);
      }
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy; mpz_init(mpzy); mpfrrb_bignum_to_mpz(y, mpzy);
      int nlz_bits_ret; size_t nb_bytes = rb_absint_size(y, &nlz_bits_ret);
      mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpy; mpfr_init2(mpy, p);
      mpfr_set_z(mpy, mpzy, MPFR_RNDN); mpz_clear(mpzy);
      r = mpfr_fmod(mpr, mpx, mpy, rnd); mpfr_clear(mpy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    }
    mpfr_clear(mpx);
  } else {
    mpfr_ptr mpx = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(x));
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long ly = NUM2LL(y);
      if (ly >= 0) {
        r = mpfr_fmod_ui(mpr, mpx, ly, rnd);
      } else {
        MPFR_DECL_INIT(mpy, 64);
        mpfr_set_si(mpy, NUM2LL(y), MPFR_RNDN);
        r = mpfr_fmod(mpr, mpx, mpy, rnd);
      }
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy; mpz_init(mpzy); mpfrrb_bignum_to_mpz(y, mpzy);
      int nlz_bits_ret; size_t nb_bytes = rb_absint_size(y, &nlz_bits_ret);
      mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpy; mpfr_init2(mpy, p);
      mpfr_set_z(mpy, mpzy, MPFR_RNDN); mpz_clear(mpzy);
      r = mpfr_fmod(mpr, mpx, mpy, rnd); mpfr_clear(mpy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_fmod(mpr, mpx, mpy, rnd);
    }
  }
  return INT2NUM(r);
}

#fms(a, b, c, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) minus c, rounded in the direction round. Concerning special values (signed zeros, infinities, NaN), these functions behave like a multiplication followed by a separate addition or subtraction. That is, the fused operation matters only for rounding.

Parameters:

  • a,

    b, c [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
# File 'ext/mpfr_rb.c', line 1893

static VALUE mpfrrb_fms(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b, c;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_c_round(argc, argv, &a, &b, &c);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(b));
  mpfr_ptr mpc = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(c));
  int r = mpfr_fms(mpr, mpa, mpb, mpc, rnd);
  return INT2NUM(r);
}

#fms!(a, b, round: MPFR.default_rounding) ⇒ Integer

Set self to (a times b) minus self, rounded in the direction round. Concerning special values (signed zeros, infinities, NaN), these functions behave like a multiplication followed by a separate addition or subtraction. That is, the fused operation matters only for rounding.

Parameters:

  • a,

    b [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'ext/mpfr_rb.c', line 1915

static VALUE mpfrrb_fms_B(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(b));
  int r = mpfr_fms(mpr, mpa, mpb, mpr, rnd);
  return INT2NUM(r);
}

#frac(op, round: MPFR.default_rounding) ⇒ Integer

Set self to the fractional part of op, having the same sign as op, rounded in the direction round. Unlike in #rint, round affects only how the exact fractional part is rounded, not how the fractional part is generated. When op is an integer or an infinity, set self to zero with the same sign as op.

Returns:



3527
3528
3529
3530
# File 'ext/mpfr_rb.c', line 3527

static VALUE mpfrrb_frac(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(frac);
}

#gamma(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the Gamma function on v, , rounded in the directionround. When v is a negative integer, self is set to NaN.

Returns:



3133
3134
3135
3136
# File 'ext/mpfr_rb.c', line 3133

static VALUE mpfrrb_gamma(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(gamma);
}

#gamma_inc(x, y, round: MPFR.default_rounding) ⇒ Integer

Note:

The current implementation of gamma_inc is slow for large values of self or x, in which case some internal overflow might also occur.

Set self to the value of the incomplete Gamma function on x and y, rounded in the direction round. (In the literature, gamma_inc is called upper incomplete Gamma function, or sometimes complementary incomplete Gamma function.) When y is zero and x is a negative integer, self is set to NaN.

Returns:



3147
3148
3149
3150
# File 'ext/mpfr_rb.c', line 3147

static VALUE mpfrrb_gamma_inc(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(gamma_inc);
}

#get_d_2exp(round: MPFR.default_rounding) ⇒ Array<Float, Integer>

Return [d, e] such that 1 ≤ abs(d) < 2 and d times 2 raised to e equals self rounded to double precision, using the given rounding mode.

Returns:



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'ext/mpfr_rb.c', line 1002

static VALUE mpfrrb_get_d_2exp(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  long e = 0;
  double d = mpfr_get_d_2exp(&e, mpfrrb_rb2ref(self), rnd);
  d *= 2.0;
  e -= 1;
  VALUE rbd = DBL2NUM(d);
  VALUE rbe = isfinite(d) ? LONG2NUM(e) : Qnil;
  return rb_ary_new_from_args(2, rbd, rbe);
}

#get_i_2expArray<Integer, Integer>

Return [i, e] such that self exactly equals i times 2 raised to the power e.

Returns:



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'ext/mpfr_rb.c', line 1018

static VALUE mpfrrb_get_i_2exp(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp)) {
    rb_raise(rb_eFloatDomainError, "NaN");
  } else if (mpfr_inf_p(mp)) {
    rb_raise(rb_eFloatDomainError, "Infinity");
  } else if (mpfr_zero_p(mp)) {
    return rb_ary_new_from_args(2, INT2NUM(0), INT2NUM(0));
  }

  mpz_t mpz;
  mpz_init(mpz);
  mpfr_exp_t e = mpfr_get_z_2exp(mpz, mpfrrb_rb2ref(self));
  VALUE rbi = mpfrrb_mpz_to_bignum(mpz);
  mpz_clear(mpz);
  return rb_ary_new_from_args(2, rbi, LL2NUM(e));
}

#hypot(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self to the Euclidean norm of x and y, i.e. the square root to the sum of the squares of x and y, rounded in the direction round.

Parameters:

  • x,

    y [MPFR]

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
# File 'ext/mpfr_rb.c', line 1977

static VALUE mpfrrb_hypot(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
  mpfr_ptr mpb = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(b));
  int r = mpfr_hypot(mpr, mpa, mpb, rnd);
  return INT2NUM(r);
}

#infinite?Boolean

Tels if self is an infinity.

Returns:

  • (Boolean)


776
777
778
779
# File 'ext/mpfr_rb.c', line 776

static VALUE mpfrrb_is_infinite(VALUE self)
{
  return mpfr_inf_p(mpfrrb_rb2ref(self)) ? Qtrue : Qfalse;
}

#integer?Boolean

Tels if the fractional part of self is null.

Returns:

  • (Boolean)


795
796
797
798
799
# File 'ext/mpfr_rb.c', line 795

static VALUE mpfrrb_is_integer(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return mpfr_integer_p(mp) ? Qtrue : Qfalse;
}

#j0(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the first kind Bessel function of order 0, on v, rounded in the direction round. When v is NaN, self is always set to NaN. When v is positive or negative infinity, self is set to +0. When v is zero, and n is not zero, self is set to +0 or -0 depending on the parity and sign of n, and the sign of v.

Returns:



3225
3226
3227
3228
# File 'ext/mpfr_rb.c', line 3225

static VALUE mpfrrb_j0(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(j0);
}

#j1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the first kind Bessel function of order 1, on v, rounded in the direction round. When v is NaN, self is always set to NaN. When v is positive or negative infinity, self is set to +0. When v is zero, and n is not zero, self is set to +0 or -0 depending on the parity and sign of n, and the sign of v.

Returns:



3238
3239
3240
3241
# File 'ext/mpfr_rb.c', line 3238

static VALUE mpfrrb_j1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(j1);
}

#jn(n, v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the first kind Bessel function of order n, on v, rounded in the direction round. When v is NaN, self is always set to NaN. When v is positive or negative infinity, self is set to +0. When v is zero, and n is not zero, self is set to +0 or -0 depending on the parity and sign of n, and the sign of v.

Parameters:

Returns:



3253
3254
3255
3256
# File 'ext/mpfr_rb.c', line 3253

static VALUE mpfrrb_jn(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_INT_MP_RND_FUNC_BODY(jn);
}

#li2(v, round: MPFR.default_rounding) ⇒ Integer

Set self to real part of the dilogarithm of v, rounded in the directionround. MPFR defines the dilogarithm function as the integral of −log(1−t)/t from 0 to v.

Returns:



3122
3123
3124
3125
# File 'ext/mpfr_rb.c', line 3122

static VALUE mpfrrb_li2(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(li2);
}

#lngamma(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the logarithm of the Gamma function on v, rounded in the directionround. When v is 1 or 2, set self to +0 (in all rounding modes). When v is an infinity or a non-positive integer, set self to +Inf, following the general rules on special values. When −2k − 1 < v < −2k, k being a non-negative integer, set self to NaN.

Returns:



3160
3161
3162
3163
# File 'ext/mpfr_rb.c', line 3160

static VALUE mpfrrb_lngamma(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(lngamma);
}

#log(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the natural logarithm of v, rounded in the direction round. Set self to +0 if v is 1 (in all rounding modes), for consistency with the ISO C99 and IEEE 754 standards. Set self to -Inf if v is ±0 (i.e., the sign of the zero has no influence on the result).

Returns:



2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
# File 'ext/mpfr_rb.c', line 2050

static VALUE mpfrrb_log(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;

  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_log(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    long int i = NUM2LL(a);
    if (i < 0) {
      mpfr_set_nan(mpr);
      r = 0;
    } else {
      r = mpfr_log_ui(mpr, i, rnd);
    }
  } else if (RB_TYPE_P(a, T_BIGNUM)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(a, mpz);
    int nlz_bits_ret;
    size_t nb_bytes = rb_absint_size(a, &nlz_bits_ret);
    mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret;
    mpfr_t mpa;
    mpfr_init2(mpa, p);
    mpfr_set_z(mpa, mpz, MPFR_RNDN);
    mpz_clear(mpz);
    r = mpfr_log(mpr, mpa, rnd);
    mpfr_clear(mpa);
  } else {
    mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
    r = mpfr_log(mpr, mpa, rnd);
  }
  return INT2NUM(r);
}

#log10(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the base ten logarithm of v, rounded in the direction round. Set self to +0 if v is 1 (in all rounding modes), for consistency with the ISO C99 and IEEE 754 standards. Set self to -Inf if v is ±0 (i.e., the sign of the zero has no influence on the result).

Returns:



2453
2454
2455
2456
# File 'ext/mpfr_rb.c', line 2453

static VALUE mpfrrb_log10(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(log10);
}

#log10p1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the logarithm of one plus v in radix ten, rounded in the direction round. Set self to -Inf if v is -1.

Returns:



2486
2487
2488
2489
# File 'ext/mpfr_rb.c', line 2486

static VALUE mpfrrb_log10p1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(log10p1);
}

#logp1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the logarithm of one plus v, rounded in the direction round. Set self to -Inf if v is -1.

Returns:



2464
2465
2466
2467
# File 'ext/mpfr_rb.c', line 2464

static VALUE mpfrrb_log1p(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(log1p);
}

#log2(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the base 2 logarithm of v, rounded in the direction round. Set self to +0 if v is 1 (in all rounding modes), for consistency with the ISO C99 and IEEE 754 standards. Set self to -Inf if v is ±0 (i.e., the sign of the zero has no influence on the result).

Returns:



2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
# File 'ext/mpfr_rb.c', line 2096

static VALUE mpfrrb_log2(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;

  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_log2(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_log2(mpr, mpa, rnd);
  } else if (RB_TYPE_P(a, T_BIGNUM)) {
    mpz_t mpz; mpz_init(mpz); mpfrrb_bignum_to_mpz(a, mpz);
    int nlz_bits_ret; size_t nb_bytes = rb_absint_size(a, &nlz_bits_ret);
    mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpa; mpfr_init2(mpa, p);
    mpfr_set_z(mpa, mpz, MPFR_RNDN); mpz_clear(mpz);
    r = mpfr_log2(mpr, mpa, rnd); mpfr_clear(mpa);
  } else {
    mpfr_ptr mpa = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(a));
    r = mpfr_log2(mpr, mpa, rnd);
  }
  return INT2NUM(r);
}

#log2p1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the logarithm of one plus v in radix two, rounded in the direction round. Set self to -Inf if v is -1.

Returns:



2475
2476
2477
2478
# File 'ext/mpfr_rb.c', line 2475

static VALUE mpfrrb_log2p1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(log2p1);
}

#max(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self to the maximum of x and y. If x and y are both NaN, then self is set to NaN. If x or y is NaN, then self is set to the numeric value. If x and y are zeros of different signs, then rop is set to +0.

Returns:



3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
# File 'ext/mpfr_rb.c', line 3910

static VALUE mpfrrb_max(int argc, VALUE *argv, VALUE self)
{
  VALUE x, y;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &x, &y);
  x = mpfrrb_object_to_mpfr(x);
  y = mpfrrb_object_to_mpfr(y);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_max(mp, mpfrrb_rb2ref(x), mpfrrb_rb2ref(y), rnd);
  return INT2NUM(r);
}

#min(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self to the minimum of x and y. If x and y are both NaN, then self is set to NaN. If x or y is NaN, then self is set to the numeric value. If x and y are zeros of different signs, then rop is set to -0.

Returns:



3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
# File 'ext/mpfr_rb.c', line 3891

static VALUE mpfrrb_min(int argc, VALUE *argv, VALUE self)
{
  VALUE x, y;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &x, &y);
  x = mpfrrb_object_to_mpfr(x);
  y = mpfrrb_object_to_mpfr(y);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_min(mp, mpfrrb_rb2ref(x), mpfrrb_rb2ref(y), rnd);
  return INT2NUM(r);
}

#mul(a, b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to a times b rounded in the direction round.

Parameters:

Returns:



1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'ext/mpfr_rb.c', line 1256

static VALUE mpfrrb_mul(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  a = mpfrrb_object_to_mpfr(a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  mpfr_ptr mpa = mpfrrb_rb2ref(a);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_mul_d(mpr, mpa, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_mul_si(mpr, mpa, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_mul_z(mpr, mpa, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_mul_q(mpr, mpa, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_mul(mpr, mpa, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#mul!(b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to self times b rounded in the direction round.

Parameters:

Returns:



1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'ext/mpfr_rb.c', line 1293

static VALUE mpfrrb_mul_B(int argc, VALUE *argv, VALUE self)
{
  VALUE b;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_mul_d(mpr, mpr, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_mul_si(mpr, mpr, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_mul_z(mpr, mpr, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_mul_q(mpr, mpr, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_mul(mpr, mpr, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#mul_2i(a, n, round: MPFR.default_rounding) ⇒ Integer

Set self to a times 2 raised to n, rounded int the direction round.

Parameters:

Returns:



1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
# File 'ext/mpfr_rb.c', line 1734

static VALUE mpfrrb_mul_2i(int argc, VALUE *argv, VALUE self)
{
  VALUE a, n;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_mul_2si(mpr, mpa, i, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_mul_2si(mpr, mpa, i, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_mul_2si(mpr, mpfrrb_rb2ref(a), i, rnd);
  }
  return INT2NUM(r);
}

#mul_2i!(n, round: MPFR.default_rounding) ⇒ Integer

Set self to self times 2 raised to n, rounded int the direction round.

Parameters:

  • n (Integer)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1763
1764
1765
1766
1767
1768
1769
1770
1771
# File 'ext/mpfr_rb.c', line 1763

static VALUE mpfrrb_mul_2i_B(int argc, VALUE *argv, VALUE self)
{
  VALUE n;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_mul_2si(mp, mp, i, rnd);
  return INT2NUM(r);
}

#nan?Boolean

Tels if self does not represent a number.

Returns:

  • (Boolean)


767
768
769
770
# File 'ext/mpfr_rb.c', line 767

static VALUE mpfrrb_is_nan(VALUE self)
{
  return mpfr_nan_p(mpfrrb_rb2ref(self)) ? Qtrue : Qfalse;
}

#neg(a, round: MPFR.default_rounding) ⇒ Integer

Set self to -a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'ext/mpfr_rb.c', line 1636

static VALUE mpfrrb_neg(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_neg(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_neg(mpr, mpa, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_neg(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#neg!Integer

Set self -self.

Returns:



1661
1662
1663
1664
1665
1666
# File 'ext/mpfr_rb.c', line 1661

static VALUE mpfrrb_neg_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_neg(mp, mp, MPFR_RNDN);
  return INT2NUM(r);
}

#nextabove!self

Set self to the next (toward +Infinity) representable value.

Returns:

  • (self)


3708
3709
3710
3711
3712
# File 'ext/mpfr_rb.c', line 3708

static VALUE mpfrrb_nextabove_B(VALUE self)
{
  mpfr_nextabove(mpfrrb_rb2ref(self));
  return self;
}

#nextbelow!self

Set self to the previous (toward -Infinity) representable value.

Returns:

  • (self)


3718
3719
3720
3721
3722
# File 'ext/mpfr_rb.c', line 3718

static VALUE mpfrrb_nextbelow_B(VALUE self)
{
  mpfr_nextbelow(mpfrrb_rb2ref(self));
  return self;
}

#nrandom(round: MPFR.default_rounding) ⇒ Integer

Generate a random floating-point number according to a standard normal Gaussian distribution (with mean zero and variance one).

The floating-point number self can be seen as if a random real number were generated according to the standard normal Gaussian distribution and then rounded in the direction rnd.

Returns:



3965
3966
3967
3968
3969
3970
3971
# File 'ext/mpfr_rb.c', line 3965

static VALUE mpfrrb_nrandom(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_nrandom(mp, mpfrrb_gmp_randstate, rnd);
  return INT2NUM(r);
}

#pow(x, y, round: MPFR.default_rounding) ⇒ Integer

Note:

When 0 is of integer type, it is regarded as +0 by these functions. We do not use the usual limit rules in this case, as these rules are not used for pow.

Set self to x raised to y, rounded in the direction round.

Special values are handled as described in the ISO C99 and IEEE 754 standards for the pow function:

  • pow(±0, y) returns ±Inf for y a negative odd integer.
  • pow(±0, y) returns +Inf for y negative and not an odd integer.
  • pow(±0, y) returns ±0 for y a positive odd integer.
  • pow(±0, y) returns +0 for y positive and not an odd integer.
  • pow(-1, ±Inf) returns 1.
  • pow(+1, y) returns 1 for any y, even a NaN.
  • pow(x, ±0) returns 1 for any x, even a NaN.
  • pow(x, y) returns NaN for finite negative x and finite non-integer y.
  • pow(x, -Inf) returns +Inf for 0 < abs(x) < 1, and +0 for abs(x) > 1.
  • pow(x, +Inf) returns +0 for 0 < abs(x) < 1, and +Inf for abs(x) > 1.
  • pow(-Inf, y) returns -0 for y a negative odd integer.
  • pow(-Inf, y) returns +0 for y negative and not an odd integer.
  • pow(-Inf, y) returns -Inf for y a positive odd integer.
  • pow(-Inf, y) returns +Inf for y positive and not an odd integer.
  • pow(+Inf, y) returns +0 for y negative, and +Inf for y positive.

Parameters:

Returns:



2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
# File 'ext/mpfr_rb.c', line 2576

static VALUE mpfrrb_pow(int argc, VALUE *argv, VALUE self)
{
  VALUE x, y;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &x, &y);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(x)) {
    MPFR_DECL_INIT(mpx, 53);
    mpfr_set_d(mpx, NUM2DBL(x), MPFR_RNDN);
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long n = NUM2LL(y);
      r = mpfr_pow_si(mpr, mpx, n, rnd);
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy;
      mpz_init(mpzy);
      mpfrrb_bignum_to_mpz(y, mpzy);
      r = mpfr_pow_z(mpr, mpx, mpzy, rnd);
      mpz_clear(mpzy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    }
  } else if (RB_FIXNUM_P(x)) {
    long long xn = NUM2LL(x);
    if (xn >= 0) {
      if (RB_FLOAT_TYPE_P(y)) {
        MPFR_DECL_INIT(mpy, 53);
        mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
        r = mpfr_ui_pow(mpr, xn, mpy, rnd);
      } else if (RB_FIXNUM_P(y)) {
        long long yn = NUM2LL(y);
        if (yn >= 0) {
          r = mpfr_ui_pow_ui(mpr, xn, yn, rnd);
        } else {
          MPFR_DECL_INIT(mpx, 64);
          mpfr_set_si(mpx, NUM2LL(x), MPFR_RNDN);
          r = mpfr_pow_si(mpr, mpx, yn, rnd);
        }
      } else if (RB_TYPE_P(y, T_BIGNUM)) {
        MPFR_DECL_INIT(mpx, 64);
        mpfr_set_si(mpx, NUM2LL(x), MPFR_RNDN);
        mpz_t mpzy;
        mpz_init(mpzy);
        mpfrrb_bignum_to_mpz(y, mpzy);
        r = mpfr_pow_z(mpr, mpx, mpzy, rnd);
        mpz_clear(mpzy);
      } else {
        mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
        r = mpfr_ui_pow(mpr, xn, mpy, rnd);
      }
    } else {
      MPFR_DECL_INIT(mpx, 64);
      mpfr_set_si(mpx, NUM2LL(x), MPFR_RNDN);
      if (RB_FLOAT_TYPE_P(y)) {
        MPFR_DECL_INIT(mpy, 53);
        mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
        r = mpfr_pow(mpr, mpx, mpy, rnd);
      } else if (RB_FIXNUM_P(y)) {
        long long n = NUM2LL(y);
        r = mpfr_pow_si(mpr, mpx, n, rnd);
      } else if (RB_TYPE_P(y, T_BIGNUM)) {
        mpz_t mpzy;
        mpz_init(mpzy);
        mpfrrb_bignum_to_mpz(y, mpzy);
        r = mpfr_pow_z(mpr, mpx, mpzy, rnd);
        mpz_clear(mpzy);
      } else {
        mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
        r = mpfr_pow(mpr, mpx, mpy, rnd);
      }
    }
  } else if (RB_TYPE_P(x, T_BIGNUM)) {
    mpz_t mpz; mpz_init(mpz); mpfrrb_bignum_to_mpz(x, mpz);
    int nlz_bits_ret; size_t nb_bytes = rb_absint_size(x, &nlz_bits_ret);
    mpfr_prec_t p = nb_bytes*8 - nlz_bits_ret; mpfr_t mpx; mpfr_init2(mpx, p);
    mpfr_set_z(mpx, mpz, MPFR_RNDN); mpz_clear(mpz);
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long n = NUM2LL(y);
      r = mpfr_pow_si(mpr, mpx, n, rnd);
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy;
      mpz_init(mpzy);
      mpfrrb_bignum_to_mpz(y, mpzy);
      r = mpfr_pow_z(mpr, mpx, mpzy, rnd);
      mpz_clear(mpzy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    }
    mpfr_clear(mpx);
  } else {
    mpfr_ptr mpx = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(x));
    if (RB_FLOAT_TYPE_P(y)) {
      MPFR_DECL_INIT(mpy, 53);
      mpfr_set_d(mpy, NUM2DBL(y), MPFR_RNDN);
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    } else if (RB_FIXNUM_P(y)) {
      long long n = NUM2LL(y);
      r = mpfr_pow_si(mpr, mpx, n, rnd);
    } else if (RB_TYPE_P(y, T_BIGNUM)) {
      mpz_t mpzy;
      mpz_init(mpzy);
      mpfrrb_bignum_to_mpz(y, mpzy);
      r = mpfr_pow_z(mpr, mpx, mpzy, rnd);
      mpz_clear(mpzy);
    } else {
      mpfr_ptr mpy = mpfrrb_rb2ref(mpfrrb_object_to_mpfr(y));
      r = mpfr_pow(mpr, mpx, mpy, rnd);
    }
  }
  return INT2NUM(r);
}

#precInteger

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

Returns:



536
537
538
539
# File 'ext/mpfr_rb.c', line 536

static VALUE mpfrrb_get_prec(VALUE self)
{
  return LL2NUM(mpfr_get_prec(mpfrrb_rb2ref(self)));
}

#prec=(precrb) ⇒ Object

Set the precision of self to be exactly prec bits, and set its value to NaN. The previous value stored in self is lost. In case you want to keep the previous value stored in self, use #prec_round instead.

See Also:



547
548
549
550
551
552
553
554
555
# File 'ext/mpfr_rb.c', line 547

static VALUE mpfrrb_set_prec(VALUE self, VALUE precrb)
{
  mpfr_prec_t prec = NUM2LL(precrb);
  if (prec < MPFR_PREC_MIN || prec > MPFR_PREC_MAX) {
    rb_raise(rb_eRangeError, "precision must be between %d and %ld, %ld is invalid", MPFR_PREC_MIN, MPFR_PREC_MAX, prec);
  }
  mpfr_set_prec(mpfrrb_rb2ref(self), prec);
  return precrb;
}

#prec_round(prec, round: MPFR.default_rounding) ⇒ Integer

Round self according to round with precision prec. If prec is greater than or equal to the precision of self, then new space is allocated for the significand, and it is filled with zeros. Otherwise, the significand is rounded to precision prec with the given direction; no memory reallocation to free the unused limbs is done. In both cases, the precision of self is changed to prec.

Parameters:

  • prec (Integer)

    The new precision. It must be an integer between MPFR::PREC_MIN and MPFR::PREC_MAX (otherwise the behavior is undefined).

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'ext/mpfr_rb.c', line 929

static VALUE mpfrrb_prec_round(int argc, VALUE *argv, VALUE self)
{
  VALUE precrb;
  VALUE kw;
  const ID kwkeys[1] = {id_round};
  VALUE kwvalues[1] = {Qundef};
  rb_scan_args(argc, argv, "1:", &precrb, &kw);
  mpfr_rnd_t rnd = mpfrrb_default_rounding;
  mpfr_prec_t prec = NUM2LL(precrb);
  if (prec < MPFR_PREC_MIN || prec > MPFR_PREC_MAX) {
    rb_raise(rb_eRangeError, "precision must be between %d and %ld, %ld is invalid", MPFR_PREC_MIN, MPFR_PREC_MAX, prec);
  }
  if (!NIL_P(kw)) {
    rb_get_kwargs(kw, kwkeys, 0, 1, kwvalues);
    if (kwvalues[0] != Qundef) {
      rnd = mpfrrb_sym2rnd(kwvalues[0]);
    }
  }
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_prec_round(mp, prec, rnd);
  return INT2NUM(r);
}

#rec_sqrt(a, round: MPFR.default_rounding) ⇒ Integer

Set self to the reciprocal square root of a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'ext/mpfr_rb.c', line 1505

static VALUE mpfrrb_rec_sqrt(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_rec_sqrt(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_rec_sqrt(mpr, mpa, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_rec_sqrt(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#rec_sqrt!(round: MPFR.default_rounding) ⇒ Integer

Set self to the reciprocal square root of self, rounded int the direction round.

Parameters:

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1532
1533
1534
1535
1536
1537
1538
# File 'ext/mpfr_rb.c', line 1532

static VALUE mpfrrb_rec_sqrt_B(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_rec_sqrt(mp, mp, rnd);
  return INT2NUM(r);
}

#remainder(x, y, round: MPFR.default_rounding) ⇒ Integer

Set self the the value of x - ny, rounded according to the direction round.

n is the integer quotient of x divided by y, rounded to the nearest integer (ties rounded to even).

Special values are handled as described in Section F.9.7.1 of the ISO C99 standard:

  • If x is infinite or y is zero, self is NaN.
  • If y is infinite and x is finite, self is x rounded to the precision of self.
  • If self is zero, it has the sign of x. The return value is the ternary value corresponding to self.

Returns:

See Also:



3699
3700
3701
3702
# File 'ext/mpfr_rb.c', line 3699

static VALUE mpfrrb_remainder(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_TWO_ARG_FUNC_BODY(remainder);
}

#rint(op, round: MPFR.default_rounding) ⇒ Integer

Set self to op rounded to the nearest representable integer in the direction round.

Returns:



3338
3339
3340
3341
# File 'ext/mpfr_rb.c', line 3338

static VALUE mpfrrb_rint(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint);
}

#rint!(round: MPFR.default_rounding) ⇒ Integer

Set self to self rounded to the nearest representable integer in the direction round.

Returns:



3393
3394
3395
3396
3397
3398
# File 'ext/mpfr_rb.c', line 3393

static VALUE mpfrrb_rint_B(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_rint(mp, mp, rnd));
}

#rint_ceil(op, round: MPFR.default_rounding) ⇒ Ingeger

Set self to op rounded to the next higher or equal integer. If the result is not representable, it is rounded in the direction round. This method do perform a double rounding: first op is rounded to the nearest integer in the direction given by the method name, then this nearest integer (if not representable) is rounded in the given direction round.

Returns:

  • (Ingeger)


3459
3460
3461
3462
# File 'ext/mpfr_rb.c', line 3459

static VALUE mpfrrb_rint_ceil(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint_ceil);
}

#rint_floor(op, round: MPFR.default_rounding) ⇒ Ingeger

Set self to op rounded to the next lower or equal integer. If the result is not representable, it is rounded in the direction round. This method do perform a double rounding: first op is rounded to the nearest integer in the direction given by the method name, then this nearest integer (if not representable) is rounded in the given direction round.

Returns:

  • (Ingeger)


3473
3474
3475
3476
# File 'ext/mpfr_rb.c', line 3473

static VALUE mpfrrb_rint_floor(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint_floor);
}

#rint_round(op, round: MPFR.default_rounding) ⇒ Ingeger

Set self to op rounded to the nearest integer, rounding halfway cases away from zero. If the result is not representable, it is rounded in the direction round. This method do perform a double rounding: first op is rounded to the nearest integer in the direction given by the method name, then this nearest integer (if not representable) is rounded in the given direction round.

Returns:

  • (Ingeger)


3487
3488
3489
3490
# File 'ext/mpfr_rb.c', line 3487

static VALUE mpfrrb_rint_round(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint_round);
}

#rint_roundeven(op, round: MPFR.default_rounding) ⇒ Ingeger

Set self to op rounded to the nearest integer, rounding halfway cases to the nearest even integer. If the result is not representable, it is rounded in the direction round. This method do perform a double rounding: first op is rounded to the nearest integer in the direction given by the method name, then this nearest integer (if not representable) is rounded in the given direction round.

Returns:

  • (Ingeger)


3501
3502
3503
3504
# File 'ext/mpfr_rb.c', line 3501

static VALUE mpfrrb_rint_roundeven(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint_roundeven);
}

#rint_trunc(op, round: MPFR.default_rounding) ⇒ Ingeger

Set self to op rounded to the next integer toward zero. If the result is not representable, it is rounded in the direction round. This method do perform a double rounding: first op is rounded to the nearest integer in the direction given by the method name, then this nearest integer (if not representable) is rounded in the given direction round.

Returns:

  • (Ingeger)


3515
3516
3517
3518
# File 'ext/mpfr_rb.c', line 3515

static VALUE mpfrrb_rint_trunc(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(rint_trunc);
}

#rootn(a, n, round: MPFR.default_rounding) ⇒ Integer

Set self to the nth root of a, rounded int the direction round.

Parameters:

Returns:



1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
# File 'ext/mpfr_rb.c', line 1590

static VALUE mpfrrb_rootn(int argc, VALUE *argv, VALUE self)
{
  VALUE a, n;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_rootn_si(mpr, mpa, i, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_rootn_si(mpr, mpa, i, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_rootn_si(mpr, mpfrrb_rb2ref(a), i, rnd);
  }
  return INT2NUM(r);
}

#rootn!(n, round: MPFR.default_rounding) ⇒ Integer

Set self to the nth root of self, rounded int the direction round.

Parameters:

  • n (Integer)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1619
1620
1621
1622
1623
1624
1625
1626
1627
# File 'ext/mpfr_rb.c', line 1619

static VALUE mpfrrb_rootn_B(int argc, VALUE *argv, VALUE self)
{
  VALUE n;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &n);
  long long i = NUM2LL(n);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_rootn_si(mp, mp, i, rnd);
  return INT2NUM(r);
}

#round(op) ⇒ Integer

Set self to op rounded to the nearest representable integer, rounding halfway cases away from zero.

Returns:



3365
3366
3367
3368
# File 'ext/mpfr_rb.c', line 3365

static VALUE mpfrrb_round(VALUE self, VALUE op)
{
  MPFRRB_ONE_ARG_NO_RND_FUNC_BODY(round);
}

#round!Integer

Set self to self rounded to the nearest representable integer, rounding halfway cases away from zero.

Returns:



3424
3425
3426
3427
3428
# File 'ext/mpfr_rb.c', line 3424

static VALUE mpfrrb_round_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_round(mp, mp));
}

#roundeven(op) ⇒ Integer

Set self to op rounded to the nearest representable integer, rounding halfway cases with the even-rounding rule.

Returns:



3374
3375
3376
3377
# File 'ext/mpfr_rb.c', line 3374

static VALUE mpfrrb_roundeven(VALUE self, VALUE op)
{
  MPFRRB_ONE_ARG_NO_RND_FUNC_BODY(roundeven);
}

#roundeven!Integer

Set self to self rounded to the nearest representable integer, rounding halfway cases with the even-rounding rule.

Returns:



3434
3435
3436
3437
3438
# File 'ext/mpfr_rb.c', line 3434

static VALUE mpfrrb_roundeven_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_roundeven(mp, mp));
}

#cot(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the secant of v, rounded in the direction round.

Returns:



2827
2828
2829
2830
# File 'ext/mpfr_rb.c', line 2827

static VALUE mpfrrb_sec(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(sec);
}

#sech(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic secant of v, rounded in the direction round.

Returns:



3048
3049
3050
3051
# File 'ext/mpfr_rb.c', line 3048

static VALUE mpfrrb_sech(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(sech);
}

#set(value, round: MPFR.default_rounding) ⇒ Integer

Set the value of the MPFR object.

Parameters:

  • value (Float, Integer, Rational, String, MPFR)
  • rnd (Symbol)

    , either :nearest, :up, :down, :zero, :rndn, :rndu, :rndd, :rndz, :rnda, :rndf, :rndna, :round_up, :round_down, :toward_zero, :away_from_zero or :faithful_rounding

Returns:



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'ext/mpfr_rb.c', line 450

static VALUE mpfrrb_set(int argc, VALUE *argv, VALUE self)
{
  VALUE valrb;
  VALUE kw;
  const ID kwkeys[1] = {id_round};
  VALUE kwvalues[1] = {Qundef};
  rb_scan_args(argc, argv, "1:", &valrb, &kw);
  mpfr_rnd_t rnd = mpfrrb_default_rounding;
  if (!NIL_P(kw)) {
    rb_get_kwargs(kw, kwkeys, 0, 1, kwvalues);
    if (kwvalues[0] != Qundef) {
      rnd = mpfrrb_sym2rnd(kwvalues[0]);
    }
  }
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfrrb_set_internal(mp, rnd, valrb, self);
  return INT2NUM(r);
}

#set_catalan(round: MPFR.default_rounding) ⇒ Integer

Set self to the value of of Catalan's constant 0.915..., rounded in the given direction.

Returns:



990
991
992
993
994
# File 'ext/mpfr_rb.c', line 990

static VALUE mpfrrb_set_catalan(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  return INT2NUM(mpfr_const_catalan(mpfrrb_rb2ref(self), rnd));
}

#set_euler(round: MPFR.default_rounding) ⇒ Integer

Set self to the value of of Euler's constant 0.577..., rounded in the given direction.

Returns:



979
980
981
982
983
# File 'ext/mpfr_rb.c', line 979

static VALUE mpfrrb_set_euler(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  return INT2NUM(mpfr_const_euler(mpfrrb_rb2ref(self), rnd));
}

#set_infinity(sign = 1) ⇒ self

Set self to infinity.

Parameters:

  • sign (Integer) (defaults to: 1)

    the sign bit is set if sign < 0

Returns:

  • (self)


904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'ext/mpfr_rb.c', line 904

static VALUE mpfrrb_set_infinity(int argc, VALUE *argv, VALUE self)
{
  VALUE signrb;
  int sign = 1;
  if (argc > 0) {
    rb_scan_args(argc, argv, "01", &signrb);
    if (!NIL_P(signrb)) {
      sign = NUM2INT(signrb);
    }
  }
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  mpfr_set_inf(mp, sign);
  return self;
}

#set_log2(round: MPFR.default_rounding) ⇒ Integer

Set self to the logarithm of 2, rounded in the given direction.

Returns:



957
958
959
960
961
# File 'ext/mpfr_rb.c', line 957

static VALUE mpfrrb_set_log2(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  return INT2NUM(mpfr_const_log2(mpfrrb_rb2ref(self), rnd));
}

#set_nanself

Set self to NaN.

Returns:

  • (self)


870
871
872
873
874
875
# File 'ext/mpfr_rb.c', line 870

static VALUE mpfrrb_set_nan(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  mpfr_set_nan(mp);
  return self;
}

#set_pi(round: MPFR.default_rounding) ⇒ Integer

Set self to the value of Pi, rounded in the given direction.

Returns:



968
969
970
971
972
# File 'ext/mpfr_rb.c', line 968

static VALUE mpfrrb_set_pi(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  return INT2NUM(mpfr_const_pi(mpfrrb_rb2ref(self), rnd));
}

#set_zero(sign = 1) ⇒ self

Set self to zero.

Parameters:

  • sign (Integer) (defaults to: 1)

    the sign bit is set if sign < 0

Returns:

  • (self)


883
884
885
886
887
888
889
890
891
892
893
894
895
896
# File 'ext/mpfr_rb.c', line 883

static VALUE mpfrrb_set_zero(int argc, VALUE *argv, VALUE self)
{
  VALUE signrb;
  int sign = 1;
  if (argc > 0) {
    rb_scan_args(argc, argv, "01", &signrb);
    if (!NIL_P(signrb)) {
      sign = NUM2INT(signrb);
    }
  }
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  mpfr_set_zero(mp, sign);
  return self;
}

#signIngeger?

Returns either -1 or 1.

Returns:

  • (Ingeger, nil)

    either -1 or 1



701
702
703
704
705
706
707
708
# File 'ext/mpfr_rb.c', line 701

static VALUE mpfrrb_get_sign(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp)) {
    return Qnil;
  }
  return INT2NUM(mpfr_sgn(mp) >= 0 ? 1 : -1);
}

#sign=(s) ⇒ Object

Set the sign of self to the sign of the argument.

Parameters:



714
715
716
717
718
719
720
# File 'ext/mpfr_rb.c', line 714

static VALUE mpfrrb_set_sign(VALUE self, VALUE s)
{
  int p = NUM2INT(s);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  mpfr_setsign(mp, mp, p < 0, MPFR_RNDN);
  return s;
}

#sin(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the sine of v, rounded in the direction round.

Returns:



2728
2729
2730
2731
# File 'ext/mpfr_rb.c', line 2728

static VALUE mpfrrb_sin(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(sin);
}

#sinh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic sine of v, rounded in the direction round.

Returns:



3028
3029
3030
3031
# File 'ext/mpfr_rb.c', line 3028

static VALUE mpfrrb_sinh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(sinh);
}

#sinpi(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the sine of v multiplied by Pi, rounded in the direction round. See the description of #sinu for special values.

Returns:



2806
2807
2808
2809
# File 'ext/mpfr_rb.c', line 2806

static VALUE mpfrrb_sinpi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(sinpi);
}

#cosu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the sine of x, multiplied by 2 Pi and divided by u, rounded in the direction round.

For example, if u equals 360, one gets the osine for x in degrees.

When x multiplied by 2 and divided by u is an integer, the result is zero with the same sign as x, following IEEE 754 (sinPi), so that the function is odd.

Parameters:

Returns:



2769
2770
2771
2772
# File 'ext/mpfr_rb.c', line 2769

static VALUE mpfrrb_sinu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(sinu);
}

#sqr(a, round: MPFR.default_rounding) ⇒ Integer

Set self to the square of a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
# File 'ext/mpfr_rb.c', line 1417

static VALUE mpfrrb_sqr(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_sqr(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    MPFR_DECL_INIT(mpa, 64);
    mpfr_set_si(mpa, NUM2LL(a), MPFR_RNDN);
    r = mpfr_sqr(mpr, mpa, rnd);
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_sqr(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#sqr!(round: MPFR.default_rounding) ⇒ Integer

Set self to the square of self, rounded int the direction round.

Parameters:

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1444
1445
1446
1447
1448
1449
1450
# File 'ext/mpfr_rb.c', line 1444

static VALUE mpfrrb_sqr_B(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_sqr(mp, mp, rnd);
  return INT2NUM(r);
}

#sqrt(a, round: MPFR.default_rounding) ⇒ Integer

Set self to the square root of a, rounded int the direction round.

Parameters:

  • a (MPFR)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
# File 'ext/mpfr_rb.c', line 1459

static VALUE mpfrrb_sqrt(int argc, VALUE *argv, VALUE self)
{
  VALUE a;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(a)) {
    MPFR_DECL_INIT(mpa, 53);
    mpfr_set_d(mpa, NUM2DBL(a), MPFR_RNDN);
    r = mpfr_sqrt(mpr, mpa, rnd);
  } else if (RB_FIXNUM_P(a)) {
    long int i = NUM2LL(a);
    if (i < 0) {
      mpfr_set_nan(mpr);
      r = 0;
    } else {
      r = mpfr_sqrt_ui(mpr, i, rnd);
    }
  } else {
    a = mpfrrb_object_to_mpfr(a);
    r = mpfr_sqrt(mpr, mpfrrb_rb2ref(a), rnd);
  }
  return INT2NUM(r);
}

#sqrt!(round: MPFR.default_rounding) ⇒ Integer

Set self to the square root of self, rounded int the direction round.

Parameters:

  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:



1490
1491
1492
1493
1494
1495
1496
# File 'ext/mpfr_rb.c', line 1490

static VALUE mpfrrb_sqrt_B(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_sqrt(mp, mp, rnd);
  return INT2NUM(r);
}

#sub(a, b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to a - b rounded in the direction round.

Parameters:

Returns:



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'ext/mpfr_rb.c', line 1162

static VALUE mpfrrb_sub(int argc, VALUE *argv, VALUE self)
{
  VALUE a, b;
  mpfr_rnd_t rnd = mpfrrb_get_a_b_round(argc, argv, &a, &b);
  a = mpfrrb_object_to_mpfr(a);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (rb_obj_is_kind_of(a, c_MPFR)) {
    mpfr_ptr mpa = mpfrrb_rb2ref(a);
    if (RB_FLOAT_TYPE_P(b)) {
      r = mpfr_sub_d(mpr, mpa, NUM2DBL(b), rnd);
    } else if (RB_FIXNUM_P(b)) {
      r = mpfr_sub_si(mpr, mpa, NUM2LL(b), rnd);
    } else if (RB_INTEGER_TYPE_P(b)) {
      mpz_t mpz;
      mpz_init(mpz);
      mpfrrb_bignum_to_mpz(b, mpz);
      r = mpfr_sub_z(mpr, mpa, mpz, rnd);
      mpz_clear(mpz);
    } else if (RB_TYPE_P(b, T_RATIONAL)) {
      mpq_t mpq;
      mpq_init(mpq);
      mpfrrb_rational_to_mpq(b, mpq);
      r = mpfr_sub_q(mpr, mpa, mpq, rnd);
      mpq_clear(mpq);
    } else {
      b = mpfrrb_object_to_mpfr(b);
      r = mpfr_sub(mpr, mpa, mpfrrb_rb2ref(b), rnd);
    }
  } else {
    b = mpfrrb_object_to_mpfr(b);
    mpfr_ptr mpb = mpfrrb_rb2ref(b);
    if (RB_FLOAT_TYPE_P(a)) {
      r = mpfr_d_sub(mpr, NUM2DBL(a), mpb, rnd);
    } else if (RB_FIXNUM_P(a)) {
      r = mpfr_si_sub(mpr, NUM2LL(a), mpb, rnd);
    } else if (RB_INTEGER_TYPE_P(a)) {
      mpz_t mpz;
      mpz_init(mpz);
      mpfrrb_bignum_to_mpz(a, mpz);
      r = mpfr_z_sub(mpr, mpz, mpb, rnd);
      mpz_clear(mpz);
    } else {
      a = mpfrrb_object_to_mpfr(a);
      r = mpfr_sub(mpr, mpfrrb_rb2ref(a), mpb, rnd);
    }

  }

  return INT2NUM(r);
}

#sub!(b, round: MPFR.default_rounding) ⇒ Integer

Set the value of self to self - b rounded in the direction round.

Parameters:

Returns:



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
# File 'ext/mpfr_rb.c', line 1220

static VALUE mpfrrb_sub_B(int argc, VALUE *argv, VALUE self)
{
  VALUE b;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &b);
  mpfr_ptr mpr = mpfrrb_rb2ref(self);
  int r;
  if (RB_FLOAT_TYPE_P(b)) {
    r = mpfr_sub_d(mpr, mpr, NUM2DBL(b), rnd);
  } else if (RB_FIXNUM_P(b)) {
    r = mpfr_sub_si(mpr, mpr, NUM2LL(b), rnd);
  } else if (RB_INTEGER_TYPE_P(b)) {
    mpz_t mpz;
    mpz_init(mpz);
    mpfrrb_bignum_to_mpz(b, mpz);
    r = mpfr_sub_z(mpr, mpr, mpz, rnd);
    mpz_clear(mpz);
  } else if (RB_TYPE_P(b, T_RATIONAL)) {
    mpq_t mpq;
    mpq_init(mpq);
    mpfrrb_rational_to_mpq(b, mpq);
    r = mpfr_sub_q(mpr, mpr, mpq, rnd);
    mpq_clear(mpq);
  } else {
    b = mpfrrb_object_to_mpfr(b);
    r = mpfr_sub(mpr, mpr, mpfrrb_rb2ref(b), rnd);
  }
  return INT2NUM(r);
}

#subnormalize(t, round: MPFR.default_rounding) ⇒ Integer

This function rounds self emulating subnormal number arithmetic. If self is outside the subnormal exponent range of the emulated floating-point system, this function just propagates the ternary value t; otherwise, if self.exponent denotes the exponent of self, it rounds self to precision self.exponent - emin + 1 according to rounding mode round and previous ternary value t, avoiding double rounding problems. More precisely in the subnormal domain, denoting by e the value of emin, self is rounded in fixed-point arithmetic to an integer multiple of two to the power e − 1; as a consequence, 1.5 multiplied by two to the power e − 1 when t is zero is rounded to two to the power e with rounding to nearest.

The precision self.prec of self is not modified by this function. round and t must be the rounding mode and the returned ternary value used when computing self (as in mpfr_check_range). The subnormal exponent range is from emin to emin + self.prec - 1. If the result cannot be represented in the current exponent range of MPFR (due to a too small emax), the behavior is undefined. Note that unlike most functions, the result is compared to the exact one, not the input value self, i.e., the ternary value is propagated.

As usual, if the returned ternary value is non zero, the inexact flag is set. Moreover, if a second rounding occurred (because the input self was in the subnormal range), the underflow flag is set.

Warning! If you change emin (with emin=) just before calling mpfr_subnormalize, you need to make sure that the value is in the current exponent range of MPFR. But it is better to change emin before any computation, if possible.

This is an example of how to emulate binary64 IEEE 754 arithmetic (a.k.a. double precision) using MPFR:

MPFR.default_prec = 53
MPFR.emin = -1073
MPFR.emax =  1024
xa = MPFR.new
xb = MPFR.new

b = 34.3
xb.set(b, round: :nearest)
a = 0x11235.to_f * 2**-1037
xa.set(a, round: :nearest)

a /= b
i = xa.div(xa, xb, round: :nearest)
i = xa.subnormalize(i, round: :nearest) # new ternary value

Note that emin= and emax= are called early enough in order to make sure that all computed values are in the current exponent range. Warning! This emulates a double IEEE 754 arithmetic with correct rounding in the subnormal range, which may not be the case for your hardware.

Below is another example showing how to emulate fixed-point arithmetic in a specific case. Here we compute the sine of the integers 1 to 17 with a result in a fixed-point arithmetic rounded at two to the power -42 (using the fact that the result is at most 1 in absolute value):

MPFR.emin = -41
x = MPFR.new(prec: 42)
(1..17).each do |i|
  x.set(i, round: :nearest)
  inex = x.sin(x, round: :toward_zero)
  x.subnormalize(inex, round: :toward_zero)
  puts x
end

Parameters:

  • t (Integer)
  • round (Symbol) (defaults to: MPFR.default_rounding)

Returns:

See Also:



3806
3807
3808
3809
3810
3811
3812
3813
3814
# File 'ext/mpfr_rb.c', line 3806

static VALUE mpfrrb_subnormalize(int argc, VALUE *argv, VALUE self)
{
  VALUE trb;
  mpfr_rnd_t rnd = mpfrrb_get_b_round(argc, argv, &trb);
  int t = NUM2INT(trb);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_subnormalize(mp, t, rnd);
  return INT2NUM(r);
}

#tan(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the tangent of v, rounded in the direction round.

Returns:



2738
2739
2740
2741
# File 'ext/mpfr_rb.c', line 2738

static VALUE mpfrrb_tan(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(tan);
}

#tanh(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the hyperbolic tangent of v, rounded in the direction round.

Returns:



3038
3039
3040
3041
# File 'ext/mpfr_rb.c', line 3038

static VALUE mpfrrb_tanh(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(tanh);
}

#tanpi(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the tangent of v multiplied by Pi, rounded in the direction round. See the description of #tanu for special values.

Returns:



2817
2818
2819
2820
# File 'ext/mpfr_rb.c', line 2817

static VALUE mpfrrb_tanpi(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(tanpi);
}

#cosu(x, u, round: MPFR.default_rounding) ⇒ Integer

Set self to the tangent of x, multiplied by 2 Pi and divided by u, rounded in the direction round.

For example, if u equals 360, one gets the tangent for x in degrees.

The method #tanu follows IEEE 754 (tanPi).

Parameters:

Returns:



2784
2785
2786
2787
# File 'ext/mpfr_rb.c', line 2784

static VALUE mpfrrb_tanu(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_UL_FUNC_BODY(tanu);
}

#to_f(round: MPFR.default_rounding) ⇒ Float

Parameters:

Returns:



806
807
808
809
810
# File 'ext/mpfr_rb.c', line 806

static VALUE mpfrrb_to_f(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  return DBL2NUM(mpfr_get_d(mpfrrb_rb2ref(self), rnd));
}

#to_i(round: MPFR.default_rounding) ⇒ Integer

Parameters:

Returns:



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'ext/mpfr_rb.c', line 817

static VALUE mpfrrb_to_i(int argc, VALUE *argv, VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp)) {
    rb_raise(rb_eFloatDomainError, "NaN");
  } else if (mpfr_inf_p(mp)) {
    rb_raise(rb_eFloatDomainError, "Infinity");
  } else if (mpfr_zero_p(mp)) {
    return INT2NUM(0);
  }

  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);

  if (mpfr_fits_slong_p(mp, rnd)) {
    return LL2NUM(mpfr_get_si(mp, rnd));
  }

  mpz_t mpz;
  mpz_init(mpz);
  mpfr_get_z(mpz, mp, rnd);
  VALUE rbint = mpfrrb_mpz_to_bignum(mpz);
  mpz_clear(mpz);
  return rbint;
}

#to_mpfrself

Returns:

  • (self)


98
99
100
101
# File 'ext/mpfr_rb.c', line 98

static VALUE mpfrrb_MPFR_to_mpfr(VALUE self)
{
  return self;
}

#to_rRational

Convert the MPFR object to a rational. Conversion is always exact, no rounding has to be specified.

Returns:



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'ext/mpfr_rb.c', line 847

static VALUE mpfrrb_to_r(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp)) {
    rb_raise(rb_eFloatDomainError, "NaN");
  } else if (mpfr_inf_p(mp)) {
    rb_raise(rb_eFloatDomainError, "Infinity");
  } else if (mpfr_zero_p(mp)) {
    return rb_rational_new(INT2NUM(0), INT2NUM(1));
  }

  mpq_t mpq;
  mpq_init(mpq);
  mpfr_get_q(mpq, mp);
  VALUE rat = mpfrrb_mpq_to_rational(mpq);
  mpq_clear(mpq);
  return rat;
}

#to_s(conv: 'g', round: :nearest, decimals: nil) ⇒ String Also known as: inspect

Return a string representation of the number.

Parameters:

  • conf (String)

    The conversion specifier can be:

    • 'a', 'A': hex float, C99 style,
    • 'b': binary output,
    • 'e', 'E': scientific-format float,
    • 'f', 'F': fixed-point float,
    • 'g', 'G': fixed-point or scientific float.
  • rnd (Symbol)

    Rounding direction, either :rndn, :rndu, :rndd, :rndz, :rnda, :rndf, :rndna, :round_up, :round_down, :toward_zero, :away_from_zero or :faithful_rounding

  • decimals (Integer) (defaults to: nil)

    number of digits to display.

Returns:



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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'ext/mpfr_rb.c', line 632

static VALUE mpfrrb_to_s(int argc, VALUE *argv, VALUE self)
{
  VALUE kw;
  const ID kwkeys[3] = {id_conv, id_round, id_decimals};
  VALUE kwvalues[3] = {Qundef, Qundef, Qundef};

  mpfr_rnd_t rnd = MPFR_RNDN;
  char conv = 'g';
  int n_decimals = 0;

  rb_scan_args(argc, argv, ":", &kw);
  if (!NIL_P(kw)) {
    rb_get_kwargs(kw, kwkeys, 0, 3, kwvalues);
    if (kwvalues[0] != Qundef) {
      Check_Type(kwvalues[0], T_STRING);
      const char *convstr = RSTRING_PTR(kwvalues[0]);
      int len = RSTRING_LEN(kwvalues[0]);
      if (len != 1 || (convstr[0] != 'a' && convstr[0] != 'A' && convstr[0] != 'b' && convstr[0] != 'e' && convstr[0] != 'E' && convstr[0] != 'f' && convstr[0] != 'F' && convstr[0] != 'g' && convstr[0] != 'G')) {
        rb_raise(rb_eArgError, "conv must be 'a', 'A', 'b', 'e', 'E', 'f', 'F', 'g' or 'G', \"%" PRIsVALUE "\" is invalid", kwvalues[0]);
      }
      conv = convstr[0];
    }
    if (kwvalues[1] != Qundef) {
      rnd = mpfrrb_sym2rnd(kwvalues[1]);
    }
    if (kwvalues[2] != Qundef) {
      n_decimals = NUM2INT(kwvalues[2]);
    }
  }

  mpfr_ptr mp = mpfrrb_rb2ref(self);
  if (mpfr_nan_p(mp)) {
    return rb_sprintf("NaN");
  } else if (mpfr_inf_p(mp)) {
    if (mpfr_sgn(mp) < 0) {
      return rb_sprintf("-Infinity");
    } else {
      return rb_sprintf("Infinity");
    }
  }

//  if (n_decimals == 0 && conv != 'a' && conv != 'A' && conv != 'b' && conv != 'B') {
//    return mpfrrb_to_s_autoprec(mp, conv, rnd);
//  }
  if (n_decimals == 0 && conv != 'a' && conv != 'A' && conv != 'b' && conv != 'B') {
    n_decimals = mpfr_get_str_ndigits(10, mpfr_get_prec(mp));
  }

  char fmt[16];
  char *str = NULL;
  int s;
  if (n_decimals > 0) {
    ruby_snprintf(fmt, sizeof(fmt), "%%.*R*%c", conv);
    s = mpfr_asprintf(&str, fmt, n_decimals, rnd, mp);
  } else {
    ruby_snprintf(fmt, sizeof(fmt), "%%R*%c", conv);
    s = mpfr_asprintf(&str, fmt, rnd, mp);
  }
  if (s < 1 || str == NULL) {
    return Qnil;
  }
  VALUE res = rb_str_new(str, s);
  mpfr_free_str(str);
  return res;
}

#to_sollyaObject



282
283
284
285
# File 'ext/sollya_rb.c', line 282

static VALUE sollyarb_MPFR_to_sollya(VALUE self)
{
  return sollyarb_ref2rb(sollya_lib_constant(mpfrrb_rb2ref_ext(self)), c_SolFunction);
}

#trunc(op) ⇒ Integer

Set self to op rounded to the next representable integer toward zero.

Returns:



3383
3384
3385
3386
# File 'ext/mpfr_rb.c', line 3383

static VALUE mpfrrb_trunc(VALUE self, VALUE op)
{
  MPFRRB_ONE_ARG_NO_RND_FUNC_BODY(trunc);
}

#trunc!Integer

Set self to self rounded to the next representable integer toward zero.

Returns:



3444
3445
3446
3447
3448
# File 'ext/mpfr_rb.c', line 3444

static VALUE mpfrrb_trunc_B(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  return INT2NUM(mpfr_trunc(mp, mp));
}

#urandom(round: MPFR.default_rounding) ⇒ Integer

Generate a uniformly distributed random float. The floating-point number self can be seen as if a random real number is generated according to the continuous uniform distribution on the interval [0, 1] and then rounded in the direction round.

Returns:



3948
3949
3950
3951
3952
3953
3954
# File 'ext/mpfr_rb.c', line 3948

static VALUE mpfrrb_urandom(int argc, VALUE *argv, VALUE self)
{
  mpfr_rnd_t rnd = mpfrrb_get_round_single_keword(argc, argv);
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_urandom(mp, mpfrrb_gmp_randstate, rnd);
  return INT2NUM(r);
}

#urandombInteger

Generate a uniformly distributed random float in the interval 0 ≤ self < 1. More precisely, the number can be seen as a float with a random non-normalized significand and exponent 0, which is then normalized (thus if e denotes the exponent after normalization, then the least -e significant bits of the significand are always 0).

Return 0, unless the exponent is not in the current exponent range, in which case self is set to NaN and a non-zero value is returned (this should never happen in practice, except in very specific cases).

Returns:



3933
3934
3935
3936
3937
3938
# File 'ext/mpfr_rb.c', line 3933

static VALUE mpfrrb_urandomb(VALUE self)
{
  mpfr_ptr mp = mpfrrb_rb2ref(self);
  int r = mpfr_urandomb(mp, mpfrrb_gmp_randstate);
  return INT2NUM(r);
}

#y1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the second kind Bessel function of order 0 on v, rounded in the direction round. When v is NaN or negative, self is always set to NaN. When v is +Inf, self is set to +0. When v is zero, self is set to +Inf or -Inf depending on the parity and sign of n.

Returns:



3266
3267
3268
3269
# File 'ext/mpfr_rb.c', line 3266

static VALUE mpfrrb_y0(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(y0);
}

#y1(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the second kind Bessel function of order 1 on v, rounded in the direction round. When v is NaN or negative, self is always set to NaN. When v is +Inf, self is set to +0. When v is zero, self is set to +Inf or -Inf depending on the parity and sign of n.

Returns:



3279
3280
3281
3282
# File 'ext/mpfr_rb.c', line 3279

static VALUE mpfrrb_y1(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(y1);
}

#yn(n, v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the second kind Bessel function of order n on v, rounded in the direction round. When v is NaN or negative, self is always set to NaN. When v is +Inf, self is set to +0. When v is zero, self is set to +Inf or -Inf depending on the parity and sign of n.

Parameters:

Returns:



3294
3295
3296
3297
# File 'ext/mpfr_rb.c', line 3294

static VALUE mpfrrb_yn(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_INT_MP_RND_FUNC_BODY(yn);
}

#zero?Boolean

Tels if self is zero.

Returns:

  • (Boolean)


758
759
760
761
# File 'ext/mpfr_rb.c', line 758

static VALUE mpfrrb_is_zero(VALUE self)
{
  return mpfr_zero_p(mpfrrb_rb2ref(self)) ? Qtrue : Qfalse;
}

#zeta(v, round: MPFR.default_rounding) ⇒ Integer

Set self to the value of the Riemann Zeta function on v, rounded in the directionround.

Returns:



3192
3193
3194
3195
# File 'ext/mpfr_rb.c', line 3192

static VALUE mpfrrb_zeta(int argc, VALUE *argv, VALUE self)
{
  MPFRRB_ONE_ARG_FUNC_BODY(zeta);
}