class Integer
Integer 对象表示一个整数值。
您可以使用以下方式显式创建 Integer 对象:
-
一个 整数字面量。
您可以使用以下方式将某些对象转换为整数:
-
方法
Integer。
尝试向此类实例添加单例方法会导致引发异常。
此处内容¶ ↑
首先,看看别处的内容。Integer 类
-
包含 Comparable 模块。
在此,Integer 类提供了以下方法:
查询¶ ↑
比较¶ ↑
-
<:返回self是否小于给定值。 -
<=:返回self是否小于或等于给定值。 -
<=>:返回一个数字,指示self是小于、等于还是大于给定值。 -
value.
-
>:返回self是否大于给定值。 -
>=:返回self是否大于或等于给定值。
转换¶ ↑
-
::sqrt:返回给定值的整数平方根。 -
::try_convert:返回转换为 Integer 的给定值。 -
&:返回self和给定值的按位与。 -
*:返回self和给定值的乘积。 -
**:返回self的给定值次幂。 -
+:返回self和给定值的和。 -
-:返回self和给定值的差。 -
/:返回self和给定值的商。 -
<<:返回self左移位后的值。 -
>>:返回self右移位后的值。 -
[]:返回self的位切片。 -
^:返回self和给定值的按位异或。 -
|:返回self和给定值的按位或。 -
ceil:返回大于或等于self的最小整数。 -
chr:返回一个包含由self值表示的字符的 1 字符字符串。 -
digits:返回一个整数数组,表示self的基数位。 -
div:返回self除以给定值的整数结果。 -
divmod:返回一个包含self除以给定值的商和余数结果的 2 元素数组。 -
floor:返回小于或等于self的最大整数。 -
pow:返回self的模幂。 -
pred:返回self的整数前驱。 -
remainder:返回self除以给定值后的余数。 -
round:返回self四舍五入到具有给定精度的最接近值。 -
truncate:返回截断为给定精度的self。
其他¶ ↑
常量
- GMP_VERSION
-
加载的 GMP 版本。
公共类方法
源代码
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);
}
}
返回非负整数 n 的整数平方根,它是小于或等于 numeric 平方根的最大非负整数。
Integer.sqrt(0) # => 0 Integer.sqrt(1) # => 1 Integer.sqrt(24) # => 4 Integer.sqrt(25) # => 5 Integer.sqrt(10**400) # => 10**200
如果 numeric 不是整数,则将其转换为整数
Integer.sqrt(Complex(4, 0)) # => 2 Integer.sqrt(Rational(4, 1)) # => 2 Integer.sqrt(4.0) # => 2 Integer.sqrt(3.14159) # => 1
此方法等效于 Math.sqrt(numeric).floor,但由于浮点运算的精度有限,后者的结果可能与真实值不同。
Integer.sqrt(10**46) # => 100000000000000000000000 Math.sqrt(10**46).floor # => 99999999999999991611392
如果 numeric 为负数,则引发异常。
源代码
static VALUE
int_s_try_convert(VALUE self, VALUE num)
{
return rb_check_integer_type(num);
}
如果 object 是一个 Integer 对象,则返回 object。
Integer.try_convert(1) # => 1
否则,如果 object 响应 :to_int,则调用 object.to_int 并返回结果。
Integer.try_convert(1.25) # => 1
如果 object 不响应 :to_int,则返回 nil
Integer.try_convert([]) # => nil
除非 object.to_int 返回 Integer 对象,否则引发异常。
公共实例方法
源代码
VALUE
rb_int_modulo(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_mod(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_modulo(x, y);
}
return num_modulo(x, y);
}
返回作为实数的 self 对 other 的模。
对于整数 n 和实数 r,以下表达式等效
n % r n-r*(n/r).floor n.divmod(r)[1]
请参见 Numeric#divmod。
示例
10 % 2 # => 0 10 % 3 # => 1 10 % 4 # => 2 10 % -2 # => 0 10 % -3 # => -2 10 % -4 # => -2 10 % 3.0 # => 1.0 10 % Rational(3, 1) # => (1/1)
源代码
源代码
VALUE
rb_int_mul(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_mul(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_mul(x, y);
}
return rb_num_coerce_bin(x, y, '*');
}
执行乘法
4 * 2 # => 8 4 * -2 # => -8 -4 * 2 # => -8 4 * 2.0 # => 8.0 4 * Rational(1, 3) # => (4/3) 4 * Complex(2, 0) # => (8+0i)
源代码
VALUE
rb_int_pow(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_pow(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_pow(x, y);
}
return Qnil;
}
将 self 提升到 numeric 的幂
2 ** 3 # => 8 2 ** -3 # => (1/8) -2 ** 3 # => -8 -2 ** -3 # => (-1/8) 2 ** 3.3 # => 9.849155306759329 2 ** Rational(3, 1) # => (8/1) 2 ** Complex(3, 0) # => (8+0i)
源代码
VALUE
rb_int_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_plus(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_plus(x, y);
}
return rb_num_coerce_bin(x, y, '+');
}
执行加法
2 + 2 # => 4 -2 + 2 # => 0 -2 + -2 # => -4 2 + 2.0 # => 4.0 2 + Rational(2, 1) # => (4/1) 2 + Complex(2, 0) # => (4+0i)
源代码
VALUE
rb_int_minus(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_minus(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_minus(x, y);
}
return rb_num_coerce_bin(x, y, '-');
}
执行减法
4 - 2 # => 2 -4 - 2 # => -6 -4 - -2 # => -2 4 - 2.0 # => 2.0 4 - Rational(2, 1) # => (2/1) 4 - Complex(2, 0) # => (2+0i)
源代码
# File numeric.rb, line 99 def -@ Primitive.attr! :leaf Primitive.cexpr! 'rb_int_uminus(self)' end
返回取反的 self。
源代码
VALUE
rb_int_div(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_div(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_div(x, y);
}
return Qnil;
}
执行除法;对于整数 numeric,将结果截断为整数
4 / 3 # => 1 4 / -3 # => -2 -4 / 3 # => -2 -4 / -3 # => 1 For other +numeric+, returns non-integer result: 4 / 3.0 # => 1.3333333333333333 4 / Rational(3, 1) # => (4/3) 4 / Complex(3, 0) # => ((4/3)+0i)
源代码
static VALUE
int_lt(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_lt(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_lt(x, y);
}
return Qnil;
}
如果 self 的值小于 other 的值,则返回 true
1 < 0 # => false 1 < 1 # => false 1 < 2 # => true 1 < 0.5 # => false 1 < Rational(1, 2) # => false Raises an exception if the comparison cannot be made.
源代码
VALUE
rb_int_lshift(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return rb_fix_lshift(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_lshift(x, y);
}
return Qnil;
}
返回 self,其位向左移动 count 位,如果 count 为负数,则向右移动
n = 0b11110000 "%08b" % (n << 1) # => "111100000" "%08b" % (n << 3) # => "11110000000" "%08b" % (n << -1) # => "01111000" "%08b" % (n << -3) # => "00011110"
相关:Integer#>>。
源代码
static VALUE
int_le(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_le(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_le(x, y);
}
return Qnil;
}
如果 self 的值小于或等于 other 的值,则返回 true
1 <= 0 # => false 1 <= 1 # => true 1 <= 2 # => true 1 <= 0.5 # => false 1 <= Rational(1, 2) # => false
如果无法进行比较,则引发异常。
源代码
VALUE
rb_int_cmp(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_cmp(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_cmp(x, y);
}
else {
rb_raise(rb_eNotImpError, "need to define '<=>' in %s", rb_obj_classname(x));
}
}
返回
-
-1,如果
self小于other。 -
0,如果
self等于other。 -
1,如果
self大于other。 -
如果
self和other不可比较,则返回nil。
示例
1 <=> 2 # => -1 1 <=> 1 # => 0 1 <=> 0 # => 1 1 <=> 'foo' # => nil 1 <=> 1.0 # => 0 1 <=> Rational(1, 1) # => 0 1 <=> Complex(1, 0) # => 0
此方法是模块 Comparable 中比较的基础。
如果 self 在数值上等于 other,则返回 true;否则返回 false。
1 == 2 #=> false 1 == 1.0 #=> true
相关:Integer#eql? (要求 other 为整数)。
源代码
VALUE
rb_int_gt(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_gt(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_gt(x, y);
}
return Qnil;
}
如果 self 的值大于 other 的值,则返回 true。
1 > 0 # => true 1 > 1 # => false 1 > 2 # => false 1 > 0.5 # => true 1 > Rational(1, 2) # => true Raises an exception if the comparison cannot be made.
源代码
VALUE
rb_int_ge(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_ge(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_ge(x, y);
}
return Qnil;
}
如果 self 的值大于或等于 other 的值,则返回 true。
1 >= 0 # => true 1 >= 1 # => true 1 >= 2 # => false 1 >= 0.5 # => true 1 >= Rational(1, 2) # => true
如果无法进行比较,则引发异常。
源代码
VALUE
rb_int_rshift(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return rb_fix_rshift(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_rshift(x, y);
}
return Qnil;
}
返回 self 的位向右移动 count 位,如果 count 为负数,则向左移动。
n = 0b11110000 "%08b" % (n >> 1) # => "01111000" "%08b" % (n >> 3) # => "00011110" "%08b" % (n >> -1) # => "111100000" "%08b" % (n >> -3) # => "11110000000"
相关:Integer#<<。
源代码
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;
}
返回 self 中的一段位。
使用参数 offset,返回给定偏移量处的位,其中偏移量 0 指的是最低有效位。
n = 0b10 # => 2 n[0] # => 0 n[1] # => 1 n[2] # => 0 n[3] # => 0
原则上,n[i] 等价于 (n >> i) & 1。因此,负索引始终返回零。
255[-1] # => 0
使用参数 offset 和 size,返回 self 中从 offset 开始的 size 位,包括更高有效位的位。
n = 0b111000 # => 56 "%010b" % n[0, 10] # => "0000111000" "%010b" % n[4, 10] # => "0000000011"
使用参数 range,返回 self 中从 range.begin 开始的 range.size 位,包括更高有效位的位。
n = 0b111000 # => 56 "%010b" % n[0..9] # => "0000111000" "%010b" % n[4..9] # => "0000000011"
如果无法构造切片,则引发异常。
源代码
源代码
static VALUE
int_or(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_or(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_or(x, y);
}
return Qnil;
}
按位或;如果 self 或 other 中相应的位中有任何一个是 1,则结果中的每个位为 1,否则为 0。
"%04b" % (0b0101 | 0b0110) # => "0111"
如果 other 不是整数,则引发异常。
源代码
# File numeric.rb, line 118 def ~ Primitive.attr! :leaf Primitive.cexpr! 'rb_int_comp(self)' end
按位取反:返回 self 的值,其中每个位都取反。
由于整数值在概念上是无限长的,因此结果的行为就好像它的左侧有无限个 1 位。在十六进制表示中,这显示为数字左侧的两个句点。
sprintf("%X", ~0x1122334455) # => "..FEEDDCCBBAA"
源代码
# File numeric.rb, line 132 def abs Primitive.attr! :leaf Primitive.cexpr! 'rb_int_abs(self)' end
返回 self 的绝对值。
(-12345).abs # => 12345 -12345.abs # => 12345 12345.abs # => 12345
源代码
static VALUE
int_allbits_p(VALUE num, VALUE mask)
{
mask = rb_to_int(mask);
return rb_int_equal(rb_int_and(num, mask), mask);
}
如果 mask 中所有置位 (=1) 的位也在 self 中置位,则返回 true;否则返回 false。
示例值
0b1010101 self
0b1010100 mask
0b1010100 self & mask
true self.allbits?(mask)
0b1010100 self
0b1010101 mask
0b1010100 self & mask
false self.allbits?(mask)
源代码
static VALUE
int_anybits_p(VALUE num, VALUE mask)
{
mask = rb_to_int(mask);
return RBOOL(!int_zero_p(rb_int_and(num, mask)));
}
如果 mask 中任何置位 (=1) 的位也在 self 中置位,则返回 true;否则返回 false。
示例值
0b10000010 self
0b11111111 mask
0b10000010 self & mask
true self.anybits?(mask)
0b00000000 self
0b11111111 mask
0b00000000 self & mask
false self.anybits?(mask)
源代码
# File numeric.rb, line 179 def bit_length Primitive.attr! :leaf Primitive.cexpr! 'rb_int_bit_length(self)' end
返回 self 值的位数,它是与符号位不同的最高位的位置(其中最低有效位的位位置为 1)。如果没有这样的位(零或负一),则返回零。
此方法返回 ceil(log2(self < 0 ? -self : self + 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
对于整数 n,此方法可用于检测 Array#pack 中的溢出。
if n.bit_length < 32 [n].pack('l') # No overflow. else raise 'Overflow' end
源代码
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);
}
返回一个整数,它是 self 的“上限”值,由给定的 ndigits 指定,它必须是可转换为整数的对象。
-
当
self为零时,返回零(无论ndigits的值如何)。0.ceil(2) # => 0 0.ceil(-2) # => 0
-
当
self不为零且ndigits为非负数时,返回self。555.ceil # => 555 555.ceil(50) # => 555
-
当
self不为零且ndigits为负数时,返回基于计算的粒度的值。-
粒度为
10 ** ndigits.abs。 -
返回的值是大于或等于
self的最小粒度倍数。
正
self的示例ndigits 粒度 1234.ceil(ndigits) -1 10 1240 -2 100 1300 -3 1000 2000 -4 10000 10000 -5 100000 100000 负
self的示例ndigits 粒度 -1234.ceil(ndigits) -1 10 -1230 -2 100 -1200 -3 1000 -1000 -4 10000 0 -5 100000 0 -
相关:Integer#floor。
源代码
# File numeric.rb, line 303 def ceildiv(other) -div(0 - other) end
返回 self 除以 numeric 的结果,向上舍入到最接近的整数。
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
源代码
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);
}
根据给定的 encoding,返回一个包含由 self 的值表示的字符的 1 字符字符串。
65.chr # => "A" 0.chr # => "\x00" 255.chr # => "\xFF" string = 255.chr(Encoding::UTF_8) string.encoding # => Encoding::UTF_8
如果 self 为负数,则引发异常。
相关:Integer#ord。
源代码
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);
}
}
返回一个数组,其中包含以 Integer 对象或 Float 对象表示的 numeric 和 int。
这是通过将 numeric 转换为 Integer 或 Float 来实现的。
如果 numeric 不是 Integer 或 Float 类型,则会引发 TypeError。
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
源代码
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_BIGNUM_TYPE_P(base_value))
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_BIGNUM_TYPE_P(num))
return rb_int_digits_bigbase(num, LONG2FIX(base));
return Qnil;
}
返回一个整数数组,表示 self 的 base 基数数字;数组的第一个元素表示最低有效数字。
12345.digits # => [5, 4, 3, 2, 1] 12345.digits(7) # => [4, 6, 6, 0, 5] 12345.digits(100) # => [45, 23, 1]
如果 self 为负数或 base 小于 2,则引发异常。
源代码
VALUE
rb_int_idiv(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_idiv(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_idiv(x, y);
}
return num_div(x, y);
}
执行整数除法;返回 self 除以 numeric 的整数结果。
4.div(3) # => 1 4.div(-3) # => -2 -4.div(3) # => -2 -4.div(-3) # => 1 4.div(3.0) # => 1 4.div(Rational(3, 1)) # => 1
如果 numeric 没有方法 div,则引发异常。
源代码
VALUE
rb_int_divmod(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_divmod(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_divmod(x, y);
}
return Qnil;
}
返回一个 2 元素数组 [q, r],其中
q = (self/other).floor # Quotient r = self % other # Remainder
示例
11.divmod(4) # => [2, 3] 11.divmod(-4) # => [-3, -1] -11.divmod(4) # => [-3, 1] -11.divmod(-4) # => [2, -3] 12.divmod(4) # => [3, 0] 12.divmod(-4) # => [-3, 0] -12.divmod(4) # => [-3, 0] -12.divmod(-4) # => [3, 0] 13.divmod(4.0) # => [3, 1.0] 13.divmod(Rational(4, 1)) # => [3, (1/1)]
源代码
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;
}
使用从 self 到 limit 的每个整数值调用给定的块;返回 self。
a = [] 10.downto(5) {|i| a << i } # => 10 a # => [10, 9, 8, 7, 6, 5] a = [] 0.downto(-5) {|i| a << i } # => 0 a # => [0, -1, -2, -3, -4, -5] 4.downto(5) {|i| fail 'Cannot happen' } # => 4
如果没有给出块,则返回 Enumerator。
源代码
# File numeric.rb, line 188 def even? Primitive.attr! :leaf Primitive.cexpr! 'rb_int_even_p(self)' end
如果 self 是偶数,则返回 true,否则返回 false。
源代码
VALUE
rb_int_fdiv(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x)) {
return DBL2NUM(rb_int_fdiv_double(x, y));
}
return Qnil;
}
返回 self 除以 numeric 的 Float 结果。
4.fdiv(2) # => 2.0 4.fdiv(-2) # => -2.0 -4.fdiv(2) # => -2.0 4.fdiv(2.0) # => 2.0 4.fdiv(Rational(3, 4)) # => 5.333333333333333
如果无法将 numeric 转换为 Float,则引发异常。
源代码
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);
}
返回一个整数,它是 self 的“下限”值,由给定的 ndigits 指定,它必须是可转换为整数的对象。
-
当
self为零时,返回零(无论ndigits的值如何)。0.floor(2) # => 0 0.floor(-2) # => 0
-
当
self不为零且ndigits为非负数时,返回self。555.floor # => 555 555.floor(50) # => 555
-
当
self不为零且ndigits为负数时,返回基于计算的粒度的值。-
粒度为
10 ** ndigits.abs。 -
返回的值是小于或等于
self的最大粒度倍数。
正
self的示例ndigits 粒度 1234.floor(ndigits) -1 10 1230 -2 100 1200 -3 1000 1000 -4 10000 0 -5 100000 0 负
self的示例ndigits 粒度 -1234.floor(ndigits) -1 10 -1240 -2 100 -1300 -3 1000 -2000 -4 10000 -10000 -5 100000 -100000 -
相关:Integer#ceil。
源代码
VALUE
rb_gcd(VALUE self, VALUE other)
{
other = nurat_int_value(other);
return f_gcd(self, other);
}
返回两个整数的最大公约数。结果始终为正数。0.gcd(x) 和 x.gcd(0) 返回 x.abs。
36.gcd(60) #=> 12 2.gcd(2) #=> 2 3.gcd(-7) #=> 1 ((1<<31)-1).gcd((1<<61)-1) #=> 1
源代码
VALUE
rb_gcdlcm(VALUE self, VALUE other)
{
other = nurat_int_value(other);
return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
}
返回一个包含两个整数的最大公约数和最小公倍数的数组,[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]
源代码
VALUE
rb_lcm(VALUE self, VALUE other)
{
other = nurat_int_value(other);
return f_lcm(self, other);
}
返回两个整数的最小公倍数。结果始终为正数。0.lcm(x) 和 x.lcm(0) 返回零。
36.lcm(60) #=> 180 2.lcm(2) #=> 2 3.lcm(-7) #=> 21 ((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
源代码
static VALUE
int_nobits_p(VALUE num, VALUE mask)
{
mask = rb_to_int(mask);
return RBOOL(int_zero_p(rb_int_and(num, mask)));
}
如果 mask 中没有置位 (=1) 的位也在 self 中置位,则返回 true;否则返回 false。
示例值
0b11110000 self
0b00001111 mask
0b00000000 self & mask
true self.nobits?(mask)
0b00000001 self
0b11111111 mask
0b00000001 self & mask
false self.nobits?(mask)
源代码
# File numeric.rb, line 207 def odd? Primitive.attr! :leaf Primitive.cexpr! 'rb_int_odd_p(self)' end
如果 self 是奇数,则返回 true,否则返回 false。
源代码
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);
}
返回(模)幂运算,如下所示
a.pow(b) #=> same as a**b a.pow(b, m) #=> same as (a**b) % m, but avoids huge temporary values
源代码
static VALUE
rb_int_pred(VALUE num)
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) - 1;
return LONG2NUM(i);
}
if (RB_BIGNUM_TYPE_P(num)) {
return rb_big_minus(num, INT2FIX(1));
}
return num_funcall1(num, '-', INT2FIX(1));
}
返回 self 的前一个值(等价于 self - 1)。
1.pred #=> 0 -1.pred #=> -2
相关:Integer#succ(后继值)。
源代码
static VALUE
integer_rationalize(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
return integer_to_r(self);
}
将值作为有理数返回。可选参数 eps 始终被忽略。
源代码
static VALUE
int_remainder(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
if (FIXNUM_P(y)) {
VALUE z = fix_mod(x, y);
RUBY_ASSERT(FIXNUM_P(z));
if (z != INT2FIX(0) && (SIGNED_VALUE)(x ^ y) < 0)
z = fix_minus(z, y);
return z;
}
else if (!RB_BIGNUM_TYPE_P(y)) {
return num_remainder(x, y);
}
x = rb_int2big(FIX2LONG(x));
}
else if (!RB_BIGNUM_TYPE_P(x)) {
return Qnil;
}
return rb_big_remainder(x, y);
}
返回 self 除以 other 后的余数。
示例
11.remainder(4) # => 3 11.remainder(-4) # => 3 -11.remainder(4) # => -3 -11.remainder(-4) # => -3 12.remainder(4) # => 0 12.remainder(-4) # => 0 -12.remainder(4) # => 0 -12.remainder(-4) # => 0 13.remainder(4.0) # => 1.0 13.remainder(Rational(4, 1)) # => (1/1)
源代码
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);
}
返回 self 四舍五入到最接近的值,精度为 ndigits 位十进制数字。
当 ndigits 为负数时,返回的值至少有 ndigits.abs 个尾随零。
555.round(-1) # => 560 555.round(-2) # => 600 555.round(-3) # => 1000 -555.round(-2) # => -600 555.round(-4) # => 0
当 ndigits 为零或正数时,返回 self。
555.round # => 555 555.round(1) # => 555 555.round(50) # => 555
如果给出了关键字参数 half,并且 self 与两个候选值的距离相等,则根据给定的 half 值进行舍入。
-
:up或nil:远离零舍入。25.round(-1, half: :up) # => 30 (-25).round(-1, half: :up) # => -30
-
:down:向零舍入。25.round(-1, half: :down) # => 20 (-25).round(-1, half: :down) # => -20
-
:even:向最后一个非零数字为偶数的候选值舍入。25.round(-1, half: :even) # => 20 15.round(-1, half: :even) # => 20 (-25).round(-1, half: :even) # => -20
如果 half 的值无效,则引发异常。
相关:Integer#truncate。
源代码
# File numeric.rb, line 234 def size Primitive.attr! :leaf Primitive.cexpr! 'rb_int_size(self)' end
返回 self 的机器表示形式中的字节数;该值取决于系统。
1.size # => 8 -1.size # => 8 2147483647.size # => 8 (256**10 - 1).size # => 10 (256**20 - 1).size # => 20 (256**40 - 1).size # => 40
源代码
VALUE
rb_int_succ(VALUE num)
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
if (RB_BIGNUM_TYPE_P(num)) {
return rb_big_plus(num, INT2FIX(1));
}
return num_funcall1(num, '+', INT2FIX(1));
}
返回 self 的后继整数(等效于 self + 1)
1.succ #=> 2 -1.succ #=> 0
相关:Integer#pred(前驱值)。
源代码
# File numeric.rb, line 250 def times Primitive.attr! :inline_block unless defined?(yield) return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, int_dotimes_size)' end i = 0 while i < self yield i i = i.succ end self end
使用 (0..self-1) 中的每个整数,调用给定的代码块 self 次。
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
如果没有给出块,则返回 Enumerator。
源代码
# File ext/openssl/lib/openssl/bn.rb, line 37 def to_bn OpenSSL::BN::new(self) end
将 Integer 强制转换为 OpenSSL::BN
请参阅“man bn”以获取更多信息。
源代码
static VALUE
int_to_f(VALUE num)
{
double val;
if (FIXNUM_P(num)) {
val = (double)FIX2LONG(num);
}
else if (RB_BIGNUM_TYPE_P(num)) {
val = rb_big2dbl(num);
}
else {
rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
}
return DBL2NUM(val);
}
将 self 转换为 Float。
1.to_f # => 1.0 -1.to_f # => -1.0
如果 self 的值不适合 Float,则结果为无穷大。
(10**400).to_f # => Infinity (-10**400).to_f # => -Infinity
源代码
static VALUE
integer_to_r(VALUE self)
{
return rb_rational_new1(self);
}
以有理数形式返回值。
1.to_r #=> (1/1) (1<<64).to_r #=> (18446744073709551616/1)
源代码
VALUE
rb_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);
}
返回一个字符串,其中包含 self 在基数 base(2..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"
如果 base 超出范围,则会引发异常。
源代码
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);
}
返回 self 截断(向零方向)到精度为 ndigits 个十进制数字。
当 ndigits 为负数时,返回的值至少有 ndigits.abs 个尾随零。
555.truncate(-1) # => 550 555.truncate(-2) # => 500 -555.truncate(-2) # => -500
当 ndigits 为零或正数时,返回 self。
555.truncate # => 555 555.truncate(50) # => 555
相关:Integer#round。
源代码
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;
}
使用从 self 到 limit 的每个整数值调用给定的代码块;返回 self
a = [] 5.upto(10) {|i| a << i } # => 5 a # => [5, 6, 7, 8, 9, 10] a = [] -5.upto(0) {|i| a << i } # => -5 a # => [-5, -4, -3, -2, -1, 0] 5.upto(4) {|i| fail 'Cannot happen' } # => 5
如果没有给出块,则返回 Enumerator。
源代码
# File numeric.rb, line 283 def zero? Primitive.attr! :leaf Primitive.cexpr! 'rb_int_zero_p(self)' end
如果 self 的值为零,则返回 true,否则返回 false。