[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / numeric.rb
blob4dc406fd2386a7987faa1caf3946d7a0bc0b318b
1 class Numeric
3   #  call-seq:
4   #    real? -> true or false
5   #
6   #  Returns +true+ if +self+ is a real number (i.e. not Complex).
7   #
8   def real?
9     true
10   end
12   # call-seq:
13   #   real -> self
14   #
15   # Returns +self+.
16   #
17   def real
18     self
19   end
21   #  call-seq:
22   #    integer? -> true or false
23   #
24   #  Returns +true+ if +self+ is an Integer.
25   #
26   #    1.0.integer? # => false
27   #    1.integer?   # => true
28   #
29   def integer?
30     false
31   end
33   #  call-seq:
34   #    finite? -> true or false
35   #
36   #  Returns +true+ if +self+ is a finite number, +false+ otherwise.
37   #
38   def finite?
39     true
40   end
42   #  call-seq:
43   #    infinite? -> -1, 1, or nil
44   #
45   #  Returns +nil+, -1, or 1 depending on whether +self+ is
46   #  finite, <tt>-Infinity</tt>, or <tt>+Infinity</tt>.
47   #
48   def infinite?
49     nil
50   end
52   # call-seq:
53   #   imag -> 0
54   #
55   # Returns zero.
56   #
57   def imaginary
58     0
59   end
61   alias imag imaginary
63   # call-seq:
64   #   conj -> self
65   #
66   # Returns +self+.
67   #
68   def conjugate
69     self
70   end
72   alias conj conjugate
73 end
75 class Integer
76   # call-seq:
77   #    -int -> integer
78   #
79   # Returns +self+, negated.
80   def -@
81     Primitive.attr! :leaf
82     Primitive.cexpr! 'rb_int_uminus(self)'
83   end
85   # call-seq:
86   #   ~int -> integer
87   #
88   # One's complement:
89   # returns the value of +self+ with each bit inverted.
90   #
91   # Because an integer value is conceptually of infinite length,
92   # the result acts as if it had an infinite number of
93   # one bits to the left.
94   # In hex representations, this is displayed
95   # as two periods to the left of the digits:
96   #
97   #   sprintf("%X", ~0x1122334455)    # => "..FEEDDCCBBAA"
98   #
99   def ~
100     Primitive.attr! :leaf
101     Primitive.cexpr! 'rb_int_comp(self)'
102   end
104   # call-seq:
105   #   abs -> integer
106   #
107   # Returns the absolute value of +self+.
108   #
109   #   (-12345).abs # => 12345
110   #   -12345.abs   # => 12345
111   #   12345.abs    # => 12345
112   #
113   def abs
114     Primitive.attr! :leaf
115     Primitive.cexpr! 'rb_int_abs(self)'
116   end
118   # call-seq:
119   #   bit_length -> integer
120   #
121   # Returns the number of bits of the value of +self+,
122   # which is the bit position of the highest-order bit
123   # that is different from the sign bit
124   # (where the least significant bit has bit position 1).
125   # If there is no such bit (zero or minus one), returns zero.
126   #
127   # This method returns <tt>ceil(log2(self < 0 ? -self : self + 1))</tt>>.
128   #
129   #   (-2**1000-1).bit_length   # => 1001
130   #   (-2**1000).bit_length     # => 1000
131   #   (-2**1000+1).bit_length   # => 1000
132   #   (-2**12-1).bit_length     # => 13
133   #   (-2**12).bit_length       # => 12
134   #   (-2**12+1).bit_length     # => 12
135   #   -0x101.bit_length         # => 9
136   #   -0x100.bit_length         # => 8
137   #   -0xff.bit_length          # => 8
138   #   -2.bit_length             # => 1
139   #   -1.bit_length             # => 0
140   #   0.bit_length              # => 0
141   #   1.bit_length              # => 1
142   #   0xff.bit_length           # => 8
143   #   0x100.bit_length          # => 9
144   #   (2**12-1).bit_length      # => 12
145   #   (2**12).bit_length        # => 13
146   #   (2**12+1).bit_length      # => 13
147   #   (2**1000-1).bit_length    # => 1000
148   #   (2**1000).bit_length      # => 1001
149   #   (2**1000+1).bit_length    # => 1001
150   #
151   # For \Integer _n_,
152   # this method can be used to detect overflow in Array#pack:
153   #
154   #   if n.bit_length < 32
155   #     [n].pack('l') # No overflow.
156   #   else
157   #     raise 'Overflow'
158   #   end
159   #
160   def bit_length
161     Primitive.attr! :leaf
162     Primitive.cexpr! 'rb_int_bit_length(self)'
163   end
165   #  call-seq:
166   #    even? -> true or false
167   #
168   #  Returns +true+ if +self+ is an even number, +false+ otherwise.
169   def even?
170     Primitive.attr! :leaf
171     Primitive.cexpr! 'rb_int_even_p(self)'
172   end
174   #  call-seq:
175   #    integer? -> true
176   #
177   #  Since +self+ is already an \Integer, always returns +true+.
178   def integer?
179     true
180   end
182   alias magnitude abs
184   #  call-seq:
185   #    odd? -> true or false
186   #
187   #  Returns +true+ if +self+ is an odd number, +false+ otherwise.
188   def odd?
189     Primitive.attr! :leaf
190     Primitive.cexpr! 'rb_int_odd_p(self)'
191   end
193   #  call-seq:
194   #    ord -> self
195   #
196   #  Returns +self+;
197   #  intended for compatibility to character literals in Ruby 1.9.
198   def ord
199     self
200   end
202   #  call-seq:
203   #    size -> integer
204   #
205   #  Returns the number of bytes in the machine representation of +self+;
206   #  the value is system-dependent:
207   #
208   #    1.size             # => 8
209   #    -1.size            # => 8
210   #    2147483647.size    # => 8
211   #    (256**10 - 1).size # => 10
212   #    (256**20 - 1).size # => 20
213   #    (256**40 - 1).size # => 40
214   #
215   def size
216     Primitive.attr! :leaf
217     Primitive.cexpr! 'rb_int_size(self)'
218   end
220   # call-seq:
221   #   times {|i| ... } -> self
222   #   times            -> enumerator
223   #
224   # Calls the given block +self+ times with each integer in <tt>(0..self-1)</tt>:
225   #
226   #   a = []
227   #   5.times {|i| a.push(i) } # => 5
228   #   a                        # => [0, 1, 2, 3, 4]
229   #
230   # With no block given, returns an Enumerator.
231   def times
232     Primitive.attr! :inline_block
233     unless defined?(yield)
234       return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, int_dotimes_size)'
235     end
236     i = 0
237     while i < self
238       yield i
239       i = i.succ
240     end
241     self
242   end
244   #  call-seq:
245   #    to_i -> self
246   #
247   #  Returns +self+ (which is already an \Integer).
248   def to_i
249     self
250   end
252   #  call-seq:
253   #    to_int -> self
254   #
255   #  Returns +self+ (which is already an \Integer).
256   def to_int
257     self
258   end
260   # call-seq:
261   #   zero? -> true or false
262   #
263   # Returns +true+ if +self+ has a zero value, +false+ otherwise.
264   def zero?
265     Primitive.attr! :leaf
266     Primitive.cexpr! 'rb_int_zero_p(self)'
267   end
269   #  call-seq:
270   #    ceildiv(numeric) -> integer
271   #
272   #  Returns the result of division +self+ by +numeric+.
273   #  rounded up to the nearest integer.
274   #
275   #    3.ceildiv(3)   # => 1
276   #    4.ceildiv(3)   # => 2
277   #
278   #    4.ceildiv(-3)  # => -1
279   #    -4.ceildiv(3)  # => -1
280   #    -4.ceildiv(-3) # => 2
281   #
282   #    3.ceildiv(1.2) # => 3
283   #
284   def ceildiv(other)
285     -div(0 - other)
286   end
288   #
289   # call-seq:
290   #   numerator -> self
291   #
292   # Returns +self+.
293   #
294   def numerator
295     self
296   end
298   # call-seq:
299   #   denominator -> 1
300   #
301   # Returns +1+.
302   def denominator
303     1
304   end
307 class Float
309   # call-seq:
310   #   to_f -> self
311   #
312   #  Returns +self+ (which is already a \Float).
313   def to_f
314     self
315   end
317   #  call-seq:
318   #    float.abs ->  float
319   #
320   #  Returns the absolute value of +self+:
321   #
322   #    (-34.56).abs # => 34.56
323   #    -34.56.abs   # => 34.56
324   #    34.56.abs    # => 34.56
325   #
326   def abs
327     Primitive.attr! :leaf
328     Primitive.cexpr! 'rb_float_abs(self)'
329   end
331   def magnitude
332     Primitive.attr! :leaf
333     Primitive.cexpr! 'rb_float_abs(self)'
334   end
336   # call-seq:
337   #   -float -> float
338   #
339   # Returns +self+, negated.
340   #
341   def -@
342     Primitive.attr! :leaf
343     Primitive.cexpr! 'rb_float_uminus(self)'
344   end
346   #  call-seq:
347   #    zero? -> true or false
348   #
349   #  Returns +true+ if +self+ is 0.0, +false+ otherwise.
350   def zero?
351     Primitive.attr! :leaf
352     Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
353   end
355   #  call-seq:
356   #    positive? -> true or false
357   #
358   #  Returns +true+ if +self+ is greater than 0, +false+ otherwise.
359   def positive?
360     Primitive.attr! :leaf
361     Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
362   end
364   #  call-seq:
365   #    negative? -> true or false
366   #
367   #  Returns +true+ if +self+ is less than 0, +false+ otherwise.
368   def negative?
369     Primitive.attr! :leaf
370     Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
371   end