Class: Integer

Inherits:
Numeric show all
Defined in:
numeric.c,
numeric.c

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

Instance Method Summary collapse

Methods inherited from Numeric

#+@, #-@, #abs, #abs2, #angle, #arg, #clone, #conj, #conjugate, #dup, #eql?, #finite?, #i, #imag, #imaginary, #infinite?, #integer?, #magnitude, #negative?, #nonzero?, #phase, #polar, #positive?, #quo, #real, #real?, #rect, #rectangular, #singleton_method_added, #step, #to_c, #to_int, #zero?

Methods included from Comparable

#between?, #clamp

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.

Returns:



5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
# File 'numeric.c', line 5353

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.



3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
# File 'numeric.c', line 3872

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.

Returns:



4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
# File 'numeric.c', line 4435

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.



3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
# File 'numeric.c', line 3685

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)


4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
# File 'numeric.c', line 4087

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.



3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
# File 'numeric.c', line 3596

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.



3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
# File 'numeric.c', line 3635

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, '-');
}

#/(numeric) ⇒ Object

Performs division: the class of the resulting object depends on the class of numeric.



3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
# File 'numeric.c', line 3802

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.

Returns:

  • (Boolean)


4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
# File 'numeric.c', line 4313

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.

Returns:



4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
# File 'numeric.c', line 4551

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.

Returns:

  • (Boolean)


4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
# File 'numeric.c', line 4353

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.

Returns:

  • (-1, 0, +1, nil)


4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
# File 'numeric.c', line 4195

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

Returns:

  • (Boolean)


4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
# File 'numeric.c', line 4146

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;
}

#===(y) ⇒ Object



4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
# File 'numeric.c', line 4146

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.

Returns:

  • (Boolean)


4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
# File 'numeric.c', line 4235

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.

Returns:

  • (Boolean)


4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
# File 'numeric.c', line 4275

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.

Returns:



4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
# File 'numeric.c', line 4598

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 nth 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 if n & ((1 << (j + 1)) - 1) is zero. Otherwise, raises an ArgumentError.

  • n[...j] is zero if n & ((1 << j) - 1) is zero. Otherwise, raises an ArgumentError.

Note that range operation may exhaust memory. For example, -1[0, 1000000000000] will raise NoMemoryError.

Overloads:



4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
# File 'numeric.c', line 4758

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.

Returns:



4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
# File 'numeric.c', line 4505

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;
}

#allbits?(mask) ⇒ Boolean

Returns true if all bits of int & mask are 1.

Returns:

  • (Boolean)


3285
3286
3287
3288
3289
3290
# File 'numeric.c', line 3285

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.

Returns:

  • (Boolean)


3299
3300
3301
3302
3303
3304
# File 'numeric.c', line 3299

static VALUE
int_anybits_p(VALUE num, VALUE mask)
{
    mask = rb_to_int(mask);
    return int_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue;
}

#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

Returns:



5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
# File 'numeric.c', line 5239

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"

Returns:



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
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
# File 'numeric.c', line 3412

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, "%u 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]

Returns:



6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
# File 'bignum.c', line 6745

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);
    }
}

#denominator1

Returns 1.

Returns:

  • (1)


2059
2060
2061
2062
2063
# File 'rational.c', line 2059

static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}

#digitsArray #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

Overloads:



4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
# File 'numeric.c', line 4965

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.

Returns:



3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
# File 'numeric.c', line 3829

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.

Returns:



3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
# File 'numeric.c', line 3949

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!"

Overloads:

  • #downto(limit) {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
# File 'numeric.c', line 5065

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;
}

#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

Returns:



3747
3748
3749
3750
3751
3752
3753
3754
# File 'numeric.c', line 3747

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

Returns:



5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
# File 'numeric.c', line 5207

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

Returns:



1888
1889
1890
1891
1892
1893
# File 'rational.c', line 1888

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]

Returns:



1926
1927
1928
1929
1930
1931
# File 'rational.c', line 1926

VALUE
rb_gcdlcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
}

#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

Returns:



1907
1908
1909
1910
1911
1912
# File 'rational.c', line 1907

VALUE
rb_lcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return f_lcm(self, other);
}

#%(other) ⇒ Object #modulo(other) ⇒ Object

Returns int modulo other.

See Numeric#divmod for more information.



3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
# File 'numeric.c', line 3872

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);
}

#nextInteger #succInteger

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

Overloads:

#nobits?(mask) ⇒ Boolean

Returns true if no bits of int & mask are 1.

Returns:

  • (Boolean)


3313
3314
3315
3316
3317
3318
# File 'numeric.c', line 3313

static VALUE
int_nobits_p(VALUE num, VALUE mask)
{
    mask = rb_to_int(mask);
    return int_zero_p(rb_int_and(num, mask));
}

#numeratorself

Returns self.

Returns:

  • (self)


2047
2048
2049
2050
2051
# File 'rational.c', line 2047

static VALUE
integer_numerator(VALUE self)
{
    return self;
}

#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

Overloads:



7107
7108
7109
7110
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
# File 'bignum.c', line 7107

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 == 1) return INT2FIX(0);
            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();
	    if (bignorm(m) == INT2FIX(1)) return INT2FIX(0);
            return int_pow_tmp3(rb_int_modulo(a, m), b, m, nega_flg);
        }
    }
    UNREACHABLE_RETURN(Qnil);
}

#predObject

#rationalize([eps]) ⇒ Object

Returns the value as a rational. The optional argument eps is always ignored.



2157
2158
2159
2160
2161
2162
# File 'rational.c', line 2157

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.



3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
# File 'numeric.c', line 3901

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

Returns:



5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
# File 'numeric.c', line 5172

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);
}

#sizeInteger

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

Returns:



4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
# File 'numeric.c', line 4857

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;
}

#nextInteger #succInteger

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

Overloads:

#times {|i| ... } ⇒ self #timesObject

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

Overloads:

  • #times {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
# File 'numeric.c', line 5115

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_fFloat

Converts int to a Float. If int doesn’t fit in a Float, the result is infinity.

Returns:



4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
# File 'numeric.c', line 4779

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_rObject

Returns the value as a rational.

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)


2144
2145
2146
2147
2148
# File 'rational.c', line 2144

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"

Returns:



3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
# File 'numeric.c', line 3536

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

Returns:



5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
# File 'numeric.c', line 5271

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

Overloads:

  • #upto(limit) {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
# File 'numeric.c', line 5019

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));
	}
	ensure_cmp(c, i, to);
    }
    return from;
}

#|(other_int) ⇒ Integer

Bitwise OR.

Returns:



4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
# File 'numeric.c', line 4470

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;
}