Class: Integer
- Defined in:
- mruby/mrblib/numeric.rb,
mruby/src/numeric.c,
mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb
Overview
Integer
ISO 15.2.8
Instance Method Summary collapse
-
#%(num) ⇒ Numeric
Returns
int
moduloother
. -
#&(integer) ⇒ Object
Bitwise AND.
-
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#**(other) ⇒ Numeric
Raises
num
theother
power. -
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#-(numeric) ⇒ Numeric
Performs subtraction: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#/(num) ⇒ Numeric
Performs division: the class of the resulting object depends on the class of
num
and on the magnitude of the result. -
#< ⇒ Object
15.2.8.3.1.
-
#<<(count) ⇒ Integer, Float
Shifts int left count positions (right if count is negative).
- #<= ⇒ Object
-
#<=> ⇒ Object
> => 1 Comparison—Returns -1, 0, or 1 depending on whether int is less than, equal to, or greater than numeric.
-
#==(other) ⇒ Boolean
Return
true
ifint
equalsother
numerically. - #> ⇒ Object
- #>= ⇒ Object
-
#>>(count) ⇒ Integer, Float
Shifts int right count positions (left if count is negative).
-
#^(integer) ⇒ Object
Bitwise EXCLUSIVE OR.
-
#__coerce_step_counter ⇒ Object
15.2.8.3.30(x).
-
#ceil ⇒ Object
Returns self.
-
#ceildiv(other) ⇒ Object
call-seq: ceildiv(other) -> integer.
-
#div(other) ⇒ Integer
Performs division: resulting integer.
-
#divmod(numeric) ⇒ Array
See
Numeric#divmod
. -
#downto(num, &block) ⇒ Object
Calls the given block once for each Integer from
self
downtonum
. -
#floor ⇒ Object
Returns self.
-
#hash ⇒ Object
15.2.8.3.18.
-
#to_s(base = 10) ⇒ String
Returns a string containing the representation of int radix base (between 2 and 36).
-
#next ⇒ Object
(also: #succ)
Returns self + 1.
-
#quo ⇒ Object
15.2.7.4.5(x).
-
#round ⇒ Object
Returns self.
-
#step(num = nil, step = 1, &block) ⇒ Object
Calls the given block from
self
tonum
incremented bystep
(default 1). -
#times(&block) ⇒ Object
Calls the given block
self
times. -
#to_f ⇒ Object
15.2.8.3.23.
-
#to_i ⇒ Integer
As int is already an
Integer
, all these methods simply return the receiver. -
#to_i ⇒ Integer
As int is already an
Integer
, all these methods simply return the receiver. -
#to_s(base = 10) ⇒ String
Returns a string containing the representation of int radix base (between 2 and 36).
-
#truncate ⇒ Object
Returns self.
-
#upto(num, &block) ⇒ Object
Calls the given block once for each Integer from
self
uptonum
. -
#|(integer) ⇒ Object
Bitwise OR.
-
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
Methods inherited from Numeric
#+@, #-@, #abs, #allbits?, #anybits?, #eql?, #finite?, #infinite?, #negative?, #nobits?, #nonzero?, #positive?, #to_c, #to_r, #zero?
Methods included from Comparable
Instance Method Details
#%(num) ⇒ Numeric
Returns int
modulo other
. See numeric.divmod
for more information.
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 |
# File 'mruby/src/numeric.c', line 1232
static mrb_value
int_mod(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_int a, b;
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_mod(mrb, x, y);
}
#endif
a = mrb_integer(x);
if (a == 0) return x;
if (mrb_integer_p(y)) {
b = mrb_integer(y);
if (b == 0) mrb_int_zerodiv(mrb);
if (a == MRB_INT_MIN && b == -1) return mrb_fixnum_value(0);
mrb_int mod = a % b;
if ((a < 0) != (b < 0) && mod != 0) {
mod += b;
}
return mrb_int_value(mrb, mod);
}
#ifdef MRB_NO_FLOAT
mrb_raise(mrb, E_TYPE_ERROR, "non integer modulo");
#else
mrb_float mod;
flodivmod(mrb, (mrb_float)a, mrb_as_float(mrb, y), NULL, &mod);
return mrb_float_value(mrb, mod);
#endif
}
|
#&(integer) ⇒ Object
Bitwise AND.
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 |
# File 'mruby/src/numeric.c', line 1409
static mrb_value
int_and(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_and(mrb, x, y);
}
if (mrb_bigint_p(y)) {
return mrb_bint_and(mrb, mrb_as_bint(mrb, x), y);
}
#endif
bit_op(x, y, and, &);
}
|
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 |
# File 'mruby/src/numeric.c', line 1188
static mrb_value
int_mul(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_mul(mrb, x, y);
}
#endif
return mrb_int_mul(mrb, x, y);
}
|
#**(other) ⇒ Numeric
Raises num
the other
power.
2.0**3 #=> 8.0
110 111 112 113 114 |
# File 'mruby/src/numeric.c', line 110
static mrb_value
int_pow(mrb_state *mrb, mrb_value x)
{
return mrb_int_pow(mrb, x, mrb_get_arg1(mrb));
}
|
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 |
# File 'mruby/src/numeric.c', line 1831
static mrb_value
int_add(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(self)) {
return mrb_bint_add(mrb, self, other);
}
#endif
return mrb_int_add(mrb, self, other);
}
|
#-(numeric) ⇒ Numeric
Performs subtraction: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 |
# File 'mruby/src/numeric.c', line 1895
static mrb_value
int_sub(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(self)) {
return mrb_bint_sub(mrb, self, other);
}
#endif
return mrb_int_sub(mrb, self, other);
}
|
#/(num) ⇒ Numeric
Performs division: the class of the resulting object depends on the class of num
and on the magnitude of the result.
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 |
# File 'mruby/src/numeric.c', line 152
static mrb_value
int_div(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_div(mrb, x, y);
}
#endif
mrb_int a = mrb_integer(x);
if (mrb_integer_p(y)) {
return mrb_div_int_value(mrb, a, mrb_integer(y));
}
switch (mrb_type(y)) {
#ifdef MRB_USE_BIGINT
case MRB_TT_BIGINT:
return mrb_bint_div(mrb, mrb_bint_new_int(mrb, a), y);
#endif
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_rational_div(mrb, mrb_rational_new(mrb, a, 1), y);
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
x = mrb_complex_new(mrb, (mrb_float)a, 0);
return mrb_complex_div(mrb, x, y);
#endif
#ifndef MRB_NO_FLOAT
case MRB_TT_FLOAT:
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y)));
#endif
default:
mrb_int_noconv(mrb, y);
}
}
|
#< ⇒ Object
15.2.8.3.1
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 |
# File 'mruby/src/numeric.c', line 2080
static mrb_value
num_lt(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
mrb_int n;
n = cmpnum(mrb, self, other);
if (n == -2) cmperr(mrb, self, other);
if (n < 0) return mrb_true_value();
return mrb_false_value();
}
|
#<<(count) ⇒ Integer, Float
Shifts int left count positions (right if count is negative).
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 |
# File 'mruby/src/numeric.c', line 1519
static mrb_value
int_lshift(mrb_state *mrb, mrb_value x)
{
mrb_int width, val;
mrb_get_args(mrb, "i", &width);
if (width == 0) {
return x;
}
if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift");
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_lshift(mrb, x, width);
}
#endif
val = mrb_integer(x);
if (val == 0) return x;
if (!mrb_num_shift(mrb, val, width, &val)) {
#ifdef MRB_USE_BIGINT
return mrb_bint_lshift(mrb, mrb_bint_new_int(mrb, val), width);
#else
mrb_int_overflow(mrb, "bit shift");
#endif
}
return mrb_int_value(mrb, val);
}
|
#<= ⇒ Object
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 |
# File 'mruby/src/numeric.c', line 2092
static mrb_value
num_le(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
mrb_int n;
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 <i>int</i> is
less than, equal to, or greater than <i>numeric</i>. This is the
basis for the tests in <code>Comparable</code>. When the operands are
not comparable, it returns nil instead of raising an exception.
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 |
# File 'mruby/src/numeric.c', line 2063
static mrb_value
num_cmp(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
mrb_int n;
n = cmpnum(mrb, self, other);
if (n == -2) return mrb_nil_value();
return mrb_fixnum_value(n);
}
|
#==(other) ⇒ Boolean
Return true
if int
equals other
numerically.
1 == 2 #=> false
1 == 1.0 #=> true
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 |
# File 'mruby/src/numeric.c', line 1334
static mrb_value
int_equal(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
switch (mrb_type(y)) {
case MRB_TT_INTEGER:
return mrb_bool_value(mrb_integer(x) == mrb_integer(y));
#ifndef MRB_NO_FLOAT
case MRB_TT_FLOAT:
return mrb_bool_value((mrb_float)mrb_integer(x) == mrb_float(y));
#endif
#ifdef MRB_USE_BIGINT
case MRB_TT_BIGINT:
return mrb_bool_value(mrb_bint_cmp(mrb, y, x) == 0);
#endif
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_bool_value(mrb_equal(mrb, y, x));
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
return mrb_bool_value(mrb_equal(mrb, y, x));
#endif
default:
return mrb_false_value();
}
}
|
#> ⇒ Object
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 |
# File 'mruby/src/numeric.c', line 2104
static mrb_value
num_gt(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
mrb_int n;
n = cmpnum(mrb, self, other);
if (n == -2) cmperr(mrb, self, other);
if (n > 0) return mrb_true_value();
return mrb_false_value();
}
|
#>= ⇒ Object
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 |
# File 'mruby/src/numeric.c', line 2116
static mrb_value
num_ge(mrb_state *mrb, mrb_value self)
{
mrb_value other = mrb_get_arg1(mrb);
mrb_int n;
n = cmpnum(mrb, self, other);
if (n == -2) cmperr(mrb, self, other);
if (n >= 0) return mrb_true_value();
return mrb_false_value();
}
|
#>>(count) ⇒ Integer, Float
Shifts int right count positions (left if count is negative).
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 |
# File 'mruby/src/numeric.c', line 1554
static mrb_value
int_rshift(mrb_state *mrb, mrb_value x)
{
mrb_int width, val;
mrb_get_args(mrb, "i", &width);
if (width == 0) {
return x;
}
if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift");
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_rshift(mrb, x, width);
}
#endif
val = mrb_integer(x);
if (val == 0) return x;
if (!mrb_num_shift(mrb, val, -width, &val)) {
#ifdef MRB_USE_BIGINT
return mrb_bint_rshift(mrb, mrb_bint_new_int(mrb, val), width);
#else
mrb_int_overflow(mrb, "bit shift");
#endif
}
return mrb_int_value(mrb, val);
}
|
#^(integer) ⇒ Object
Bitwise EXCLUSIVE OR.
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 |
# File 'mruby/src/numeric.c', line 1457
static mrb_value
int_xor(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_xor(mrb, x, y);
}
if (mrb_bigint_p(y)) {
return mrb_bint_xor(mrb, mrb_as_bint(mrb, x), y);
}
#endif
bit_op(x, y, or, ^);
}
|
#__coerce_step_counter ⇒ Object
15.2.8.3.30(x)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'mruby/src/numeric.c', line 258
static mrb_value
coerce_step_counter(mrb_state *mrb, mrb_value self)
{
mrb_value num, step;
mrb_get_args(mrb, "oo", &num, &step);
#ifndef MRB_NO_FLOAT
mrb->c->ci->mid = 0;
if (mrb_float_p(num) || mrb_float_p(step)) {
return mrb_ensure_float_type(mrb, self);
}
#endif
return self;
}
|
#ceil ⇒ Integer #ceil(ndigits) ⇒ Integer
Returns self.
When the precision (ndigits) is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 |
# File 'mruby/src/numeric.c', line 1615
static mrb_value
int_ceil(mrb_state *mrb, mrb_value x)
{
mrb_value f = prepare_int_rounding(mrb, x);
if (mrb_undef_p(f)) return mrb_fixnum_value(0);
if (mrb_nil_p(f)) return x;
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_add(mrb, x, mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f)));
}
#endif
mrb_int a = mrb_integer(x);
mrb_int b = mrb_integer(f);
int neg = a < 0;
if (neg) a = -a;
else a += b - 1;
a = a / b * b;
if (neg) a = -a;
return mrb_int_value(mrb, a);
}
|
#ceildiv(other) ⇒ Object
call-seq:
ceildiv(other) -> integer
Returns the result of division +self+ by +other+. The
result is rounded up to the nearest integer.
3.ceildiv(3) # => 1
4.ceildiv(3) # => 2
4.ceildiv(-3) # => -1
-4.ceildiv(3) # => -1
-4.ceildiv(-3) # => 2
3.ceildiv(1.2) # => 3
96 97 98 |
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 96 def ceildiv(other) -div(-other) end |
#div(other) ⇒ Integer
Performs division: resulting integer.
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'mruby/src/numeric.c', line 203
static mrb_value
int_idiv(mrb_state *mrb, mrb_value x)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_div(mrb, x, mrb_get_arg1(mrb));
}
#endif
mrb_int y;
mrb_get_args(mrb, "i", &y);
return mrb_div_int_value(mrb, mrb_integer(x), y);
}
|
#divmod(numeric) ⇒ Array
See Numeric#divmod
.
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 |
# File 'mruby/src/numeric.c', line 1275
static mrb_value
int_divmod(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
#ifndef MRB_NO_FLOAT
if (mrb_float_p(y)) {
mrb_float f = mrb_bint_as_float(mrb, x);
return flo_divmod(mrb, mrb_float_value(mrb, f));
}
#endif
return mrb_bint_divmod(mrb, x, y);
}
#endif
if (mrb_integer_p(y)) {
mrb_int div, mod;
intdivmod(mrb, mrb_integer(x), mrb_integer(y), &div, &mod);
return mrb_assoc_new(mrb, mrb_int_value(mrb, div), mrb_int_value(mrb, mod));
}
#ifdef MRB_NO_FLOAT
mrb_raise(mrb, E_TYPE_ERROR, "non integer divmod");
#else
return flo_divmod(mrb, x);
#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 'mruby/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 |
#floor ⇒ Integer #floor(ndigits) ⇒ Integer
Returns self.
When the precision (ndigits) is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 |
# File 'mruby/src/numeric.c', line 1647
static mrb_value
int_floor(mrb_state *mrb, mrb_value x)
{
mrb_value f = prepare_int_rounding(mrb, x);
if (mrb_undef_p(f)) return mrb_fixnum_value(0);
if (mrb_nil_p(f)) return x;
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f));
}
#endif
mrb_int a = mrb_integer(x);
mrb_int b = mrb_integer(f);
int neg = a < 0;
if (neg) a = -a + b - 1;
a = a / b * b;
if (neg) a = -a;
return mrb_int_value(mrb, a);
}
|
#hash ⇒ Object
15.2.8.3.18
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 |
# File 'mruby/src/numeric.c', line 2038
static mrb_value
int_hash(mrb_state *mrb, mrb_value self)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(self)) {
return mrb_bint_hash(mrb, self);
}
#endif
mrb_int n = mrb_integer(self);
return mrb_int_value(mrb, mrb_byte_hash((uint8_t*)&n, sizeof(n)));
}
|
#to_s(base = 10) ⇒ String
Returns a string containing the representation of int radix base (between 2 and 36).
12345.to_s #=> "12345"
12345.to_s(2) #=> "11000000111001"
12345.to_s(8) #=> "30071"
12345.to_s(10) #=> "12345"
12345.to_s(16) #=> "3039"
12345.to_s(36) #=> "9ix"
1978 1979 1980 1981 1982 1983 1984 1985 |
# File 'mruby/src/numeric.c', line 1978
static mrb_value
int_to_s(mrb_state *mrb, mrb_value self)
{
mrb_int base = 10;
mrb_get_args(mrb, "|i", &base);
return mrb_integer_to_str(mrb, self, base);
}
|
#next ⇒ Object Also known as: succ
Returns self + 1
ISO 15.2.8.3.19
62 63 64 |
# File 'mruby/mrblib/numeric.rb', line 62 def next self + 1 end |
#quo ⇒ Object
15.2.7.4.5(x)
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 |
# File 'mruby/src/numeric.c', line 217
static mrb_value
int_quo(mrb_state *mrb, mrb_value x)
{
#ifndef MRB_USE_RATIONAL
#ifdef MRB_NO_FLOAT
return int_idiv(mrb, x);
#else
mrb_float y;
mrb_get_args(mrb, "f", &y);
if (y == 0) {
mrb_int_zerodiv(mrb);
}
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_float_value(mrb, mrb_bint_as_float(mrb, x) / y);
}
#endif
return mrb_float_value(mrb, mrb_integer(x) / y);
#endif
#else
mrb_int a = mrb_integer(x);
mrb_value y = mrb_get_arg1(mrb);
if (mrb_integer_p(y) && mrb_class_defined_id(mrb, MRB_SYM(Rational))) {
return mrb_rational_new(mrb, a, mrb_integer(y));
}
switch (mrb_type(y)) {
case MRB_TT_RATIONAL:
x = mrb_rational_new(mrb, a, 1);
return mrb_rational_div(mrb, x, y);
default:
#ifndef MRB_NO_FLOAT
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y)));
#else
mrb_int_noconv(mrb, y);
break;
#endif
}
#endif
}
|
#round ⇒ Integer #round(ndigits) ⇒ Integer
Returns self.
When the precision (ndigits) is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 |
# File 'mruby/src/numeric.c', line 1678
static mrb_value
int_round(mrb_state *mrb, mrb_value x)
{
mrb_value f = prepare_int_rounding(mrb, x);
if (mrb_undef_p(f)) return mrb_fixnum_value(0);
if (mrb_nil_p(f)) return x;
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
mrb_value r = mrb_bint_mod(mrb, x, f);
mrb_value n = mrb_bint_sub(mrb, x, r);
mrb_value h = mrb_bigint_p(f) ? mrb_bint_rshift(mrb, f, 1) : mrb_int_value(mrb, mrb_integer(f)>>1);
mrb_int cmp = mrb_bigint_p(r) ? mrb_bint_cmp(mrb, r, h) : (mrb_integer(r) - mrb_integer(h));
if ((cmp > 0) || (cmp == 0 && mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) > 0)) {
n = mrb_as_bint(mrb, n);
n = mrb_bint_add(mrb, n, f);
}
return n;
}
#endif
mrb_int a = mrb_integer(x);
mrb_int b = mrb_integer(f);
int neg = a < 0;
if (neg) a = -a;
a = (a + b / 2) / b * b;
if (neg) a = -a;
return mrb_int_value(mrb, a);
}
|
#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 'mruby/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 'mruby/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 |
#to_f ⇒ Object
15.2.8.3.23
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 |
# File 'mruby/src/numeric.c', line 1753
static mrb_value
int_to_f(mrb_state *mrb, mrb_value num)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(num)) {
return mrb_float_value(mrb, mrb_bint_as_float(mrb, num));
}
#endif
return mrb_float_value(mrb, (mrb_float)mrb_integer(num));
}
|
#to_i ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1120 1121 1122 1123 1124 |
# File 'mruby/src/numeric.c', line 1120
static mrb_value
int_to_i(mrb_state *mrb, mrb_value num)
{
return num;
}
|
#to_i ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1120 1121 1122 1123 1124 |
# File 'mruby/src/numeric.c', line 1120
static mrb_value
int_to_i(mrb_state *mrb, mrb_value num)
{
return num;
}
|
#to_s(base = 10) ⇒ String
Returns a string containing the representation of int radix base (between 2 and 36).
12345.to_s #=> "12345"
12345.to_s(2) #=> "11000000111001"
12345.to_s(8) #=> "30071"
12345.to_s(10) #=> "12345"
12345.to_s(16) #=> "3039"
12345.to_s(36) #=> "9ix"
1978 1979 1980 1981 1982 1983 1984 1985 |
# File 'mruby/src/numeric.c', line 1978
static mrb_value
int_to_s(mrb_state *mrb, mrb_value self)
{
mrb_int base = 10;
mrb_get_args(mrb, "|i", &base);
return mrb_integer_to_str(mrb, self, base);
}
|
#truncate ⇒ Integer #truncate(ndigits) ⇒ Integer
Returns self.
When the precision (ndigits) is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 |
# File 'mruby/src/numeric.c', line 1717
static mrb_value
int_truncate(mrb_state *mrb, mrb_value x)
{
mrb_value f = prepare_int_rounding(mrb, x);
if (mrb_undef_p(f)) return mrb_fixnum_value(0);
if (mrb_nil_p(f)) return x;
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
mrb_value m = mrb_bint_mod(mrb, x, f);
if (mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) < 0) {
return mrb_bint_add(mrb, x, mrb_bint_sub(mrb, x, m));
}
else {
return mrb_bint_sub(mrb, x, m);
}
}
#endif
mrb_int a = mrb_integer(x);
mrb_int b = mrb_integer(f);
int neg = a < 0;
if (neg) a = -a;
a = a / b * b;
if (neg) a = -a;
return mrb_int_value(mrb, a);
}
|
#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 'mruby/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 |
#|(integer) ⇒ Object
Bitwise OR.
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 |
# File 'mruby/src/numeric.c', line 1433
static mrb_value
int_or(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_or(mrb, x, y);
}
if (mrb_bigint_p(y)) {
return mrb_bint_or(mrb, mrb_as_bint(mrb, x), y);
}
#endif
bit_op(x, y, or, |);
}
|
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
ex.0---00001 (1)-> 1---11110 (-2)
ex.0---00010 (2)-> 1---11101 (-3)
ex.0---00100 (4)-> 1---11011 (-5)
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
# File 'mruby/src/numeric.c', line 1374
static mrb_value
int_rev(mrb_state *mrb, mrb_value num)
{
mrb_int val = mrb_integer(num);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(num)) {
mrb_bint_rev(mrb, num);
}
#endif
return mrb_int_value(mrb, ~val);
}
|