Module: Integral
- Included in:
- Integer
- Defined in:
- mrblib/numeric.rb,
src/numeric.c
Overview
Integral
mruby special - module to share methods between Floats and Integers to make them compatible
Instance Method Summary collapse
-
#**(other) ⇒ Numeric
Raises
numtheotherpower. -
#quo(numeric) ⇒ Object
Returns most exact division.
-
#< ⇒ Object
15.2.8,9.3.1.
-
#<= ⇒ Object
-
#<=> ⇒ Object
=> +1 Comparison—Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than numeric.
-
#> ⇒ Object
-
#>= ⇒ Object
-
#__coerce_step_counter ⇒ Object
-
#div ⇒ Object
15.2.7.4.5 (x).
-
#downto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ downto +num+.
-
#next ⇒ Object
(also: #succ)
Returns self + 1.
-
#quo(numeric) ⇒ Object
Returns most exact division.
-
#step(num = nil, step = 1, &block) ⇒ Object
Calls the given block from +self+ to +num+ incremented by +step+ (default 1).
-
#times(&block) ⇒ Object
Calls the given block +self+ times.
-
#upto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ upto +num+.
Instance Method Details
#**(other) ⇒ Numeric
Raises num the other power.
2.0**3 #=> 8.0
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 104 105 106 107 108 109 110 111 112 113 |
# File 'src/numeric.c', line 67 static mrb_value integral_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 } |
#quo(numeric) ⇒ Object
Returns most exact division.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'src/numeric.c', line 153 static mrb_value integral_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.8,9.3.1
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 |
# File 'src/numeric.c', line 1503 static mrb_value integral_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
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 |
# File 'src/numeric.c', line 1516 static mrb_value integral_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.
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 |
# File 'src/numeric.c', line 1485 static mrb_value integral_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
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 |
# File 'src/numeric.c', line 1529 static mrb_value integral_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
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 |
# File 'src/numeric.c', line 1542 static mrb_value integral_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(); } |
#__coerce_step_counter ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'src/numeric.c', line 172 static mrb_value integral_coerce_step_counter(mrb_state *mrb, mrb_value self) { mrb_value num, step; mrb_get_args(mrb, "oo", &num, &step); #ifndef MRB_WITHOUT_FLOAT if (mrb_float_p(self) || mrb_float_p(num) || mrb_float_p(step)) { return mrb_Float(mrb, self); } #endif return self; } |
#div ⇒ Object
15.2.7.4.5 (x)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'src/numeric.c', line 115 static mrb_value integral_idiv(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_int_value(mrb, mrb_to_flo(mrb, x) / y); #endif } |
#downto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ downto +num+.
ISO 15.2.8.3.15
47 48 49 50 51 52 53 54 55 56 |
# File 'mrblib/numeric.rb', line 47 def downto(num, &block) return to_enum(:downto, num) unless block i = self.to_i while i >= num block.call(i) i -= 1 end self end |
#next ⇒ Object Also known as: succ
Returns self + 1
ISO 15.2.8.3.19
62 63 64 |
# File 'mrblib/numeric.rb', line 62 def next self + 1 end |
#quo(numeric) ⇒ Object
Returns most exact division.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'src/numeric.c', line 153 static mrb_value integral_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 } |
#step(num = nil, step = 1, &block) ⇒ Object
Calls the given block from +self+ to +num+ incremented by +step+ (default 1).
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'mrblib/numeric.rb', line 103 def step(num=nil, step=1, &block) raise ArgumentError, "step can't be 0" if step == 0 return to_enum(:step, num, step) unless block i = __coerce_step_counter(num, step) if num == self || step.infinite? block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) elsif num == nil while true block.call(i) i += step end elsif step > 0 while i <= num block.call(i) i += step end else while i >= num block.call(i) i += step end end self end |
#times(&block) ⇒ Object
Calls the given block +self+ times.
ISO 15.2.8.3.22
72 73 74 75 76 77 78 79 80 81 |
# File 'mrblib/numeric.rb', line 72 def times &block return to_enum :times unless block i = 0 while i < self block.call i i += 1 end self end |
#upto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ upto +num+.
ISO 15.2.8.3.27
88 89 90 91 92 93 94 95 96 97 |
# File 'mrblib/numeric.rb', line 88 def upto(num, &block) return to_enum(:upto, num) unless block i = self.to_i while i <= num block.call(i) i += 1 end self end |