2021-02-19 22:11:19 +03:00
|
|
|
class Numeric
|
2023-08-14 04:23:41 +03:00
|
|
|
|
2021-02-19 22:11:19 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# real? -> true or false
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ 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?
|
2022-10-27 19:13:16 +03:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# real -> self
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+.
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
|
|
|
def real
|
|
|
|
self
|
2021-02-19 22:11:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# integer? -> true or false
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is an Integer.
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# 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?
|
2022-10-27 19:13:16 +03:00
|
|
|
false
|
2021-02-19 22:11:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# finite? -> true or false
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is a finite number, +false+ otherwise.
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
|
|
|
def finite?
|
2022-10-27 19:13:16 +03:00
|
|
|
true
|
2021-02-19 22:11:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# infinite? -> -1, 1, or nil
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +nil+, -1, or 1 depending on whether +self+ is
|
|
|
|
# finite, <tt>-Infinity</tt>, or <tt>+Infinity</tt>.
|
2021-02-19 22:11:19 +03:00
|
|
|
#
|
|
|
|
def infinite?
|
2022-10-27 19:13:16 +03:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# imag -> 0
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
|
|
|
# Returns zero.
|
|
|
|
#
|
|
|
|
def imaginary
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
alias imag imaginary
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# conj -> self
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+.
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
|
|
|
def conjugate
|
|
|
|
self
|
2021-02-19 22:11:19 +03:00
|
|
|
end
|
2022-10-27 19:13:16 +03:00
|
|
|
|
|
|
|
alias conj conjugate
|
2021-02-19 22:11:19 +03:00
|
|
|
end
|
|
|
|
|
2020-06-21 00:55:09 +03:00
|
|
|
class Integer
|
2020-12-23 09:23:45 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# -int -> integer
|
2020-12-23 09:23:45 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+, negated.
|
2020-12-23 09:23:45 +03:00
|
|
|
def -@
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-12-23 09:23:45 +03:00
|
|
|
Primitive.cexpr! 'rb_int_uminus(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# ~int -> integer
|
|
|
|
#
|
|
|
|
# One's complement:
|
|
|
|
# returns the value of +self+ with each bit inverted.
|
2020-12-23 09:23:45 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Because an integer value is 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:
|
2020-12-23 09:23:45 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# sprintf("%X", ~0x1122334455) # => "..FEEDDCCBBAA"
|
2020-12-23 09:23:45 +03:00
|
|
|
#
|
|
|
|
def ~
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-12-23 09:23:45 +03:00
|
|
|
Primitive.cexpr! 'rb_int_comp(self)'
|
|
|
|
end
|
|
|
|
|
2021-08-30 07:20:03 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# abs -> integer
|
2021-08-30 07:20:03 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns the absolute value of +self+.
|
2021-08-30 07:20:03 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# (-12345).abs # => 12345
|
|
|
|
# -12345.abs # => 12345
|
|
|
|
# 12345.abs # => 12345
|
2021-08-30 07:20:03 +03:00
|
|
|
#
|
2020-06-27 20:07:47 +03:00
|
|
|
def abs
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-06-27 20:07:47 +03:00
|
|
|
Primitive.cexpr! 'rb_int_abs(self)'
|
|
|
|
end
|
|
|
|
|
2020-12-15 04:01:55 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# bit_length -> integer
|
2020-12-15 04:01:55 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns the number of bits of the value of +self+,
|
|
|
|
# which is the bit position of the highest-order bit
|
|
|
|
# that is different from the sign bit
|
2020-12-15 04:01:55 +03:00
|
|
|
# (where the least significant bit has bit position 1).
|
2023-08-14 04:23:41 +03:00
|
|
|
# If there is no such bit (zero or minus one), returns zero.
|
|
|
|
#
|
|
|
|
# This method returns <tt>ceil(log2(self < 0 ? -self : self + 1))</tt>>.
|
|
|
|
#
|
|
|
|
# (-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
|
|
|
|
#
|
|
|
|
# For \Integer _n_,
|
|
|
|
# this method can be used to detect overflow in Array#pack:
|
|
|
|
#
|
|
|
|
# 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
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-06-27 20:07:47 +03:00
|
|
|
Primitive.cexpr! 'rb_int_bit_length(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# even? -> true or false
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is an even number, +false+ otherwise.
|
2020-06-27 20:07:47 +03:00
|
|
|
def even?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
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:
|
2023-08-14 04:23:41 +03:00
|
|
|
# integer? -> true
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Since +self+ is already an \Integer, always returns +true+.
|
2020-06-27 20:07:47 +03:00
|
|
|
def integer?
|
2022-10-27 19:13:16 +03:00
|
|
|
true
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2021-08-30 07:20:03 +03:00
|
|
|
alias magnitude abs
|
2020-06-27 20:07:47 +03:00
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# odd? -> true or false
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is an odd number, +false+ otherwise.
|
2020-06-27 20:07:47 +03:00
|
|
|
def odd?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-06-27 20:07:47 +03:00
|
|
|
Primitive.cexpr! 'rb_int_odd_p(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# ord -> self
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+;
|
|
|
|
# intended for compatibility to character literals in Ruby 1.9.
|
2020-06-27 20:07:47 +03:00
|
|
|
def ord
|
2022-10-27 19:13:16 +03:00
|
|
|
self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2021-06-05 07:57:21 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# size -> integer
|
2021-06-05 07:57:21 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns the number of bytes in the machine representation of +self+;
|
|
|
|
# the value is system-dependent:
|
2021-06-05 07:57:21 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# 1.size # => 8
|
|
|
|
# -1.size # => 8
|
|
|
|
# 2147483647.size # => 8
|
|
|
|
# (256**10 - 1).size # => 10
|
|
|
|
# (256**20 - 1).size # => 20
|
|
|
|
# (256**40 - 1).size # => 40
|
2021-06-05 07:57:21 +03:00
|
|
|
#
|
|
|
|
def size
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2021-06-05 07:57:21 +03:00
|
|
|
Primitive.cexpr! 'rb_int_size(self)'
|
|
|
|
end
|
|
|
|
|
2023-09-07 20:57:52 +03:00
|
|
|
# call-seq:
|
|
|
|
# times {|i| ... } -> self
|
|
|
|
# times -> enumerator
|
|
|
|
#
|
|
|
|
# Calls the given block +self+ times with each integer in <tt>(0..self-1)</tt>:
|
|
|
|
#
|
|
|
|
# a = []
|
|
|
|
# 5.times {|i| a.push(i) } # => 5
|
|
|
|
# a # => [0, 1, 2, 3, 4]
|
|
|
|
#
|
|
|
|
# With no block given, returns an Enumerator.
|
|
|
|
def times
|
2024-01-23 22:36:23 +03:00
|
|
|
Primitive.attr! :inline_block
|
2024-02-17 17:28:00 +03:00
|
|
|
unless defined?(yield)
|
|
|
|
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, int_dotimes_size)'
|
2023-09-07 20:57:52 +03:00
|
|
|
end
|
|
|
|
i = 0
|
|
|
|
while i < self
|
|
|
|
yield i
|
|
|
|
i = i.succ
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2020-06-27 20:07:47 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# to_i -> self
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+ (which is already an \Integer).
|
2020-06-27 20:07:47 +03:00
|
|
|
def to_i
|
2022-10-27 19:13:16 +03:00
|
|
|
self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# to_int -> self
|
2020-06-27 20:07:47 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+ (which is already an \Integer).
|
2020-06-27 20:07:47 +03:00
|
|
|
def to_int
|
2022-10-27 19:13:16 +03:00
|
|
|
self
|
2020-06-27 20:07:47 +03:00
|
|
|
end
|
|
|
|
|
2020-06-21 00:55:09 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# zero? -> true or false
|
2020-06-21 00:55:09 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ has a zero value, +false+ otherwise.
|
2020-06-21 00:55:09 +03:00
|
|
|
def zero?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2020-07-10 05:49:50 +03:00
|
|
|
Primitive.cexpr! 'rb_int_zero_p(self)'
|
2020-06-21 00:55:09 +03:00
|
|
|
end
|
2022-07-22 06:05:16 +03:00
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# ceildiv(numeric) -> integer
|
2022-07-22 06:05:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns the result of division +self+ by +numeric+.
|
|
|
|
# rounded up to the nearest integer.
|
2022-07-22 06:05:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# 3.ceildiv(3) # => 1
|
|
|
|
# 4.ceildiv(3) # => 2
|
2022-07-22 06:05:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# 4.ceildiv(-3) # => -1
|
|
|
|
# -4.ceildiv(3) # => -1
|
2022-07-22 06:05:16 +03:00
|
|
|
# -4.ceildiv(-3) # => 2
|
|
|
|
#
|
|
|
|
# 3.ceildiv(1.2) # => 3
|
2023-08-14 04:23:41 +03:00
|
|
|
#
|
2022-07-22 06:05:16 +03:00
|
|
|
def ceildiv(other)
|
2023-01-22 12:53:02 +03:00
|
|
|
-div(0 - other)
|
2022-07-22 06:05:16 +03:00
|
|
|
end
|
2022-10-27 19:13:16 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# numerator -> self
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+.
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
|
|
|
def numerator
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# denominator -> 1
|
2022-10-27 19:13:16 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +1+.
|
2022-10-27 19:13:16 +03:00
|
|
|
def denominator
|
|
|
|
1
|
|
|
|
end
|
2020-06-21 00:55:09 +03:00
|
|
|
end
|
2021-01-02 05:39:07 +03:00
|
|
|
|
|
|
|
class Float
|
2023-08-14 04:23:41 +03:00
|
|
|
|
2021-01-02 05:39:07 +03:00
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# to_f -> self
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+ (which is already a \Float).
|
2021-01-02 05:39:07 +03:00
|
|
|
def to_f
|
2022-10-27 19:13:16 +03:00
|
|
|
self
|
2021-01-02 05:39:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# float.abs -> float
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns the absolute value of +self+:
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# (-34.56).abs # => 34.56
|
|
|
|
# -34.56.abs # => 34.56
|
|
|
|
# 34.56.abs # => 34.56
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
|
|
|
def abs
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2021-01-02 05:39:07 +03:00
|
|
|
Primitive.cexpr! 'rb_float_abs(self)'
|
|
|
|
end
|
|
|
|
|
2024-10-02 06:04:17 +03:00
|
|
|
alias magnitude abs
|
2021-01-02 05:39:07 +03:00
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# -float -> float
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +self+, negated.
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
|
|
|
def -@
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
2021-01-02 05:39:07 +03:00
|
|
|
Primitive.cexpr! 'rb_float_uminus(self)'
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2023-08-14 04:23:41 +03:00
|
|
|
# zero? -> true or false
|
2021-01-02 05:39:07 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is 0.0, +false+ otherwise.
|
2021-01-02 05:39:07 +03:00
|
|
|
def zero?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
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:
|
2023-08-14 04:23:41 +03:00
|
|
|
# positive? -> true or false
|
2021-02-09 07:29:42 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is greater than 0, +false+ otherwise.
|
2021-02-09 07:29:42 +03:00
|
|
|
def positive?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
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:
|
2023-08-14 04:23:41 +03:00
|
|
|
# negative? -> true or false
|
2021-02-09 07:29:42 +03:00
|
|
|
#
|
2023-08-14 04:23:41 +03:00
|
|
|
# Returns +true+ if +self+ is less than 0, +false+ otherwise.
|
2021-02-09 07:29:42 +03:00
|
|
|
def negative?
|
2023-03-12 01:25:11 +03:00
|
|
|
Primitive.attr! :leaf
|
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
|