Class: Calc::Numeric
- Inherits:
-
Data
- Object
- Data
- Calc::Numeric
- 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)
Instance Method Summary collapse
-
#%(other) ⇒ Calc::Numeric
Modulo operator.
-
#+@ ⇒ Calc::Numeric
Unary plus.
-
#<<(other) ⇒ Calc::C, Calc::Q
Left shift an integer by a given number of bits.
-
#>>(other) ⇒ Calc::C, Calc::Q
Right shift an integer by a given number of bits.
-
#abs2 ⇒ Calc::C, Calc::Q
Returns the square of the absolute value.
-
#ceil(ndigits = 0) ⇒ Calc::Q, Calc::C
Ceiling.
-
#cmp(other) ⇒ Object
Compare 2 values.
-
#coerce(other) ⇒ Object
Provides support for Ruby type coercion.
-
#comb(other) ⇒ Calc::Q, Calc::C
combinatorial number.
-
#fdiv(y) ⇒ Calc::C, Calc::Q
Division.
-
#finite? ⇒ true
Returns true - calc values are always finite.
-
#floor(ndigits = 0) ⇒ Calc::Q, Calc::C
Floor.
-
#ilog(base) ⇒ Calc::Q
floor of logarithm to specified integer base.
-
#ilog10 ⇒ Calc::Q
Floor of logarithm to base 10.
-
#ilog2 ⇒ Calc::Q
Floor of logarithm to base 2.
-
#infinite? ⇒ nil
Returns nil - calc values are never inifinite.
-
#isint ⇒ Calc::Q
Returns 1 if self is an integer, otherwise 0.
-
#ln(*args) ⇒ Calc::Q, Calc::C
Natural logarithm.
-
#log(*args) ⇒ Calc::Q, Calc::C
Base 10 logarithm.
-
#log2(*args) ⇒ Calc::Q
Base 2 logarithm.
-
#mmin(md) ⇒ Calc::Numeric
least-absol;ute-value residues modulo a specified number.
-
#nonzero? ⇒ Boolean
Returns true if the number is not zero.
-
#polar ⇒ Array
Returns an array containing the absolute value and the argument (angle).
-
#quo(*args) ⇒ Calc::Q, Calc::C
Compute integer quotient of a value by a real number (integer division).
-
#rectangular ⇒ Array
(also: #rect)
Rerurns an array containing the real and imaginary parts as elements.
-
#root(*args) ⇒ Calc::Q, Calc::C
Root of a number.
-
#scale(other) ⇒ Calc::C, Calc::Q
Scale a number by a power of 2.
-
#sgn ⇒ Calc::C, Calc::Q
Indicates sign of a real or complex number.
-
#sqrt(*args) ⇒ Object
Square root.
-
#to_int ⇒ Integer
Invokes the child class’s ‘to_i` method to convert self to an integer.
Instance Method Details
#%(other) ⇒ Calc::Numeric
Modulo operator
x % y is equivalent to x.mod(y). Rounding mode is determined by Calc.config(:mod).
13 14 15 |
# File 'lib/calc/numeric.rb', line 13 def %(other) mod other end |
#+@ ⇒ Calc::Numeric
Unary plus. Returns the receiver’s value.
23 24 25 |
# File 'lib/calc/numeric.rb', line 23 def +@ self end |
#<<(other) ⇒ Calc::C, Calc::Q
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
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);
}
|
#abs2 ⇒ Calc::C, Calc::Q
Returns the square of the absolute value
This method exists for compatibility with ruby Complex/Numeric.
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.
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.
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.
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.
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
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.
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)
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;
}
|
#ilog10 ⇒ Calc::Q
Floor of logarithm to base 10
Returns the greatest integer n for which 10^n <= self.
113 114 115 |
# File 'lib/calc/numeric.rb', line 113 def ilog10 ilog 10 end |
#ilog2 ⇒ Calc::Q
Floor of logarithm to base 2
Returns the greatest integer n for which 2^n <= self
127 128 129 |
# File 'lib/calc/numeric.rb', line 127 def ilog2 ilog 2 end |
#infinite? ⇒ nil
Returns nil - calc values are never inifinite.
137 138 139 |
# File 'lib/calc/numeric.rb', line 137 def infinite? nil end |
#isint ⇒ Calc::Q
Returns 1 if self is an integer, otherwise 0.
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.
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.
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
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)
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.
187 188 189 |
# File 'lib/calc/numeric.rb', line 187 def nonzero? !zero? end |
#polar ⇒ Array
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]
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.
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);
}
|
#rectangular ⇒ Array 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.
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.
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.
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));
}
}
|
#sgn ⇒ Calc::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)
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.
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_int ⇒ Integer
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`.
228 229 230 |
# File 'lib/calc/numeric.rb', line 228 def to_int to_i end |