forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbignum.rb
198 lines (155 loc) · 3.92 KB
/
bignum.rb
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
class Bignum < Integer
# unary operators
def ~
Rubinius.primitive :bignum_invert
raise PrimitiveFailure, "Bignum#~ primitive failed"
end
def -@
Rubinius.primitive :bignum_neg
raise PrimitiveFailure, "Bignum#-@ primitive failed"
end
# binary math operators
def +(o)
Rubinius.primitive :bignum_add
redo_coerced :+, o
end
def -(o)
Rubinius.primitive :bignum_sub
redo_coerced :-, o
end
def *(o)
Rubinius.primitive :bignum_mul
redo_coerced :*, o
end
def bit_length
Rubinius.invoke_primitive :bignum_bit_length, self > 0 ? self : ~self
end
# this method is aliased to / in core
# see README-DEVELOPERS regarding safe math compiler plugin
def divide(o)
Rubinius.primitive :bignum_div
redo_coerced :/, o
end
# This is separate from divide because it calls a different method
# if it has to coerce. This is important because Bignum#div(Float) and
# Bignum#/(Float) return different things.
#
def div(other)
Rubinius.primitive :bignum_div
redo_coerced :div, other
end
def %(other)
Rubinius.primitive :bignum_mod
redo_coerced :%, other
end
def divmod(other)
Rubinius.primitive :bignum_divmod
redo_coerced :divmod, other
end
# bitwise binary operators
def &(o)
Rubinius.primitive :bignum_and
super(o)
end
def |(o)
Rubinius.primitive :bignum_or
super(o)
end
def ^(o)
Rubinius.primitive :bignum_xor
super(o)
end
def <<(other)
Rubinius.primitive :bignum_left_shift
other = Rubinius::Type.coerce_to other, Integer, :to_int
unless other.kind_of? Fixnum
raise RangeError, "argument is out of range for a Fixnum"
end
self << other
end
def >>(other)
Rubinius.primitive :bignum_right_shift
other = Rubinius::Type.coerce_to other, Integer, :to_int
unless other.kind_of? Fixnum
raise RangeError, "argument is out of range for a Fixnum"
end
self >> other
end
# comparison operators
def <(other)
Rubinius.primitive :bignum_lt
redo_compare :<, other
end
def <=(other)
Rubinius.primitive :bignum_le
redo_compare :<=, other
end
def >(other)
Rubinius.primitive :bignum_gt
redo_compare :>, other
end
def >=(other)
Rubinius.primitive :bignum_ge
redo_compare :>=, other
end
def ==(o)
Rubinius.primitive :bignum_equal
# This is to make sure the return value is true or false
o == self ? true : false
end
def <=>(other)
Rubinius.primitive :bignum_compare
# We do not use redo_compare here because Ruby does not
# raise if any part of the coercion or comparison raises
# an exception.
begin
coerced = other.coerce self
return nil unless coerced
coerced[0] <=> coerced[1]
rescue
return nil
end
end
# conversions
def self.from_float(value)
Rubinius.primitive :bignum_from_float
raise PrimitiveFailure, "Bignum.from_float primitive failed"
end
def coerce(other)
Rubinius.primitive :bignum_coerce
super other
end
def to_f
Rubinius.primitive :bignum_to_float
raise PrimitiveFailure, "Bignum#to_f primitive failed"
end
def to_s(base=10)
Rubinius.invoke_primitive :bignum_to_s, self, base
end
# We do not alias this to #to_s in case someone overrides #to_s.
def inspect
Rubinius.invoke_primitive :bignum_to_s, self, 10
end
def size
Rubinius.primitive :bignum_size
raise PrimitiveFailure, "Bignum#size primitive failed"
end
# see README-DEVELOPERS regarding safe math compiler plugin
alias_method :/, :divide
def eql?(value)
value.is_a?(Bignum) && self == value
end
alias_method :modulo, :%
def fdiv(n)
to_f / n
end
def **(o)
Rubinius.primitive :bignum_pow
if o.is_a?(Float) && self < 0 && o != o.round
return Complex.new(self, 0) ** o
elsif o.is_a?(Integer) && o < 0
return Rational.new(self, 1) ** o
end
redo_coerced :**, o
end
end