Re-enable spec/library for full CI runs.
[rbx.git] / kernel / core / bignum.rb
blob48ce564ab98f1a51dc5a45c35e08e58da3212306
1 # depends on: class.rb integer.rb
3 ##
4 # Bignum objects hold integers outside the range of Fixnum. Bignum objects are
5 # created automatically when integer calculations would otherwise overflow a
6 # Fixnum. When a calculation involving Bignum objects returns a result that
7 # will fit in a Fixnum, the result is automatically converted.
9 # For the purposes of the bitwise operations and [], a Bignum is treated as if
10 # it were an infinite-length bitstring with 2's complement representation.
12 # While Fixnum values are immediate, Bignum objects are not. Assignment and
13 # parameter passing work with references to objects, not the objects
14 # themselves.
16 class Bignum < Integer
18   # see README-DEVELOPERS regarding safe math compiler plugin
19   alias_method :/, :divide
21   def %(other)
22     if other.kind_of?(Float)
23       if other == 0.0
24         return 0 / 0.0
25       else
26         return self.to_f % other
27       end
28     end
30     raise TypeError unless other.kind_of?(Numeric)
31     raise ZeroDivisionError if other == 0
33     if self == 0 || self.abs == other.abs
34       0
35     else
36       mod_primitive(other)#.to_int)
37     end
38   end
40   def >>(s)
41     s = Type.coerce_to(s, Integer, :to_int)
42     return self << -s if s < 0
43     unless s.is_a?(Fixnum)
44       return  0 if self >= 0
45       return -1 if self <  0
46     end
48     __bignum_right_shift__(s) 
49   end
51   def <<(s)
52     s = Type.coerce_to(s, Integer, :to_int)
53     raise RangeError, "Object is out of range for a Fixnum" unless s.is_a?(Fixnum)
54     s < 0 ? __bignum_right_shift__(-s) : __bignum_left_shift__(s) 
55   end
56   
57   def eql?(value)
58     return false unless value.is_a?(Bignum)
60     self == value
61   end
63   alias_method :modulo, :%
64   
65   alias_method :div, :/
67   private :radix_to_s # in kernel/bootstrap/bignum.rb
68 end