4 # num.real? -> true or false
6 # Returns +true+ if +num+ is a real number (i.e. not Complex).
14 # num.integer? -> true or false
16 # Returns +true+ if +num+ is an Integer.
18 # 1.0.integer? #=> false
27 # num.finite? -> true or false
29 # Returns +true+ if +num+ is a finite number, otherwise returns +false+.
37 # num.infinite? -> -1, 1, or nil
39 # Returns +nil+, -1, or 1 depending on whether the value is
40 # finite, <code>-Infinity</code>, or <code>+Infinity</code>.
51 # Returns +int+, negated.
53 Primitive.attr! 'inline'
54 Primitive.cexpr! 'rb_int_uminus(self)'
60 # One's complement: returns a number where each bit is flipped.
62 # Inverts the bits in an Integer. As integers are conceptually of
63 # infinite length, the result acts as if it had an infinite number of
64 # one bits to the left. In hex representations, this is displayed
65 # as two periods to the left of the digits.
67 # sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
69 Primitive.attr! 'inline'
70 Primitive.cexpr! 'rb_int_comp(self)'
75 # int.magnitude -> integer
77 # Returns the absolute value of +int+.
79 # (-12345).abs #=> 12345
80 # -12345.abs #=> 12345
83 # Integer#magnitude is an alias for Integer#abs.
85 Primitive.attr! 'inline'
86 Primitive.cexpr! 'rb_int_abs(self)'
90 # int.bit_length -> integer
92 # Returns the number of bits of the value of +int+.
94 # "Number of bits" means the bit position of the highest bit
95 # which is different from the sign bit
96 # (where the least significant bit has bit position 1).
97 # If there is no such bit (zero or minus one), zero is returned.
99 # I.e. this method returns <i>ceil(log2(int < 0 ? -int : int+1))</i>.
101 # (-2**1000-1).bit_length #=> 1001
102 # (-2**1000).bit_length #=> 1000
103 # (-2**1000+1).bit_length #=> 1000
104 # (-2**12-1).bit_length #=> 13
105 # (-2**12).bit_length #=> 12
106 # (-2**12+1).bit_length #=> 12
107 # -0x101.bit_length #=> 9
108 # -0x100.bit_length #=> 8
109 # -0xff.bit_length #=> 8
110 # -2.bit_length #=> 1
111 # -1.bit_length #=> 0
114 # 0xff.bit_length #=> 8
115 # 0x100.bit_length #=> 9
116 # (2**12-1).bit_length #=> 12
117 # (2**12).bit_length #=> 13
118 # (2**12+1).bit_length #=> 13
119 # (2**1000-1).bit_length #=> 1000
120 # (2**1000).bit_length #=> 1001
121 # (2**1000+1).bit_length #=> 1001
123 # This method can be used to detect overflow in Array#pack as follows:
125 # if n.bit_length < 32
126 # [n].pack("l") # no overflow
131 Primitive.attr! 'inline'
132 Primitive.cexpr! 'rb_int_bit_length(self)'
136 # int.even? -> true or false
138 # Returns +true+ if +int+ is an even number.
140 Primitive.attr! 'inline'
141 Primitive.cexpr! 'rb_int_even_p(self)'
145 # int.integer? -> true
147 # Since +int+ is already an Integer, this always returns +true+.
155 Primitive.attr! 'inline'
156 Primitive.cexpr! 'rb_int_abs(self)'
161 # int.odd? -> true or false
163 # Returns +true+ if +int+ is an odd number.
165 Primitive.attr! 'inline'
166 Primitive.cexpr! 'rb_int_odd_p(self)'
172 # Returns the +int+ itself.
176 # This method is intended for compatibility to character literals
179 # For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
185 # Document-method: Integer#size
189 # Returns the number of bytes in the machine representation of +int+
190 # (machine dependent).
194 # 2147483647.size #=> 8
195 # (256**10 - 1).size #=> 10
196 # (256**20 - 1).size #=> 20
197 # (256**40 - 1).size #=> 40
200 Primitive.attr! 'inline'
201 Primitive.cexpr! 'rb_int_size(self)'
205 # int.to_i -> integer
207 # Since +int+ is already an Integer, returns +self+.
209 # #to_int is an alias for #to_i.
215 # int.to_int -> integer
217 # Since +int+ is already an Integer, returns +self+.
223 # int.zero? -> true or false
225 # Returns +true+ if +int+ has a zero value.
227 Primitive.attr! 'inline'
228 Primitive.cexpr! 'rb_int_zero_p(self)'
233 # Integer.try_convert(object) -> object, integer, or nil
235 # If +object+ is an \Integer object, returns +object+.
236 # Integer.try_convert(1) # => 1
238 # Otherwise if +object+ responds to <tt>:to_int</tt>,
239 # calls <tt>object.to_int</tt> and returns the result.
240 # Integer.try_convert(1.25) # => 1
242 # Returns +nil+ if +object+ does not respond to <tt>:to_int</tt>
243 # Integer.try_convert([]) # => nil
245 # Raises an exception unless <tt>object.to_int</tt> returns an \Integer object.
247 def Integer.try_convert(num)
249 Primitive.attr! 'inline'
250 Primitive.cexpr! 'rb_check_integer_type(num)'
259 # Since +float+ is already a Float, returns +self+.
268 # float.magnitude -> float
270 # Returns the absolute value of +float+.
272 # (-34.56).abs #=> 34.56
273 # -34.56.abs #=> 34.56
274 # 34.56.abs #=> 34.56
276 # Float#magnitude is an alias for Float#abs.
279 Primitive.attr! 'inline'
280 Primitive.cexpr! 'rb_float_abs(self)'
284 Primitive.attr! 'inline'
285 Primitive.cexpr! 'rb_float_abs(self)'
292 # Returns +float+, negated.
295 Primitive.attr! 'inline'
296 Primitive.cexpr! 'rb_float_uminus(self)'
301 # float.zero? -> true or false
303 # Returns +true+ if +float+ is 0.0.
306 Primitive.attr! 'inline'
307 Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
312 # float.positive? -> true or false
314 # Returns +true+ if +float+ is greater than 0.
317 Primitive.attr! 'inline'
318 Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
323 # float.negative? -> true or false
325 # Returns +true+ if +float+ is less than 0.
328 Primitive.attr! 'inline'
329 Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'