2021-02-19 22:11:19 +03:00
|
|
|
class Numeric
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# num.real? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +num+ is a real number (i.e. not Complex).
|
2021-03-04 05:40:02 +03:00
|
|
|
#
|
2021-02-19 22:11:19 +03:00
|
|
|
def real?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# num.integer? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +num+ is an Integer.
|
|
|
|
#
|
|
|
|
# 1.0.integer? #=> false
|
|
|
|
# 1.integer? #=> true
|
2021-03-04 05:40:02 +03:00
|
|
|
#
|
2021-02-19 22:11:19 +03:00
|
|
|
def integer?
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# num.finite? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +num+ is a finite number, otherwise returns +false+.
|
|
|
|
#
|
|
|
|
def finite?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# num.infinite? -> -1, 1, or nil
|
|
|
|
#
|
|
|
|
# Returns +nil+, -1, or 1 depending on whether the value is
|
|
|
|
# finite, <code>-Infinity</code>, or <code>+Infinity</code>.
|
|
|
|
#
|
|
|
|
def infinite?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-21 00:55:09 +03:00
|
|
|
class Integer
|
2020-12-23 09:23:45 +03:00
|
|
|
# call-seq:
|
|
|
|
# -int -> integer
|
|
|
|
#
|
|
|
|
# Returns +int+, negated.
|
|
|
|
def -@
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_uminus(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# ~int -> 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"
|
|
|
|
def ~
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_comp(self)'
|
|
|
|
end
|
|
|
|
|
2021-08-30 07:20:03 +03:00
|
|
|
# call-seq:
|
|
|
|
# int.abs -> integer
|
|
|
|
# int.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.
|
2020-06-27 20:07:47 +03:00
|
|
|
def abs
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_abs(self)'
|
|
|
|
end
|
|
|
|
|
2020-12-15 04:01:55 +03:00
|
|
|
# call-seq:
|
|
|
|
# int.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 <i>ceil(log2(int < 0 ? -int : int+1))</i>.
|
|
|
|
#
|
|
|
|
# (-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
|
2020-06-27 20:07:47 +03:00
|
|
|
def bit_length
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_bit_length(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# int.even? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +int+ is an even number.
|
|
|
|
def even?
|
|
|
|
Primitive.attr! 'inline'
|
2020-07-10 05:49:50 +03:00
|
|
|
Primitive.cexpr! 'rb_int_even_p(self)'
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# int.integer? -> true
|
|
|
|
#
|
|
|
|
# Since +int+ is already an Integer, this always returns +true+.
|
|
|
|
def integer?
|
2020-07-02 03:44:23 +03:00
|
|
|
return true
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2021-08-30 07:20:03 +03:00
|
|
|
alias magnitude abs
|
|
|
|
=begin
|
2020-06-27 20:07:47 +03:00
|
|
|
def magnitude
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_abs(self)'
|
|
|
|
end
|
2021-08-30 07:20:03 +03:00
|
|
|
=end
|
2020-06-27 20:07:47 +03:00
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# int.odd? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +int+ is an odd number.
|
|
|
|
def odd?
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_odd_p(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# int.ord -> self
|
|
|
|
#
|
|
|
|
# Returns the +int+ itself.
|
|
|
|
#
|
|
|
|
# 97.ord #=> 97
|
|
|
|
#
|
|
|
|
# This method is intended for compatibility to character literals
|
|
|
|
# in Ruby 1.9.
|
|
|
|
#
|
|
|
|
# For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
|
|
|
|
def ord
|
2020-07-02 03:44:23 +03:00
|
|
|
return self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2021-06-05 07:57:21 +03:00
|
|
|
#
|
|
|
|
# Document-method: Integer#size
|
|
|
|
# call-seq:
|
|
|
|
# int.size -> int
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
def size
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_int_size(self)'
|
|
|
|
end
|
|
|
|
|
2020-06-27 20:07:47 +03:00
|
|
|
# call-seq:
|
|
|
|
# int.to_i -> integer
|
|
|
|
#
|
|
|
|
# Since +int+ is already an Integer, returns +self+.
|
|
|
|
#
|
|
|
|
# #to_int is an alias for #to_i.
|
|
|
|
def to_i
|
2020-07-02 03:44:23 +03:00
|
|
|
return self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# int.to_int -> integer
|
|
|
|
#
|
|
|
|
# Since +int+ is already an Integer, returns +self+.
|
|
|
|
def to_int
|
2020-07-02 03:44:23 +03:00
|
|
|
return self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2020-06-21 00:55:09 +03:00
|
|
|
# call-seq:
|
|
|
|
# int.zero? -> true or false
|
|
|
|
#
|
2020-06-30 11:13:37 +03:00
|
|
|
# Returns +true+ if +int+ has a zero value.
|
2020-06-21 00:55:09 +03:00
|
|
|
def zero?
|
2020-06-21 03:13:03 +03:00
|
|
|
Primitive.attr! 'inline'
|
2020-07-10 05:49:50 +03:00
|
|
|
Primitive.cexpr! 'rb_int_zero_p(self)'
|
2020-06-21 00:55:09 +03:00
|
|
|
end
|
|
|
|
end
|
2021-01-02 05:39:07 +03:00
|
|
|
|
2021-12-08 11:59:16 +03:00
|
|
|
# call-seq:
|
|
|
|
# Integer.try_convert(object) -> object, integer, or nil
|
|
|
|
#
|
|
|
|
# If +object+ is an \Integer object, returns +object+.
|
2021-12-14 20:12:37 +03:00
|
|
|
# Integer.try_convert(1) # => 1
|
2021-12-08 11:59:16 +03:00
|
|
|
#
|
|
|
|
# Otherwise if +object+ responds to <tt>:to_int</tt>,
|
|
|
|
# calls <tt>object.to_int</tt> and returns the result.
|
2021-12-14 20:12:37 +03:00
|
|
|
# Integer.try_convert(1.25) # => 1
|
2021-12-08 11:59:16 +03:00
|
|
|
#
|
2021-12-14 19:46:08 +03:00
|
|
|
# Returns +nil+ if +object+ does not respond to <tt>:to_int</tt>
|
2021-12-14 20:12:37 +03:00
|
|
|
# Integer.try_convert([]) # => nil
|
2021-12-08 11:59:16 +03:00
|
|
|
#
|
|
|
|
# Raises an exception unless <tt>object.to_int</tt> returns an \Integer object.
|
|
|
|
#
|
|
|
|
def Integer.try_convert(num)
|
|
|
|
=begin
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_check_integer_type(num)'
|
|
|
|
=end
|
|
|
|
end if false
|
|
|
|
|
2021-01-02 05:39:07 +03:00
|
|
|
class Float
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# float.to_f -> self
|
|
|
|
#
|
|
|
|
# Since +float+ is already a Float, returns +self+.
|
|
|
|
#
|
|
|
|
def to_f
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# float.abs -> float
|
|
|
|
# float.magnitude -> float
|
|
|
|
#
|
|
|
|
# Returns the absolute value of +float+.
|
|
|
|
#
|
|
|
|
# (-34.56).abs #=> 34.56
|
|
|
|
# -34.56.abs #=> 34.56
|
|
|
|
# 34.56.abs #=> 34.56
|
|
|
|
#
|
|
|
|
# Float#magnitude is an alias for Float#abs.
|
|
|
|
#
|
|
|
|
def abs
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_float_abs(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
def magnitude
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_float_abs(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# -float -> float
|
|
|
|
#
|
|
|
|
# Returns +float+, negated.
|
|
|
|
#
|
|
|
|
def -@
|
|
|
|
Primitive.attr! 'inline'
|
|
|
|
Primitive.cexpr! 'rb_float_uminus(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# float.zero? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +float+ is 0.0.
|
|
|
|
#
|
|
|
|
def zero?
|
|
|
|
Primitive.attr! 'inline'
|
2021-08-31 14:30:35 +03:00
|
|
|
Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
|
2021-01-02 05:39:07 +03:00
|
|
|
end
|
2021-02-09 07:29:42 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# float.positive? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +float+ is greater than 0.
|
|
|
|
#
|
|
|
|
def positive?
|
|
|
|
Primitive.attr! 'inline'
|
2021-08-31 14:30:35 +03:00
|
|
|
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
|
2021-02-09 07:29:42 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# float.negative? -> true or false
|
|
|
|
#
|
|
|
|
# Returns +true+ if +float+ is less than 0.
|
|
|
|
#
|
|
|
|
def negative?
|
|
|
|
Primitive.attr! 'inline'
|
2021-08-31 14:30:35 +03:00
|
|
|
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
|
2021-02-09 07:29:42 +03:00
|
|
|
end
|
2021-11-15 22:52:23 +03:00
|
|
|
|
2021-01-02 05:39:07 +03:00
|
|
|
end
|