Class: Numeric

Inherits:
Object show all
Includes:
Comparable
Defined in:
mrblib/numeric.rb,
src/numeric.c

Overview

Numeric

ISO 15.2.7

Direct Known Subclasses

Float, Integer

Instance Method Summary collapse

Methods included from Comparable

#==, #between?, #clamp

Instance Method Details

#**(other) ⇒ Numeric

Raises num the other power.

2.0**3 #=> 8.0

Returns:


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'src/numeric.c', line 57

static mrb_value
num_pow(mrb_state *mrb, mrb_value x)
{
  mrb_value y;
#ifndef MRB_WITHOUT_FLOAT
  mrb_float d;
#endif

  mrb_get_args(mrb, "o", &y);
  if (mrb_fixnum_p(x) && mrb_fixnum_p(y)) {
    /* try ipow() */
    mrb_int base = mrb_fixnum(x);
    mrb_int exp = mrb_fixnum(y);
    mrb_int result = 1;

    if (exp < 0)
#ifdef MRB_WITHOUT_FLOAT
      return mrb_fixnum_value(0);
#else
      goto float_pow;
#endif
    for (;;) {
      if (exp & 1) {
        if (mrb_int_mul_overflow(result, base, &result)) {
#ifndef MRB_WITHOUT_FLOAT
          goto float_pow;
#endif
        }
      }
      exp >>= 1;
      if (exp == 0) break;
      if (mrb_int_mul_overflow(base, base, &base)) {
#ifndef MRB_WITHOUT_FLOAT
        goto float_pow;
#endif
      }
    }
    return mrb_fixnum_value(result);
  }
#ifdef MRB_WITHOUT_FLOAT
  mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
#else
 float_pow:
  d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
  return mrb_float_value(mrb, d);
#endif
}

#[email protected]Object

Returns the receiver simply.

ISO 15.2.7.4.1


11
12
13
# File 'mrblib/numeric.rb', line 11

def [email protected]
  self
end

#[email protected]Object

Returns the receiver’s value, negated.

ISO 15.2.7.4.2


19
20
21
# File 'mrblib/numeric.rb', line 19

def [email protected]
  0 - self
end

#quo(numeric) ⇒ Object

Returns most exact division.


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'src/numeric.c', line 137

static mrb_value
num_div(mrb_state *mrb, mrb_value x)
{
#ifdef MRB_WITHOUT_FLOAT
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  if (!mrb_fixnum_p(y)) {
    mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
  }
  return mrb_fixnum_value(mrb_fixnum(x) / mrb_fixnum(y));
#else
  mrb_float y;

  mrb_get_args(mrb, "f", &y);
  return mrb_float_value(mrb, mrb_to_flo(mrb, x) / y);
#endif
}

#<Object

15.2.9.3.6


1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
# File 'src/numeric.c', line 1392

static mrb_value
num_lt(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  mrb_int n;

  mrb_get_args(mrb, "o", &other);
  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n < 0) return mrb_true_value();
  return mrb_false_value();
}

#<=Object


1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
# File 'src/numeric.c', line 1405

static mrb_value
num_le(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  mrb_int n;

  mrb_get_args(mrb, "o", &other);
  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n <= 0) return mrb_true_value();
  return mrb_false_value();
}

#<=>(other.f) ⇒ -1, ... #<-1

=> +1 Comparison—Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.

Overloads:

  • #<=>(other.f) ⇒ -1, ...

    Returns:

    • (-1, 0, +1)
  • #<-1

    Returns:

    • (-1)

1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
# File 'src/numeric.c', line 1372

static mrb_value
num_cmp(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  mrb_int n;

  mrb_get_args(mrb, "o", &other);
  n = cmpnum(mrb, self, other);
  if (n == -2) return mrb_nil_value();
  return mrb_fixnum_value(n);
}

#>Object


1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
# File 'src/numeric.c', line 1418

static mrb_value
num_gt(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  mrb_int n;

  mrb_get_args(mrb, "o", &other);
  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n > 0) return mrb_true_value();
  return mrb_false_value();
}

#>=Object


1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
# File 'src/numeric.c', line 1431

static mrb_value
num_ge(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  mrb_int n;

  mrb_get_args(mrb, "o", &other);
  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n >= 0) return mrb_true_value();
  return mrb_false_value();
}

#absObject

Returns the absolute value of the receiver.

ISO 15.2.7.4.3


27
28
29
30
31
32
33
# File 'mrblib/numeric.rb', line 27

def abs
  if self < 0
    -self
  else
    self
  end
end

#finite?Boolean

Returns:

  • (Boolean)

1444
1445
1446
1447
1448
1449
# File 'src/numeric.c', line 1444

static mrb_value
num_finite_p(mrb_state *mrb, mrb_value self)
{
  mrb_get_args(mrb, "");
  return mrb_true_value();
}

#infinite?Boolean

Returns:

  • (Boolean)

1451
1452
1453
1454
1455
1456
# File 'src/numeric.c', line 1451

static mrb_value
num_infinite_p(mrb_state *mrb, mrb_value self)
{
  mrb_get_args(mrb, "");
  return mrb_false_value();
}

#quo(numeric) ⇒ Object

Returns most exact division.


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'src/numeric.c', line 137

static mrb_value
num_div(mrb_state *mrb, mrb_value x)
{
#ifdef MRB_WITHOUT_FLOAT
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  if (!mrb_fixnum_p(y)) {
    mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
  }
  return mrb_fixnum_value(mrb_fixnum(x) / mrb_fixnum(y));
#else
  mrb_float y;

  mrb_get_args(mrb, "f", &y);
  return mrb_float_value(mrb, mrb_to_flo(mrb, x) / y);
#endif
}