Class: Calc::Numeric

Inherits:
Data
  • Object
show all
Defined in:
ext/calc/numeric.c,
ext/calc/numeric.c,
lib/calc/numeric.rb

Overview

Parent class to the libraries numeric classes (Calc::Q and Calc::C)

Direct Known Subclasses

C, Q

Instance Method Summary collapse

Instance Method Details

#%(other) ⇒ Calc::Numeric

Modulo operator

x % y is equivalent to x.mod(y). Rounding mode is determined by Calc.config(:mod).

Examples:

Calc::Q(11).mod(5)     #=> Calc::Q(1)
Calc::C(11, 11).mod(5) #=> Calc::C(1+1i)

Parameters:

  • y (Integer)

Returns:



13
14
15
# File 'lib/calc/numeric.rb', line 13

def %(other)
  mod other
end

#+@Calc::Numeric

Unary plus. Returns the receiver’s value.

Examples:

+Calc::C(1,1) #=> Calc::C(1,1)
+Calc::Q(1)   #=> Calc::Q(1)

Returns:



23
24
25
# File 'lib/calc/numeric.rb', line 23

def +@
  self
end

#<<(other) ⇒ Calc::C, Calc::Q

Left shift an integer by a given number of bits. This multiplies the number by the appropriate power of 2.

@example:

Calc::Q(2) << 3    #=> Calc::Q(16)
Calc::C(2, 3) << 3 #=> Calc::C(16+24i)

Parameters:

  • n (Integer)

    number of bits to shift

Returns:

Raises:

  • (Calc::MathError)

    if self is a non-integer

  • (ArgumentError)

    if abs(n) is >= 2^31



101
102
103
104
105
# File 'ext/calc/numeric.c', line 101

static VALUE
cn_shift_left(VALUE self, VALUE other)
{
    return shift(self, other, FALSE);
}

#>>(other) ⇒ Calc::C, Calc::Q

Right shift an integer by a given number of bits. This multiplies the number by the appropriate power of 2. Low bits are truncated.

@example:

Calc::Q(8) >> 2     #=> Calc::Q(2)
Calc::C(8, 12) >> 2 #=> Calc::C(2+3i)

Parameters:

  • n (Integer)

    number of bits to shift

Returns:

Raises:

  • (Calc::MathError)

    if self is a non-integer

  • (ArgumentError)

    if abs(n) is >= 2^31



118
119
120
121
122
# File 'ext/calc/numeric.c', line 118

static VALUE
cn_shift_right(VALUE self, VALUE other)
{
    return shift(self, other, TRUE);
}

#abs2Calc::C, Calc::Q

Returns the square of the absolute value

This method exists for compatibility with ruby Complex/Numeric.

Examples:

Calc::Q(5).abs2     #=> Calc::Q(25)
Calc::C(3, -4).abs2 #=> Calc:c::Q(25)

Returns:



35
36
37
# File 'lib/calc/numeric.rb', line 35

def abs2
  abs * abs
end

#ceil(ndigits = 0) ⇒ Calc::Q, Calc::C

Ceiling

For real self, returns the least integer not less than self.

For complex self, returns a complex number composed of the ceiling of the real and imaginary parts separately.

If ndigits is present, the ceiling is calculated at the nth digit instead of returning an integer.

Examples:

Calc::Q(1.23).ceil      #=> Calc::Q(2)
Calc::Q(1.23).ceil(1)   #=> Calc::Q(1.3)
Calc::C(7.8, 9.1).ceil  #=> Calc::C(8+10i)

Parameters:

  • ndigits (Integer) (defaults to: 0)

Returns:



55
56
57
# File 'lib/calc/numeric.rb', line 55

def ceil(ndigits = 0)
  appr(Q.new(10)**-ndigits, 1)
end

#cmp(other) ⇒ Object

Compare 2 values.

If x and y are both real, returns -1, 0 or 1 according as x < y, x == y or x > y.

If one or both of x and y are complex, returns a complex number composed of the real and imaginary parts being compared individually as above.

Examples:

Calc::Q(3).cmp(4)     #=> Calc::Q(-1)
Calc::Q(3).cmp(4+4i)  #=> Calc::C(-1-1i)
Calc::C(3i).cmp(3+3i) #=> Calc::Q(-1)

Parameters:

  • y (Numeric)

    value to compare self to



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/calc/numeric.c', line 138

static VALUE
cn_cmp(VALUE self, VALUE other)
{
    VALUE result;
    NUMBER *qself, *qother;
    COMPLEX *cself, *cother, *cresult;
    int r, i;
    setup_math_error();

    if (CALC_Q_P(self)) {
        qself = DATA_PTR(self);
        if (CALC_C_P(other) || RB_TYPE_P(other, T_COMPLEX)) {
            cother = value_to_complex(other);
            r = qrel(qself, cother->real);
            i = qrel(&_qzero_, cother->imag);
            comfree(cother);
        }
        else {
            qother = value_to_number(other, 0);
            r = qrel(qself, qother);
            i = 0;
            qfree(qother);
        }
    }
    else if (CALC_C_P(self)) {
        cself = DATA_PTR(self);
        if (CALC_C_P(other) || RB_TYPE_P(other, T_COMPLEX)) {
            cother = value_to_complex(other);
            r = qrel(cself->real, cother->real);
            i = qrel(cself->imag, cother->imag);
        }
        else {
            qother = value_to_number(other, 0);
            r = qrel(cself->real, qother);
            i = qrel(cself->imag, &_qzero_);
            qfree(qother);
        }
    }
    else {
        rb_raise(rb_eTypeError, "receiver must be Calc::Q or Calc::C");
    }
    if (i == 0) {
        result = cq_new();
        DATA_PTR(result) = sign_of_int(r);
    }
    else {
        result = cc_new();
        cresult = comalloc();
        qfree(cresult->real);
        cresult->real = sign_of_int(r);
        qfree(cresult->imag);
        cresult->imag = sign_of_int(i);
        DATA_PTR(result) = cresult;
    }
    return result;
}

#coerce(other) ⇒ Object

Provides support for Ruby type coercion.



233
234
235
# File 'lib/calc/numeric.rb', line 233

def coerce(other)
  [self.class.new(other), self]
end

#comb(other) ⇒ Calc::Q, Calc::C

combinatorial number

Returns the number of combinations in which ‘other` things may be chosen from `self` items ignoring order.

Examples:

Calc::Q(5).comb(3)     #=> Calc::Q(10)
Calc::Q(60).comb(30)   #=> Calc::Q(118264581564861424)
Calc::Q("5.1").comb(5) #=> Calc::Q(1.24780425)
Calc::C(8,9).comb(3)   #=> Calc::C(-227.5+97.5i)

Parameters:

  • other (Integer)

Returns:

Raises:

  • (MathError)

    if ‘other` is too large or not a positive integer



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/calc/numeric.c', line 209

static VALUE
cn_comb(VALUE self, VALUE other)
{
    VALUE result;
    NUMBER *qother, *qresult, *qdiv, *qtmp;
    COMPLEX *cresult, *ctmp1, *ctmp2;
    long n;
    setup_math_error();

    qother = value_to_number(other, 0);
    if (qisfrac(qother)) {
        qfree(qother);
        rb_raise(e_MathError, "non-integer argument to comb");
    }
    if (qisneg(qother)) {
        qfree(qother);
        result = cq_new();
        DATA_PTR(result) = qlink(&_qzero_);
        return result;
    }
    else if (qiszero(qother)) {
        qfree(qother);
        result = cq_new();
        DATA_PTR(result) = qlink(&_qone_);
        return result;
    }
    else if (qisone(qother)) {
        qfree(qother);
        return self;
    }
    else if (CALC_Q_P(self)) {
        qresult = qcomb(DATA_PTR(self), qother);
        qfree(qother);
        if (qresult == NULL) {
            rb_raise(e_MathError, "argument too large for comb");
        }
        result = cq_new();
        DATA_PTR(result) = qresult;
        return result;
    }
    /* if here, self is a Calc::C and qother is integer > 1.  algorithm based
     * on calc's func.c, but only for COMPLEX*. */
    if (zge24b(qother->num)) {
        qfree(qother);
        rb_raise(e_MathError, "argument too large for comb");
    }
    n = qtoi(qother);
    cresult = clink((COMPLEX *) DATA_PTR(self));
    ctmp1 = c_addq((COMPLEX *) DATA_PTR(self), &_qnegone_);
    qdiv = qlink(&_qtwo_);
    n--;
    for (;;) {
        ctmp2 = c_mul(cresult, ctmp1);
        comfree(cresult);
        cresult = c_divq(ctmp2, qdiv);
        comfree(ctmp2);
        if (--n == 0 || ciszero(cresult)) {
            comfree(ctmp1);
            qfree(qdiv);
            result = cc_new();
            DATA_PTR(result) = cresult;
            return result;
        }
        ctmp2 = c_addq(ctmp1, &_qnegone_);
        comfree(ctmp1);
        ctmp1 = ctmp2;
        qtmp = qinc(qdiv);
        qfree(qdiv);
        qdiv = qtmp;
    }
}

#fdiv(y) ⇒ Calc::C, Calc::Q

Division

This method exists for ruby compatibility. Note that Integer#fdiv will return a Float, however Q#div returns another Q.

Examples:

Calc::Q(2, 3).fdiv(0.5) #=> Calc::Q(~1.33333333333333333333)
Calc::C(11, 22).fdiv(3) #=> Calc::C(~3.66666666666666666667+~7.33333333333333333333i)

Parameters:

Returns:

Raises:



70
71
72
# File 'lib/calc/numeric.rb', line 70

def fdiv(y)
  self / y
end

#finite?true

Returns true - calc values are always finite

Examples:

Calc::Q(1).finite?    #=> true
Calc::C(1, 1).finite? #=> true

Returns:

  • (true)


80
81
82
# File 'lib/calc/numeric.rb', line 80

def finite?
  true
end

#floor(ndigits = 0) ⇒ Calc::Q, Calc::C

Floor

For real self, returns the greatest integer not greater than self.

For complex self, returns a complex number composed of the floor of the real and imaginary parts separately.

If ndigits is present, the floor is calculated at the nth digit instead of returning an integer.

Examples:

Calc::Q(1.23).floor     #=> Calc::Q(1)
Calc::C(7.8, 9.1).floor #=> Calc::C(7+9i)

Parameters:

  • ndigits (Integer) (defaults to: 0)

Returns:



99
100
101
# File 'lib/calc/numeric.rb', line 99

def floor(ndigits = 0)
  appr(Q.new(10)**-ndigits, 0)
end

#ilog(base) ⇒ Calc::Q

floor of logarithm to specified integer base

x.ilog(b) returns the greatest integer for which b^n <= abs(x)

Examples:

Calc::Q(2).ilog(3) #=> Calc::Q(0)
Calc::Q(8).ilog(3) #=> Calc::Q(1)
Calc::Q(9).ilog(3) #=> Calc::Q(2)

Parameters:

  • base (Integer)

Returns:

Raises:



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/calc/numeric.c', line 293

static VALUE
cn_ilog(VALUE self, VALUE base)
{
    VALUE result;
    NUMBER *qbase, *qresult;
    setup_math_error();

    qbase = value_to_number(base, 0);
    if (qisfrac(qbase) || qiszero(qbase) || qisunit(qbase) || qisneg(qbase)) {
        qfree(qbase);
        rb_raise(e_MathError, "base must be an integer > 1");
    }
    if (CALC_Q_P(self)) {
        qresult = qilog(DATA_PTR(self), qbase->num);
    }
    else if (CALC_C_P(self)) {
        qresult = c_ilog(DATA_PTR(self), qbase->num);
    }
    else {
        rb_raise(rb_eTypeError, "cn_ilog called with invalid receiver");
    }
    qfree(qbase);
    if (!qresult) {
        rb_raise(e_MathError, "invalid argument for ilog");
    }
    result = cq_new();
    DATA_PTR(result) = qresult;
    return result;
}

#ilog10Calc::Q

Floor of logarithm to base 10

Returns the greatest integer n for which 10^n <= self.

Examples:

Calc::Q("1/15").ilog10 #=> Calc::Q(-2)
Calc::Q(777).ilog10    #=> Calc::Q(2)
Calc::C(10, 10).ilog10 #=> Calc::Q(1)

Returns:

Raises:



113
114
115
# File 'lib/calc/numeric.rb', line 113

def ilog10
  ilog 10
end

#ilog2Calc::Q

Floor of logarithm to base 2

Returns the greatest integer n for which 2^n <= self

Examples:

Calc::Q("1/15").ilog2 #=> Calc::Q(-4)
Calc::Q(777).ilog2    #=> Calc::Q(9)
Calc::C(10, 10).ilog2 #=> Calc::Q(3)

Returns:

Raises:



127
128
129
# File 'lib/calc/numeric.rb', line 127

def ilog2
  ilog 2
end

#infinite?nil

Returns nil - calc values are never inifinite.

Examples:

Calc::Q(1).infinite?    #=> nil
Calc::C(1, 1).infinite? #=> nil

Returns:

  • (nil)


137
138
139
# File 'lib/calc/numeric.rb', line 137

def infinite?
  nil
end

#isintCalc::Q

Returns 1 if self is an integer, otherwise 0.

Examples:

Calc::Q(1).isint    #=> Calc::Q(1)
Calc::Q(0.5).isint  #=> Calc::Q(0)

Returns:



147
148
149
# File 'lib/calc/numeric.rb', line 147

def isint
  int? ? Q::ONE : Q::ZERO
end

#ln(*args) ⇒ Calc::Q, Calc::C

Natural logarithm

Note that this is like using ruby’s Math.log.

Examples:

Calc::Q(10).ln    #=> Calc::Q(2.30258509299404568402)
Calc::Q(-10).ln   #=> Calc::C(2.30258509299404568402+3.14159265358979323846i)
Calc::C(0, 10).ln #=> Calc::C(2.30258509299404568402+1.57079632679489661923i)

Parameters:

Returns:

Raises:



335
336
337
338
339
# File 'ext/calc/numeric.c', line 335

static VALUE
cn_ln(int argc, VALUE * argv, VALUE self)
{
    return log_function(argc, argv, self, &qln, &c_ln);
}

#log(*args) ⇒ Calc::Q, Calc::C

Base 10 logarithm

Note that this is like using ruby’s Math.log10.

Examples:

Calc::Q(-1).log     #=> Calc::C(~1.36437635384184134748i)
Calc::Q(10).log     #=> Calc::Q(1)
Calc::Q(100).log    #=> Calc::Q(2)
Calc::Q("1e10").log #=> Calc::Q(10)
Calc::C(0, 10).log  #=> Calc::C(1+~0.68218817692092067374i)

Parameters:

Returns:

Raises:



355
356
357
358
359
# File 'ext/calc/numeric.c', line 355

static VALUE
cn_log(int argc, VALUE * argv, VALUE self)
{
    return log_function(argc, argv, self, &qlog, &c_log);
}

#log2(*args) ⇒ Calc::Q

Base 2 logarithm

Examples:

Calc::Q(1).log2    #=> Calc::Q(0)
Calc::Q(2).log2    #=> Calc::Q(1)
Calc::Q(10).log2   #=> Calc::Q(~3.32192809488736234786)
Calc::Q(-2).log2   #=> Calc::C(1+~4.53236014182719380961i)
Calc::C(1, 2).log2 #=> Calc::C(~1.16096404744368117393+~1.59727796468810880664i)

Returns:



160
161
162
# File 'lib/calc/numeric.rb', line 160

def log2(*args)
  ln(*args) / Q::TWO.ln(*args)
end

#mmin(md) ⇒ Calc::Numeric

least-absol;ute-value residues modulo a specified number

x.mmin(md) is equivalent to x.mod(md, 16)

Examples:

Calc::Q(3).mmin(6)    #=> Calc::Q(3)
Calc::C(0, 3).mmin(6) #=> Calc::C(3i)

Parameters:

Returns:



173
174
175
# File 'lib/calc/numeric.rb', line 173

def mmin(md)
  mod md, 16
end

#nonzero?Boolean

Returns true if the number is not zero. For complex ‘self`, this means that either the real or imaginary parts are not zero.

Examples:

Calc::Q(0).nonzero?    #=> false
Calc::Q(1).nonzero?    #=> true
Calc::C(0, 0).nonzero? #=> false
Calc::C(1, 0).nonzero? #=> true
Calc::C(0, 1).nonzero? #=> true

Returns:

  • (Boolean)


187
188
189
# File 'lib/calc/numeric.rb', line 187

def nonzero?
  !zero?
end

#polarArray

Returns an array containing the absolute value and the argument (angle). This method exists for compatiblity with ruby’s Numeric class.

Note that: Calc.polar(a, b).polar == [a, b]

Examples:

Calc::Q(2).polar       #=> [Calc::Q(2), Calc::Q(0)]
Calc::C(1, 2).polar    #=> [Calc::Q(2.23606797749978969641), Calc::Q(1.10714871779409050302)]
Calc.polar(1, 2).polar #=> [Calc::Q(1), Calc::Q(2)]

Returns:

  • (Array)


201
202
203
# File 'lib/calc/numeric.rb', line 201

def polar
  [abs, arg]
end

#quo(*args) ⇒ Calc::Q, Calc::C

Compute integer quotient of a value by a real number (integer division)

If y is zero, returns zero.

Examples:

Calc::Q(11,5) #=> Calc::Q(2)

Parameters:

  • y (Numeric)
  • rnd (Integer)

    rounding flags, default Calc.config(:quo)

Returns:



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'ext/calc/numeric.c', line 371

static VALUE
cn_quo(int argc, VALUE * argv, VALUE self)
{
    VALUE y, rnd;
    NUMBER *qy, *qresult;
    COMPLEX *cself, *cresult;
    long r;
    setup_math_error();

    if (rb_scan_args(argc, argv, "11", &y, &rnd) == 2) {
        r = value_to_long(rnd);
    }
    else {
        r = conf->quo;
    }
    qy = value_to_number(y, 1);
    if (qiszero(qy)) {
        qfree(qy);
        rb_raise(rb_eZeroDivError, "division by zero in quo");
    }
    if (CALC_Q_P(self)) {
        qresult = qquo(DATA_PTR(self), qy, r);
        qfree(qy);
        return wrap_number(qresult);
    }
    cself = DATA_PTR(self);
    cresult = comalloc();
    qfree(cresult->real);
    qfree(cresult->imag);
    cresult->real = qquo(cself->real, qy, r);
    cresult->imag = qquo(cself->imag, qy, r);
    qfree(qy);
    return wrap_complex(cresult);
}

#rectangularArray Also known as: rect

Rerurns an array containing the real and imaginary parts as elements. This method exists for compatibility with ruby’s Numeric class.

Examples:

Calc::Q(2).rectangular    #=> [Calc::Q(2), Calc::Q(0)]
Calc::C(1, 2).rectangular #=> [Calc::Q(1), Calc::Q(2)]

Returns:

  • (Array)


212
213
214
# File 'lib/calc/numeric.rb', line 212

def rectangular
  [re, im]
end

#root(*args) ⇒ Calc::Q, Calc::C

Root of a number

x.root(n) returns the nth root of x. x can be real or complex, n must be a positive integer.

If the nth root of x is a multiple of eps, it will be returned exactly. Otherwise the returned value will be a multiple of eps close to the real nth root of x.

Examples:

Calc::Q(7).root(4)    #=> Calc::Q(1.62657656169778574321)
Calc::Q(-7).root(4)   #=> Calc::C(1.15016331689560300254+1.15016331689560300254i)
Calc::C(7, 7).root(4) #=> Calc::C(1.73971135785495811228+0.34605010474820910752i)

Parameters:

  • n (Integer)
  • eps (Numeric)

    optional epsilon, default Calc.config(:epsilon)

Returns:

Raises:



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'ext/calc/numeric.c', line 424

static VALUE
cn_root(int argc, VALUE * argv, VALUE self)
{
    VALUE n, epsilon, result;
    NUMBER *qn, *qepsilon, *qself;
    COMPLEX ctmp;
    setup_math_error();

    if (rb_scan_args(argc, argv, "11", &n, &epsilon) > 1) {
        qepsilon = value_to_number(epsilon, 1);
        if (qiszero(qepsilon)) {
            qfree(qepsilon);
            rb_raise(e_MathError, "zero epsilon for root");
        }
    }
    else {
        qepsilon = qlink(conf->epsilon);
    }
    qn = value_to_number(n, 0);
    if (qisneg(qn) || qiszero(qn) || qisfrac(qn)) {
        qfree(qepsilon);
        qfree(qn);
        rb_raise(e_MathError, "non-positive integer root");
    }
    if (CALC_Q_P(self)) {
        qself = DATA_PTR(self);
        if (!qisneg(qself)) {
            result = wrap_number(qroot(qself, qn, qepsilon));
        }
        else {
            ctmp.real = qself;
            ctmp.imag = &_qzero_;
            ctmp.links = 1;
            result = wrap_complex(c_root(&ctmp, qn, qepsilon));
        }
    }
    else {
        result = wrap_complex(c_root(DATA_PTR(self), qn, qepsilon));
    }
    qfree(qepsilon);
    qfree(qn);
    return result;
}

#scale(other) ⇒ Calc::C, Calc::Q

Scale a number by a power of 2

x.scale(n) returns the value of 2**n * x.

Unlike the << and >> operators, this function works on fractional x.

Examples:

Calc::Q(3).scale(2)    #=> Calc::Q(12)
Calc::Q(3).scale(-2)   #=> Calc::Q(0.75)
Calc::C(3, 3).scale(2) #=> Calc::C(12+12i)

Parameters:

  • n (Integer)

Returns:

Raises:

  • (ArgumentError)

    if n isn’t an integer < 2^31



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'ext/calc/numeric.c', line 482

static VALUE
cn_scale(VALUE self, VALUE other)
{
    NUMBER *qother;
    long n;
    setup_math_error();

    qother = value_to_number(other, 0);
    if (qisfrac(qother)) {
        qfree(qother);
        rb_raise(rb_eArgError, "scale by non-integer");
    }
    if (zge31b(qother->num)) {
        qfree(qother);
        rb_raise(rb_eArgError, "scale factor must be < 2^31");
    }
    n = qtoi(qother);
    qfree(qother);
    if (CALC_Q_P(self)) {
        return wrap_number(qscale(DATA_PTR(self), n));
    }
    else {
        return wrap_complex(c_scale(DATA_PTR(self), n));
    }
}

#sgnCalc::C, Calc::Q

Indicates sign of a real or complex number

For real x, x.sgn returns

-1 if x < 0
 0 if x == 0
 1 if x > 0

For complex x, x.sgn returns Calc::C(x.re.sgn, x.im.sgn)

Examples:

Calc::Q(9).sgn     #=> Calc::Q(1)
Calc::Q(0).sgn     #=> Calc::Q(0)
Calc::Q(-9).sgn    #=> Calc::Q(-1)
Calc::C(1, -1).sgn #=> Calc::C(1-1i)

Returns:



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/calc/numeric.c', line 524

static VALUE
cn_sgn(VALUE self)
{
    COMPLEX *cself, *cresult;
    setup_math_error();

    if (CALC_Q_P(self)) {
        return wrap_number(qsign(DATA_PTR(self)));
    }
    cself = DATA_PTR(self);
    cresult = comalloc();
    qfree(cresult->real);
    qfree(cresult->imag);
    cresult->real = qsign(cself->real);
    cresult->imag = qsign(cself->imag);
    return wrap_complex(cresult);
}

#sqrt(*args) ⇒ Object

Square root

Calculates the square root of self (rational or complex). If eps is provided, it specifies the accuracy/error of the calculation, otherwise config(“epsilon”) is used. If z is provided, it controls the sign and rounding if required, otherwise config(“sqrt”) is used. Type “help sqrt” in calc for a full explanation of z.

Examples:

Calc::Q(4).sqrt     #=> Calc::Q(2)
Calc::Q(5).sqrt     #=> Calc::Q(2.23606797749978969641)
Calc::C(0,8).sqrt   #=> Calc::C(2+2i)

Parameters:

  • eps (Numeric, Calc::Q)

    (optional) calculation accuracy

  • z (Integer)

    (optional) sign and rounding flags



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'ext/calc/numeric.c', line 558

static VALUE
cn_sqrt(int argc, VALUE * argv, VALUE self)
{
    VALUE result, epsilon, z;
    NUMBER *qtmp, *qepsilon;
    COMPLEX *cresult;
    long R;
    int n;
    setup_math_error();

    n = rb_scan_args(argc, argv, "02", &epsilon, &z);
    if (n >= 1) {
        qepsilon = value_to_number(epsilon, 1);
    }
    else {
        qepsilon = conf->epsilon;
    }
    if (n == 2) {
        R = FIX2LONG(z);
    }
    else {
        R = conf->sqrt;
    }
    if (CALC_Q_P(self) && !qisneg((NUMBER *) DATA_PTR(self))) {
        /* non-negative rational */
        result = cq_new();
        DATA_PTR(result) = qsqrt(DATA_PTR(self), qepsilon, R);
    }
    else {
        if (CALC_Q_P(self)) {
            /* negative rational */
            qtmp = qneg(DATA_PTR(self));
            cresult = comalloc();
            qfree(cresult->imag);
            cresult->imag = qsqrt(qtmp, qepsilon, R);
            qfree(qtmp);
        }
        else {
            /* complex */
            cresult = c_sqrt(DATA_PTR(self), qepsilon, R);
        }
        result = wrap_complex(cresult);
    }
    if (n >= 1) {
        qfree(qepsilon);
    }
    return result;
}

#to_intInteger

Invokes the child class’s ‘to_i` method to convert self to an integer.

Note that the return value is a ruby Integer. If you want to convert to an integer but have the result be a ‘Calc::Q` object, use `trunc` or `round`.

Examples:

Calc::Q("5/2").to_int #=> 2
Calc::C(2, 0).to_int  #=> 2

Returns:

  • (Integer)

Raises:

  • (RangeError)

    if self is complex with non-zero imaginary part



228
229
230
# File 'lib/calc/numeric.rb', line 228

def to_int
  to_i
end