1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
class Numeric
# call-seq:
# dup -> self
#
# Returns +self+.
#
# Related: Numeric#clone.
#
def dup
self
end
# call-seq:
# real? -> true or false
#
# Returns +true+ if +self+ is a real number (i.e. not Complex).
#
def real?
true
end
# call-seq:
# real -> self
#
# Returns +self+.
#
def real
self
end
# call-seq:
# integer? -> true or false
#
# Returns +true+ if +self+ is an Integer.
#
# 1.0.integer? # => false
# 1.integer? # => true
#
def integer?
false
end
# call-seq:
# finite? -> true or false
#
# Returns +true+ if +self+ is a finite number, +false+ otherwise.
#
def finite?
true
end
# call-seq:
# infinite? -> -1, 1, or nil
#
# Returns +nil+, -1, or 1 depending on whether +self+ is
# finite, <tt>-Infinity</tt>, or <tt>+Infinity</tt>.
#
def infinite?
nil
end
# call-seq:
# imag -> 0
#
# Returns zero.
#
def imaginary
0
end
alias imag imaginary
# call-seq:
# conj -> self
#
# Returns +self+.
#
def conjugate
self
end
alias conj conjugate
# call-seq:
# +self -> self
#
# Returns +self+.
#
def +@
self
end
end
class Integer
# call-seq:
# -int -> integer
#
# Returns +self+, negated.
def -@
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_uminus(self)'
end
# call-seq:
# ~int -> integer
#
# One's complement:
# returns the value of +self+ with each bit inverted.
#
# 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:
#
# sprintf("%X", ~0x1122334455) # => "..FEEDDCCBBAA"
#
def ~
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_comp(self)'
end
# call-seq:
# abs -> integer
#
# Returns the absolute value of +self+.
#
# (-12345).abs # => 12345
# -12345.abs # => 12345
# 12345.abs # => 12345
#
def abs
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_abs(self)'
end
# call-seq:
# bit_length -> integer
#
# 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
# (where the least significant bit has bit position 1).
# 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
#
def bit_length
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_bit_length(self)'
end
# call-seq:
# even? -> true or false
#
# Returns +true+ if +self+ is an even number, +false+ otherwise.
def even?
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_even_p(self)'
end
# call-seq:
# integer? -> true
#
# Since +self+ is already an \Integer, always returns +true+.
def integer?
true
end
alias magnitude abs
# call-seq:
# odd? -> true or false
#
# Returns +true+ if +self+ is an odd number, +false+ otherwise.
def odd?
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_odd_p(self)'
end
# call-seq:
# ord -> self
#
# Returns +self+;
# intended for compatibility to character literals in Ruby 1.9.
def ord
self
end
# call-seq:
# size -> integer
#
# Returns the number of bytes in the machine representation of +self+;
# the value is system-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! :leaf
Primitive.cexpr! 'rb_int_size(self)'
end
# 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
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
# call-seq:
# to_i -> self
#
# Returns +self+ (which is already an \Integer).
def to_i
self
end
# call-seq:
# to_int -> self
#
# Returns +self+ (which is already an \Integer).
def to_int
self
end
# call-seq:
# zero? -> true or false
#
# Returns +true+ if +self+ has a zero value, +false+ otherwise.
def zero?
Primitive.attr! :leaf
Primitive.cexpr! 'rb_int_zero_p(self)'
end
# call-seq:
# ceildiv(numeric) -> integer
#
# Returns the result of division +self+ by +numeric+.
# rounded up to the nearest integer.
#
# 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
#
def ceildiv(other)
-div(0 - other)
end
#
# call-seq:
# numerator -> self
#
# Returns +self+.
#
def numerator
self
end
# call-seq:
# denominator -> 1
#
# Returns +1+.
def denominator
1
end
with_yjit do
if Primitive.rb_builtin_basic_definition_p(:downto)
undef :downto
def downto(to) # :nodoc:
Primitive.attr! :inline_block, :c_trace
# When no block is given, return an Enumerator that enumerates from `self` to `to`.
# Not using `block_given?` and `to_enum` to keep them unaffected by redefinitions.
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 1, &to, int_downto_size)'
end
from = self
while from >= to
yield from
from = from.pred
end
self
end
end
end
end
class Float
# call-seq:
# to_f -> self
#
# Returns +self+ (which is already a \Float).
def to_f
self
end
# call-seq:
# float.abs -> float
#
# Returns the absolute value of +self+:
#
# (-34.56).abs # => 34.56
# -34.56.abs # => 34.56
# 34.56.abs # => 34.56
#
def abs
Primitive.attr! :leaf
Primitive.cexpr! 'rb_float_abs(self)'
end
alias magnitude abs
# call-seq:
# -float -> float
#
# Returns +self+, negated.
#
def -@
Primitive.attr! :leaf
Primitive.cexpr! 'rb_float_uminus(self)'
end
# call-seq:
# zero? -> true or false
#
# Returns +true+ if +self+ is 0.0, +false+ otherwise.
def zero?
Primitive.attr! :leaf
Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
end
# call-seq:
# positive? -> true or false
#
# Returns +true+ if +self+ is greater than 0, +false+ otherwise.
def positive?
Primitive.attr! :leaf
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
end
# call-seq:
# negative? -> true or false
#
# Returns +true+ if +self+ is less than 0, +false+ otherwise.
def negative?
Primitive.attr! :leaf
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
end
end
|