[ruby/etc] bump up to 1.3.1
[ruby-80x24.org.git] / numeric.rb
blob9f2200d2a87642de1d35a48af3274fc1cbe93382
1 class Numeric
2   #
3   #  call-seq:
4   #     num.real?  ->  true or false
5   #
6   #  Returns +true+ if +num+ is a real number (i.e. not Complex).
7   #
8   def real?
9     return true
10   end
12   #
13   #  call-seq:
14   #     num.integer?  ->  true or false
15   #
16   #  Returns +true+ if +num+ is an Integer.
17   #
18   #      1.0.integer?   #=> false
19   #      1.integer?     #=> true
20   #
21   def integer?
22     return false
23   end
25   #
26   #  call-seq:
27   #     num.finite?  ->  true or false
28   #
29   #  Returns +true+ if +num+ is a finite number, otherwise returns +false+.
30   #
31   def finite?
32     return true
33   end
35   #
36   #  call-seq:
37   #     num.infinite?  ->  -1, 1, or nil
38   #
39   #  Returns +nil+, -1, or 1 depending on whether the value is
40   #  finite, <code>-Infinity</code>, or <code>+Infinity</code>.
41   #
42   def infinite?
43     return nil
44   end
45 end
47 class Integer
48   # call-seq:
49   #    -int  ->  integer
50   #
51   # Returns +int+, negated.
52   def -@
53     Primitive.attr! 'inline'
54     Primitive.cexpr! 'rb_int_uminus(self)'
55   end
57   # call-seq:
58   #   ~int  ->  integer
59   #
60   # One's complement: returns a number where each bit is flipped.
61   #
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.
66   #
67   #   sprintf("%X", ~0x1122334455)    #=> "..FEEDDCCBBAA"
68   def ~
69     Primitive.attr! 'inline'
70     Primitive.cexpr! 'rb_int_comp(self)'
71   end
73   # call-seq:
74   #    int.abs        ->  integer
75   #    int.magnitude  ->  integer
76   #
77   # Returns the absolute value of +int+.
78   #
79   #    (-12345).abs   #=> 12345
80   #    -12345.abs     #=> 12345
81   #    12345.abs      #=> 12345
82   #
83   # Integer#magnitude is an alias for Integer#abs.
84   def abs
85     Primitive.attr! 'inline'
86     Primitive.cexpr! 'rb_int_abs(self)'
87   end
89   # call-seq:
90   #    int.bit_length  ->  integer
91   #
92   # Returns the number of bits of the value of +int+.
93   #
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.
98   #
99   # I.e. this method returns <i>ceil(log2(int < 0 ? -int : int+1))</i>.
100   #
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
112   #    0.bit_length              #=> 0
113   #    1.bit_length              #=> 1
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
122   #
123   # This method can be used to detect overflow in Array#pack as follows:
124   #
125   #    if n.bit_length < 32
126   #      [n].pack("l") # no overflow
127   #    else
128   #      raise "overflow"
129   #    end
130   def bit_length
131     Primitive.attr! 'inline'
132     Primitive.cexpr! 'rb_int_bit_length(self)'
133   end
135   #  call-seq:
136   #     int.even?  ->  true or false
137   #
138   #  Returns +true+ if +int+ is an even number.
139   def even?
140     Primitive.attr! 'inline'
141     Primitive.cexpr! 'rb_int_even_p(self)'
142   end
144   #  call-seq:
145   #     int.integer?  ->  true
146   #
147   #  Since +int+ is already an Integer, this always returns +true+.
148   def integer?
149     return true
150   end
152   alias magnitude abs
153 =begin
154   def magnitude
155     Primitive.attr! 'inline'
156     Primitive.cexpr! 'rb_int_abs(self)'
157   end
158 =end
160   #  call-seq:
161   #     int.odd?  ->  true or false
162   #
163   #  Returns +true+ if +int+ is an odd number.
164   def odd?
165     Primitive.attr! 'inline'
166     Primitive.cexpr! 'rb_int_odd_p(self)'
167   end
169   #  call-seq:
170   #     int.ord  ->  self
171   #
172   #  Returns the +int+ itself.
173   #
174   #     97.ord   #=> 97
175   #
176   #  This method is intended for compatibility to character literals
177   #  in Ruby 1.9.
178   #
179   #  For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
180   def ord
181     return self
182   end
184   #
185   #  Document-method: Integer#size
186   #  call-seq:
187   #     int.size  ->  int
188   #
189   #  Returns the number of bytes in the machine representation of +int+
190   #  (machine dependent).
191   #
192   #     1.size               #=> 8
193   #     -1.size              #=> 8
194   #     2147483647.size      #=> 8
195   #     (256**10 - 1).size   #=> 10
196   #     (256**20 - 1).size   #=> 20
197   #     (256**40 - 1).size   #=> 40
198   #
199   def size
200     Primitive.attr! 'inline'
201     Primitive.cexpr! 'rb_int_size(self)'
202   end
204   #  call-seq:
205   #     int.to_i    ->  integer
206   #
207   #  Since +int+ is already an Integer, returns +self+.
208   #
209   #  #to_int is an alias for #to_i.
210   def to_i
211     return self
212   end
214   #  call-seq:
215   #     int.to_int  ->  integer
216   #
217   #  Since +int+ is already an Integer, returns +self+.
218   def to_int
219     return self
220   end
222   # call-seq:
223   #    int.zero? -> true or false
224   #
225   # Returns +true+ if +int+ has a zero value.
226   def zero?
227     Primitive.attr! 'inline'
228     Primitive.cexpr! 'rb_int_zero_p(self)'
229   end
232 #  call-seq:
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)
248 =begin
249   Primitive.attr! 'inline'
250   Primitive.cexpr! 'rb_check_integer_type(num)'
251 =end
252 end if false
254 class Float
255   #
256   # call-seq:
257   #    float.to_f  ->  self
258   #
259   # Since +float+ is already a Float, returns +self+.
260   #
261   def to_f
262     return self
263   end
265   #
266   #  call-seq:
267   #     float.abs        ->  float
268   #     float.magnitude  ->  float
269   #
270   #  Returns the absolute value of +float+.
271   #
272   #     (-34.56).abs   #=> 34.56
273   #     -34.56.abs     #=> 34.56
274   #     34.56.abs      #=> 34.56
275   #
276   #  Float#magnitude is an alias for Float#abs.
277   #
278   def abs
279     Primitive.attr! 'inline'
280     Primitive.cexpr! 'rb_float_abs(self)'
281   end
283   def magnitude
284     Primitive.attr! 'inline'
285     Primitive.cexpr! 'rb_float_abs(self)'
286   end
288   #
289   # call-seq:
290   #    -float  ->  float
291   #
292   # Returns +float+, negated.
293   #
294   def -@
295     Primitive.attr! 'inline'
296     Primitive.cexpr! 'rb_float_uminus(self)'
297   end
299   #
300   #  call-seq:
301   #     float.zero?  ->  true or false
302   #
303   #  Returns +true+ if +float+ is 0.0.
304   #
305   def zero?
306     Primitive.attr! 'inline'
307     Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
308   end
310   #
311   #  call-seq:
312   #     float.positive?  ->  true or false
313   #
314   #  Returns +true+ if +float+ is greater than 0.
315   #
316   def positive?
317     Primitive.attr! 'inline'
318     Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
319   end
321   #
322   #  call-seq:
323   #     float.negative?  ->  true or false
324   #
325   #  Returns +true+ if +float+ is less than 0.
326   #
327   def negative?
328     Primitive.attr! 'inline'
329     Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
330   end