Class: Integer
Overview
******************************************************************
Holds Integer values. You cannot add a singleton method to an
Integer object, any attempt to do so will raise a TypeError.
Constant Summary collapse
- GMP_VERSION =
The version of loaded GMP.
rb_sprintf("GMP %s", gmp_version)
Class Method Summary collapse
-
.sqrt(n) ⇒ Integer
Returns the integer square root of the non-negative integer
n
, i.e.
Instance Method Summary collapse
-
#%(y) ⇒ Object
Returns
int
moduloother
. -
#&(other_int) ⇒ Integer
Bitwise AND.
-
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of
numeric
. -
#**(numeric) ⇒ Object
Raises
int
to the power ofnumeric
, which may be negative or fractional. -
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of
numeric
. -
#-(numeric) ⇒ Object
Performs subtraction: the class of the resulting object depends on the class of
numeric
. -
#- ⇒ Integer
Returns
int
, negated. -
#/(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of
numeric
. -
#<(real) ⇒ Boolean
Returns
true
if the value ofint
is less than that ofreal
. -
#<<(count) ⇒ Integer
Returns
int
shifted leftcount
positions, or right ifcount
is negative. -
#<=(real) ⇒ Boolean
Returns
true
if the value ofint
is less than or equal to that ofreal
. -
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1, 0, or +1 depending on whether
int
is less than, equal to, or greater thannumeric
. -
#==(other) ⇒ Boolean
Returns
true
ifint
equalsother
numerically. -
#==(other) ⇒ Boolean
Returns
true
ifint
equalsother
numerically. -
#>(real) ⇒ Boolean
Returns
true
if the value ofint
is greater than that ofreal
. -
#>=(real) ⇒ Boolean
Returns
true
if the value ofint
is greater than or equal to that ofreal
. -
#>>(count) ⇒ Integer
Returns
int
shifted rightcount
positions, or left ifcount
is negative. -
#[](*, const) ⇒ Object
Bit Reference—Returns the
n
th bit in the binary representation ofint
, whereint[0]
is the least significant bit. -
#^(other_int) ⇒ Integer
Bitwise EXCLUSIVE OR.
- #abs ⇒ Object
-
#allbits?(mask) ⇒ Boolean
Returns
true
if all bits ofint & mask
are 1. -
#anybits?(mask) ⇒ Boolean
Returns
true
if any bits ofint & mask
are 1. -
#bit_length ⇒ Integer
Returns the number of bits of the value of
int
. -
#ceil([ndigits]) ⇒ Integer, Float
Returns the smallest number greater than or equal to
int
with a precision ofndigits
decimal digits (default: 0). -
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the
int
‘s value according toencoding
. -
#coerce(numeric) ⇒ Array
Returns an array with both a
numeric
and abig
represented as Bignum objects. -
#denominator ⇒ 1
Returns 1.
-
#digits(*args) ⇒ Object
Returns the digits of
int
‘s place-value representation with radixbase
(default: 10). -
#div(numeric) ⇒ Integer
Performs integer division: returns the integer result of dividing
int
bynumeric
. -
#divmod(numeric) ⇒ Array
See Numeric#divmod.
-
#downto(to) ⇒ Object
Iterates the given block, passing in decreasing values from
int
down to and includinglimit
. -
#even? ⇒ Boolean
Returns
true
ifint
is an even number. -
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing
int
bynumeric
. -
#floor([ndigits]) ⇒ Integer, Float
Returns the largest number less than or equal to
int
with a precision ofndigits
decimal digits (default: 0). -
#gcd(other_int) ⇒ Integer
Returns the greatest common divisor of the two integers.
-
#gcdlcm(other_int) ⇒ Array
Returns an array with the greatest common divisor and the least common multiple of the two integers, [gcd, lcm].
-
#integer? ⇒ true
Since
int
is already an Integer, this always returnstrue
. -
#lcm(other_int) ⇒ Integer
Returns the least common multiple of the two integers.
-
#magnitude ⇒ Object
Returns the absolute value of
int
. -
#modulo(y) ⇒ Object
Returns
int
moduloother
. -
#next ⇒ Object
Returns the successor of
int
, i.e. -
#nobits?(mask) ⇒ Boolean
Returns
true
if no bits ofint & mask
are 1. -
#numerator ⇒ self
Returns self.
-
#odd? ⇒ Boolean
Returns
true
ifint
is an odd number. -
#ord ⇒ self
Returns the
int
itself. -
#pow(*, const) ⇒ Object
Returns (modular) exponentiation as:.
- #pred ⇒ Object
-
#rationalize([eps]) ⇒ Object
Returns the value as a rational.
-
#remainder(numeric) ⇒ Object
Returns the remainder after dividing
int
bynumeric
. -
#round([ndigits][, half: mode]) ⇒ Integer, Float
Returns
int
rounded to the nearest value with a precision ofndigits
decimal digits (default: 0). -
#size ⇒ Integer
Returns the number of bytes in the machine representation of
int
(machine dependent). -
#succ ⇒ Object
Returns the successor of
int
, i.e. -
#times ⇒ Object
Iterates the given block
int
times, passing in values from zero toint - 1
. -
#to_f ⇒ Float
Converts
int
to a Float. -
#to_i ⇒ Object
Since
int
is already an Integer, returnsself
. -
#to_int ⇒ Object
Since
int
is already an Integer, returnsself
. -
#to_r ⇒ Object
Returns the value as a rational.
-
#to_s(base = 10) ⇒ String
(also: #inspect)
Returns a string containing the place-value representation of
int
with radixbase
(between 2 and 36). -
#truncate([ndigits]) ⇒ Integer, Float
Returns
int
truncated (toward zero) to a precision ofndigits
decimal digits (default: 0). -
#upto(to) ⇒ Object
Iterates the given block, passing in integer values from
int
up to and includinglimit
. -
#|(other_int) ⇒ Integer
Bitwise OR.
-
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
Methods inherited from Numeric
#+@, #abs2, #angle, #arg, #clone, #conj, #conjugate, #dup, #eql?, #finite?, #i, #imag, #imaginary, #infinite?, #negative?, #nonzero?, #phase, #polar, #positive?, #quo, #real, #real?, #rect, #rectangular, #singleton_method_added, #step, #to_c, #zero?
Methods included from Comparable
Class Method Details
.sqrt(n) ⇒ Integer
Returns the integer square root of the non-negative integer n
, i.e. the largest non-negative integer less than or equal to the square root of n
.
Integer.sqrt(0) #=> 0
Integer.sqrt(1) #=> 1
Integer.sqrt(24) #=> 4
Integer.sqrt(25) #=> 5
Integer.sqrt(10**400) #=> 10**200
Equivalent to Math.sqrt(n).floor
, except that the result of the latter code may differ from the true value due to the limited precision of floating point arithmetic.
Integer.sqrt(10**46) #=> 100000000000000000000000
Math.sqrt(10**46).floor #=> 99999999999999991611392 (!)
If n
is not an Integer, it is converted to an Integer first. If n
is negative, a Math::DomainError is raised.
5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 |
# File 'numeric.c', line 5429 static VALUE rb_int_s_isqrt(VALUE self, VALUE num) { unsigned long n, sq; num = rb_to_int(num); if (FIXNUM_P(num)) { if (FIXNUM_NEGATIVE_P(num)) { domain_error("isqrt"); } n = FIX2ULONG(num); sq = rb_ulong_isqrt(n); return LONG2FIX(sq); } else { size_t biglen; if (RBIGNUM_NEGATIVE_P(num)) { domain_error("isqrt"); } biglen = BIGNUM_LEN(num); if (biglen == 0) return INT2FIX(0); #if SIZEOF_BDIGIT <= SIZEOF_LONG /* short-circuit */ if (biglen == 1) { n = BIGNUM_DIGITS(num)[0]; sq = rb_ulong_isqrt(n); return ULONG2NUM(sq); } #endif return rb_big_isqrt(num); } } |
Instance Method Details
#%(other) ⇒ Object #modulo(other) ⇒ Object
Returns int
modulo other
.
See Numeric#divmod for more information.
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 |
# File 'numeric.c', line 3885 VALUE rb_int_modulo(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_mod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_modulo(x, y); } return num_modulo(x, y); } |
#&(other_int) ⇒ Integer
Bitwise AND.
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 |
# File 'numeric.c', line 4466 VALUE rb_int_and(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_and(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_and(x, y); } return Qnil; } |
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of numeric
.
3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 |
# File 'numeric.c', line 3698 VALUE rb_int_mul(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_mul(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_mul(x, y); } return rb_num_coerce_bin(x, y, '*'); } |
#**(numeric) ⇒ Object
Raises int
to the power of numeric
, which may be negative or fractional. The result may be an Integer, a Float, a Rational, or a complex number.
2 ** 3 #=> 8
2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623730951
(-1) ** 0.5 #=> (0.0+1.0i)
123456789 ** 2 #=> 15241578750190521
123456789 ** 1.2 #=> 5126464716.0993185
123456789 ** -2 #=> (1/15241578750190521)
4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 |
# File 'numeric.c', line 4105 VALUE rb_int_pow(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_pow(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_pow(x, y); } return Qnil; } |
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of numeric
.
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 |
# File 'numeric.c', line 3609 VALUE rb_int_plus(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_plus(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_plus(x, y); } return rb_num_coerce_bin(x, y, '+'); } |
#-(numeric) ⇒ Object
Performs subtraction: the class of the resulting object depends on the class of numeric
.
3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 |
# File 'numeric.c', line 3648 VALUE rb_int_minus(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_minus(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_minus(x, y); } return rb_num_coerce_bin(x, y, '-'); } |
#- ⇒ Integer
Returns int
, negated.
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 |
# File 'numeric.c', line 3478 VALUE rb_int_uminus(VALUE num) { if (FIXNUM_P(num)) { return fix_uminus(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_uminus(num); } return num_funcall0(num, idUMinus); } |
#/(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of numeric
.
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 |
# File 'numeric.c', line 3815 VALUE rb_int_div(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_div(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_div(x, y); } return Qnil; } |
#<(real) ⇒ Boolean
Returns true
if the value of int
is less than that of real
.
4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 |
# File 'numeric.c', line 4329 static VALUE int_lt(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_lt(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_lt(x, y); } return Qnil; } |
#<<(count) ⇒ Integer
Returns int
shifted left count
positions, or right if count
is negative.
4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 |
# File 'numeric.c', line 4582 VALUE rb_int_lshift(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return rb_fix_lshift(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_lshift(x, y); } return Qnil; } |
#<=(real) ⇒ Boolean
Returns true
if the value of int
is less than or equal to that of real
.
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 |
# File 'numeric.c', line 4369 static VALUE int_le(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_le(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_le(x, y); } return Qnil; } |
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1, 0, or +1 depending on whether int
is less than, equal to, or greater than numeric
.
This is the basis for the tests in the Comparable module.
nil
is returned if the two values are incomparable.
4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 |
# File 'numeric.c', line 4211 VALUE rb_int_cmp(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_cmp(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_cmp(x, y); } else { rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x)); } } |
#==(other) ⇒ Boolean
Returns true
if int
equals other
numerically. Contrast this with Integer#eql?, which requires other
to be an Integer.
1 == 2 #=> false
1 == 1.0 #=> true
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 |
# File 'numeric.c', line 4162 VALUE rb_int_equal(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_equal(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_eq(x, y); } return Qnil; } |
#==(other) ⇒ Boolean
Returns true
if int
equals other
numerically. Contrast this with Integer#eql?, which requires other
to be an Integer.
1 == 2 #=> false
1 == 1.0 #=> true
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 |
# File 'numeric.c', line 4162 VALUE rb_int_equal(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_equal(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_eq(x, y); } return Qnil; } |
#>(real) ⇒ Boolean
Returns true
if the value of int
is greater than that of real
.
4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 |
# File 'numeric.c', line 4251 VALUE rb_int_gt(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_gt(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_gt(x, y); } return Qnil; } |
#>=(real) ⇒ Boolean
Returns true
if the value of int
is greater than or equal to that of real
.
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 |
# File 'numeric.c', line 4291 VALUE rb_int_ge(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_ge(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_ge(x, y); } return Qnil; } |
#>>(count) ⇒ Integer
Returns int
shifted right count
positions, or left if count
is negative.
4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 |
# File 'numeric.c', line 4629 static VALUE rb_int_rshift(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return rb_fix_rshift(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_rshift(x, y); } return Qnil; } |
#[](n) ⇒ 0, 1 #[](n, m) ⇒ Numeric #[](range) ⇒ Numeric
Bit Reference—Returns the n
th bit in the binary representation of int
, where int[0]
is the least significant bit.
a = 0b11001100101010
30.downto(0) {|n| print a[n] }
#=> 0000000000000000011001100101010
a = 9**15
50.downto(0) {|n| print a[n] }
#=> 000101110110100000111000011110010100111100010111001
In principle, n[i]
is equivalent to (n >> i) & 1
. Thus, any negative index always returns zero:
p 255[-1] #=> 0
Range operations n[i, len]
and n[i..j]
are naturally extended.
-
n[i, len]
equals to(n >> i) & ((1 << len) - 1)
. -
n[i..j]
equals to(n >> i) & ((1 << (j - i + 1)) - 1)
. -
n[i...j]
equals to(n >> i) & ((1 << (j - i)) - 1)
. -
n[i..]
equals to(n >> i)
. -
n[..j]
is zero ifn & ((1 << (j + 1)) - 1)
is zero. Otherwise, raises an ArgumentError. -
n[...j]
is zero ifn & ((1 << j) - 1)
is zero. Otherwise, raises an ArgumentError.
Note that range operation may exhaust memory. For example, -1[0, 1000000000000]
will raise NoMemoryError.
4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 |
# File 'numeric.c', line 4789 static VALUE int_aref(int const argc, VALUE * const argv, VALUE const num) { rb_check_arity(argc, 1, 2); if (argc == 2) { return int_aref2(num, argv[0], argv[1]); } return int_aref1(num, argv[0]); return Qnil; } |
#^(other_int) ⇒ Integer
Bitwise EXCLUSIVE OR.
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 |
# File 'numeric.c', line 4536 static VALUE int_xor(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_xor(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_xor(x, y); } return Qnil; } |
#abs ⇒ Object
4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 |
# File 'numeric.c', line 4854 VALUE rb_int_abs(VALUE num) { if (FIXNUM_P(num)) { return fix_abs(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_abs(num); } return Qnil; } |
#allbits?(mask) ⇒ Boolean
Returns true
if all bits of int & mask
are 1.
3269 3270 3271 3272 3273 3274 |
# File 'numeric.c', line 3269 static VALUE int_allbits_p(VALUE num, VALUE mask) { mask = rb_to_int(mask); return rb_int_equal(rb_int_and(num, mask), mask); } |
#anybits?(mask) ⇒ Boolean
Returns true
if any bits of int & mask
are 1.
3283 3284 3285 3286 3287 3288 |
# File 'numeric.c', line 3283 static VALUE int_anybits_p(VALUE num, VALUE mask) { mask = rb_to_int(mask); return num_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue; } |
#bit_length ⇒ Integer
Returns the number of bits of the value of int
.
“Number of bits” means the bit position of the highest bit which is different from the sign bit (where the least significant bit has bit position 1). If there is no such bit (zero or minus one), zero is returned.
I.e. this method returns ceil(log2(int < 0 ? -int : int+1)).
(-2**1000-1).bit_length #=> 1001
(-2**1000).bit_length #=> 1000
(-2**1000+1).bit_length #=> 1000
(-2**12-1).bit_length #=> 13
(-2**12).bit_length #=> 12
(-2**12+1).bit_length #=> 12
-0x101.bit_length #=> 9
-0x100.bit_length #=> 8
-0xff.bit_length #=> 8
-2.bit_length #=> 1
-1.bit_length #=> 0
0.bit_length #=> 0
1.bit_length #=> 1
0xff.bit_length #=> 8
0x100.bit_length #=> 9
(2**12-1).bit_length #=> 12
(2**12).bit_length #=> 13
(2**12+1).bit_length #=> 13
(2**1000-1).bit_length #=> 1000
(2**1000).bit_length #=> 1001
(2**1000+1).bit_length #=> 1001
This method can be used to detect overflow in Array#pack as follows:
if n.bit_length < 32
[n].pack("l") # no overflow
else
raise "overflow"
end
4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 |
# File 'numeric.c', line 4954 static VALUE rb_int_bit_length(VALUE num) { if (FIXNUM_P(num)) { return rb_fix_bit_length(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_bit_length(num); } return Qnil; } |
#ceil([ndigits]) ⇒ Integer, Float
Returns the smallest number greater than or equal to int
with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.ceil #=> 1
1.ceil(2) #=> 1
18.ceil(-1) #=> 20
(-18).ceil(-1) #=> -10
5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 |
# File 'numeric.c', line 5315 static VALUE int_ceil(int argc, VALUE* argv, VALUE num) { int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_ceil(num, ndigits); } |
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the int
‘s value according to encoding
.
65.chr #=> "A"
230.chr #=> "\xE6"
255.chr(Encoding::UTF_8) #=> "\u00FF"
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 |
# File 'numeric.c', line 3396 static VALUE int_chr(int argc, VALUE *argv, VALUE num) { char c; unsigned int i; rb_encoding *enc; if (rb_num_to_uint(num, &i) == 0) { } else if (FIXNUM_P(num)) { rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num)); } else { rb_raise(rb_eRangeError, "bignum out of char range"); } switch (argc) { case 0: if (0xff < i) { enc = rb_default_internal_encoding(); if (!enc) { rb_raise(rb_eRangeError, "%d out of char range", i); } goto decode; } c = (char)i; if (i < 0x80) { return rb_usascii_str_new(&c, 1); } else { return rb_str_new(&c, 1); } case 1: break; default: rb_error_arity(argc, 0, 1); } enc = rb_to_encoding(argv[0]); if (!enc) enc = rb_ascii8bit_encoding(); decode: return rb_enc_uint_chr(i, enc); } |
#coerce(numeric) ⇒ Array
Returns an array with both a numeric
and a big
represented as Bignum objects.
This is achieved by converting numeric
to a Bignum.
A TypeError is raised if the numeric
is not a Fixnum or Bignum type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 |
# File 'bignum.c', line 6748 static VALUE rb_int_coerce(VALUE x, VALUE y) { if (RB_INTEGER_TYPE_P(y)) { return rb_assoc_new(y, x); } else { x = rb_Float(x); y = rb_Float(y); return rb_assoc_new(y, x); } } |
#denominator ⇒ 1
Returns 1.
2061 2062 2063 2064 2065 |
# File 'rational.c', line 2061 static VALUE integer_denominator(VALUE self) { return INT2FIX(1); } |
#digits ⇒ Array #digits(base) ⇒ Array
Returns the digits of int
‘s place-value representation with radix base
(default: 10). The digits are returned as an array with the least significant digit as the first array element.
base
must be greater than or equal to 2.
12345.digits #=> [5, 4, 3, 2, 1]
12345.digits(7) #=> [4, 6, 6, 0, 5]
12345.digits(100) #=> [45, 23, 1]
-12345.digits(7) #=> Math::DomainError
5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 |
# File 'numeric.c', line 5041 static VALUE rb_int_digits(int argc, VALUE *argv, VALUE num) { VALUE base_value; long base; if (rb_num_negative_p(num)) rb_raise(rb_eMathDomainError, "out of domain"); if (rb_check_arity(argc, 0, 1)) { base_value = rb_to_int(argv[0]); if (!RB_INTEGER_TYPE_P(base_value)) rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(argv[0])); if (RB_TYPE_P(base_value, T_BIGNUM)) return rb_int_digits_bigbase(num, base_value); base = FIX2LONG(base_value); if (base < 0) rb_raise(rb_eArgError, "negative radix"); else if (base < 2) rb_raise(rb_eArgError, "invalid radix %ld", base); } else base = 10; if (FIXNUM_P(num)) return rb_fix_digits(num, base); else if (RB_TYPE_P(num, T_BIGNUM)) return rb_int_digits_bigbase(num, LONG2FIX(base)); return Qnil; } |
#div(numeric) ⇒ Integer
Performs integer division: returns the integer result of dividing int
by numeric
.
3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 |
# File 'numeric.c', line 3842 VALUE rb_int_idiv(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_idiv(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_idiv(x, y); } return num_div(x, y); } |
#divmod(numeric) ⇒ Array
See Numeric#divmod.
3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 |
# File 'numeric.c', line 3962 VALUE rb_int_divmod(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_divmod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_divmod(x, y); } return Qnil; } |
#downto(limit) {|i| ... } ⇒ self #downto(limit) ⇒ Object
Iterates the given block, passing in decreasing values from int
down to and including limit
.
If no block is given, an Enumerator is returned instead.
5.downto(1) { |n| print n, ".. " }
puts "Liftoff!"
#=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 |
# File 'numeric.c', line 5141 static VALUE int_downto(VALUE from, VALUE to) { RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i=FIX2LONG(from); i >= end; i--) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '<', 1, to))) { rb_yield(i); i = rb_funcall(i, '-', 1, INT2FIX(1)); } if (NIL_P(c)) rb_cmperr(i, to); } return from; } |
#even? ⇒ Boolean
Returns true
if int
is an even number.
3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 |
# File 'numeric.c', line 3245 static VALUE int_even_p(VALUE num) { if (FIXNUM_P(num)) { if ((num & 2) == 0) { return Qtrue; } } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_even_p(num); } else if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) { return Qtrue; } return Qfalse; } |
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing int
by numeric
.
654321.fdiv(13731) #=> 47.652829364212366
654321.fdiv(13731.24) #=> 47.65199646936475
-654321.fdiv(13731) #=> -47.652829364212366
3760 3761 3762 3763 3764 3765 3766 3767 |
# File 'numeric.c', line 3760 VALUE rb_int_fdiv(VALUE x, VALUE y) { if (RB_INTEGER_TYPE_P(x)) { return DBL2NUM(rb_int_fdiv_double(x, y)); } return Qnil; } |
#floor([ndigits]) ⇒ Integer, Float
Returns the largest number less than or equal to int
with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.floor #=> 1
1.floor(2) #=> 1
18.floor(-1) #=> 10
(-18).floor(-1) #=> -20
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 |
# File 'numeric.c', line 5283 static VALUE int_floor(int argc, VALUE* argv, VALUE num) { int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_floor(num, ndigits); } |
#gcd(other_int) ⇒ Integer
Returns the greatest common divisor of the two integers. The result is always positive. 0.gcd(x) and x.gcd(0) return x.abs.
36.gcd(60) #=> 12
2.gcd(2) #=> 2
3.gcd(-7) #=> 1
((1<<31)-1).gcd((1<<61)-1) #=> 1
1893 1894 1895 1896 1897 1898 |
# File 'rational.c', line 1893 VALUE rb_gcd(VALUE self, VALUE other) { other = nurat_int_value(other); return f_gcd(self, other); } |
#gcdlcm(other_int) ⇒ Array
Returns an array with the greatest common divisor and the least common multiple of the two integers, [gcd, lcm].
36.gcdlcm(60) #=> [12, 180]
2.gcdlcm(2) #=> [2, 2]
3.gcdlcm(-7) #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1) #=> [1, 4951760154835678088235319297]
1931 1932 1933 1934 1935 1936 |
# File 'rational.c', line 1931 VALUE rb_gcdlcm(VALUE self, VALUE other) { other = nurat_int_value(other); return rb_assoc_new(f_gcd(self, other), f_lcm(self, other)); } |
#integer? ⇒ true
Since int
is already an Integer, this always returns true
.
3208 3209 3210 3211 3212 |
# File 'numeric.c', line 3208 static VALUE int_int_p(VALUE num) { return Qtrue; } |
#lcm(other_int) ⇒ Integer
Returns the least common multiple of the two integers. The result is always positive. 0.lcm(x) and x.lcm(0) return zero.
36.lcm(60) #=> 180
2.lcm(2) #=> 2
3.lcm(-7) #=> 21
((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
1912 1913 1914 1915 1916 1917 |
# File 'rational.c', line 1912 VALUE rb_lcm(VALUE self, VALUE other) { other = nurat_int_value(other); return f_lcm(self, other); } |
#abs ⇒ Integer #magnitude ⇒ Integer
Returns the absolute value of int
.
(-12345).abs #=> 12345
-12345.abs #=> 12345
12345.abs #=> 12345
Integer#magnitude is an alias for Integer#abs.
4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 |
# File 'numeric.c', line 4854 VALUE rb_int_abs(VALUE num) { if (FIXNUM_P(num)) { return fix_abs(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_abs(num); } return Qnil; } |
#%(other) ⇒ Object #modulo(other) ⇒ Object
Returns int
modulo other
.
See Numeric#divmod for more information.
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 |
# File 'numeric.c', line 3885 VALUE rb_int_modulo(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_mod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_modulo(x, y); } return num_modulo(x, y); } |
#next ⇒ Integer #succ ⇒ Integer
Returns the successor of int
, i.e. the Integer equal to int+1
.
1.next #=> 2
(-1).next #=> 0
1.succ #=> 2
(-1).succ #=> 0
#nobits?(mask) ⇒ Boolean
Returns true
if no bits of int & mask
are 1.
3297 3298 3299 3300 3301 3302 |
# File 'numeric.c', line 3297 static VALUE int_nobits_p(VALUE num, VALUE mask) { mask = rb_to_int(mask); return num_zero_p(rb_int_and(num, mask)); } |
#numerator ⇒ self
Returns self.
2049 2050 2051 2052 2053 |
# File 'rational.c', line 2049 static VALUE integer_numerator(VALUE self) { return self; } |
#odd? ⇒ Boolean
Returns true
if int
is an odd number.
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 |
# File 'numeric.c', line 3221 VALUE rb_int_odd_p(VALUE num) { if (FIXNUM_P(num)) { if (num & 2) { return Qtrue; } } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_odd_p(num); } else if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) { return Qtrue; } return Qfalse; } |
#ord ⇒ self
Returns the int
itself.
97.ord #=> 97
This method is intended for compatibility to character literals in Ruby 1.9.
For example, ?a.ord
returns 97 both in 1.8 and 1.9.
3453 3454 3455 3456 3457 |
# File 'numeric.c', line 3453 static VALUE int_ord(VALUE num) { return num; } |
#pow(numeric) ⇒ Numeric #pow(integer, integer) ⇒ Integer
Returns (modular) exponentiation as:
a.pow(b) #=> same as a**b
a.pow(b, m) #=> same as (a**b) % m, but avoids huge temporary values
7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 |
# File 'bignum.c', line 7111 VALUE rb_int_powm(int const argc, VALUE * const argv, VALUE const num) { rb_check_arity(argc, 1, 2); if (argc == 1) { return rb_int_pow(num, argv[0]); } else { VALUE const a = num; VALUE const b = argv[0]; VALUE m = argv[1]; int nega_flg = 0; if ( ! RB_INTEGER_TYPE_P(b)) { rb_raise(rb_eTypeError, "Integer#pow() 2nd argument not allowed unless a 1st argument is integer"); } if (rb_int_negative_p(b)) { rb_raise(rb_eRangeError, "Integer#pow() 1st argument cannot be negative when 2nd argument specified"); } if (!RB_INTEGER_TYPE_P(m)) { rb_raise(rb_eTypeError, "Integer#pow() 2nd argument not allowed unless all arguments are integers"); } if (rb_int_negative_p(m)) { m = rb_int_uminus(m); nega_flg = 1; } if (FIXNUM_P(m)) { long const half_val = (long)HALF_LONG_MSB; long const mm = FIX2LONG(m); if (!mm) rb_num_zerodiv(); if (mm <= half_val) { return int_pow_tmp1(rb_int_modulo(a, m), b, mm, nega_flg); } else { return int_pow_tmp2(rb_int_modulo(a, m), b, mm, nega_flg); } } else { if (rb_bigzero_p(m)) rb_num_zerodiv(); return int_pow_tmp3(rb_int_modulo(a, m), b, m, nega_flg); } } UNREACHABLE_RETURN(Qnil); } |
#pred ⇒ Object
#rationalize([eps]) ⇒ Object
Returns the value as a rational. The optional argument eps
is always ignored.
2165 2166 2167 2168 2169 2170 |
# File 'rational.c', line 2165 static VALUE integer_rationalize(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 1); return integer_to_r(self); } |
#remainder(numeric) ⇒ Object
Returns the remainder after dividing int
by numeric
.
x.remainder(y)
means x-y*(x/y).truncate
.
5.remainder(3) #=> 2
-5.remainder(3) #=> -2
5.remainder(-3) #=> 2
-5.remainder(-3) #=> -2
5.remainder(1.5) #=> 0.5
See Numeric#divmod.
3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 |
# File 'numeric.c', line 3914 static VALUE int_remainder(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return num_remainder(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_remainder(x, y); } return Qnil; } |
#round([ndigits][, half: mode]) ⇒ Integer, Float
Returns int
rounded to the nearest value with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.round #=> 1
1.round(2) #=> 1
15.round(-1) #=> 20
(-15).round(-1) #=> -20
The optional half
keyword argument is available similar to Float#round.
25.round(-1, half: :up) #=> 30
25.round(-1, half: :down) #=> 20
25.round(-1, half: :even) #=> 20
35.round(-1, half: :up) #=> 40
35.round(-1, half: :down) #=> 30
35.round(-1, half: :even) #=> 40
(-25).round(-1, half: :up) #=> -30
(-25).round(-1, half: :down) #=> -20
(-25).round(-1, half: :even) #=> -20
5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 |
# File 'numeric.c', line 5248 static VALUE int_round(int argc, VALUE* argv, VALUE num) { int ndigits; int mode; VALUE nd, opt; if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num; ndigits = NUM2INT(nd); mode = rb_num_get_rounding_option(opt); if (ndigits >= 0) { return num; } return rb_int_round(num, ndigits, mode); } |
#size ⇒ Integer
Returns the number of bytes in the machine representation of int
(machine dependent).
1.size #=> 8
-1.size #=> 8
2147483647.size #=> 8
(256**10 - 1).size #=> 10
(256**20 - 1).size #=> 20
(256**40 - 1).size #=> 40
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 |
# File 'numeric.c', line 4888 static VALUE int_size(VALUE num) { if (FIXNUM_P(num)) { return fix_size(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_size_m(num); } return Qnil; } |
#next ⇒ Integer #succ ⇒ Integer
Returns the successor of int
, i.e. the Integer equal to int+1
.
1.next #=> 2
(-1).next #=> 0
1.succ #=> 2
(-1).succ #=> 0
#times {|i| ... } ⇒ self #times ⇒ Object
Iterates the given block int
times, passing in values from zero to int - 1
.
If no block is given, an Enumerator is returned instead.
5.times {|i| print i, " " } #=> 0 1 2 3 4
5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 |
# File 'numeric.c', line 5191 static VALUE int_dotimes(VALUE num) { RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size); if (FIXNUM_P(num)) { long i, end; end = FIX2LONG(num); for (i=0; i<end; i++) { rb_yield_1(LONG2FIX(i)); } } else { VALUE i = INT2FIX(0); for (;;) { if (!RTEST(rb_funcall(i, '<', 1, num))) break; rb_yield(i); i = rb_funcall(i, '+', 1, INT2FIX(1)); } } return num; } |
#to_f ⇒ Float
Converts int
to a Float. If int
doesn’t fit in a Float, the result is infinity.
4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 |
# File 'numeric.c', line 4810 static VALUE int_to_f(VALUE num) { double val; if (FIXNUM_P(num)) { val = (double)FIX2LONG(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { val = rb_big2dbl(num); } else { rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num)); } return DBL2NUM(val); } |
#to_i ⇒ Integer #to_int ⇒ Integer
Since int
is already an Integer, returns self
.
#to_int is an alias for #to_i.
3195 3196 3197 3198 3199 |
# File 'numeric.c', line 3195 static VALUE int_to_i(VALUE num) { return num; } |
#to_i ⇒ Integer #to_int ⇒ Integer
Since int
is already an Integer, returns self
.
#to_int is an alias for #to_i.
3195 3196 3197 3198 3199 |
# File 'numeric.c', line 3195 static VALUE int_to_i(VALUE num) { return num; } |
#to_r ⇒ Object
Returns the value as a rational.
1.to_r #=> (1/1)
(1<<64).to_r #=> (18446744073709551616/1)
2152 2153 2154 2155 2156 |
# File 'rational.c', line 2152 static VALUE integer_to_r(VALUE self) { return rb_rational_new1(self); } |
#to_s(base = 10) ⇒ String Also known as: inspect
Returns a string containing the place-value representation of int
with 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"
78546939656932.to_s(36) #=> "rubyrules"
3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 |
# File 'numeric.c', line 3549 static VALUE int_to_s(int argc, VALUE *argv, VALUE x) { int base; if (rb_check_arity(argc, 0, 1)) base = NUM2INT(argv[0]); else base = 10; return rb_int2str(x, base); } |
#truncate([ndigits]) ⇒ Integer, Float
Returns int
truncated (toward zero) to a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.truncate #=> 1
1.truncate(2) #=> 1
18.truncate(-1) #=> 10
(-18).truncate(-1) #=> -10
5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 |
# File 'numeric.c', line 5347 static VALUE int_truncate(int argc, VALUE* argv, VALUE num) { int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_truncate(num, ndigits); } |
#upto(limit) {|i| ... } ⇒ self #upto(limit) ⇒ Object
Iterates the given block, passing in integer values from int
up to and including limit
.
If no block is given, an Enumerator is returned instead.
5.upto(10) {|i| print i, " " } #=> 5 6 7 8 9 10
5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 |
# File 'numeric.c', line 5095 static VALUE int_upto(VALUE from, VALUE to) { RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i = FIX2LONG(from); i <= end; i++) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '>', 1, to))) { rb_yield(i); i = rb_funcall(i, '+', 1, INT2FIX(1)); } if (NIL_P(c)) rb_cmperr(i, to); } return from; } |
#|(other_int) ⇒ Integer
Bitwise OR.
4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 |
# File 'numeric.c', line 4501 static VALUE int_or(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_or(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_or(x, y); } return Qnil; } |
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
Inverts the bits in an Integer. As integers are conceptually of infinite length, the result acts as if it had an infinite number of one bits to the left. In hex representations, this is displayed as two periods to the left of the digits.
sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 |
# File 'numeric.c', line 4402 static VALUE int_comp(VALUE num) { if (FIXNUM_P(num)) { return fix_comp(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_comp(num); } return Qnil; } |