-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathinteger.rb
87 lines (77 loc) · 1.29 KB
/
integer.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
class Integer < Numeric
def downto(limit, &block)
return self.enum_for(:downto, limit) unless block
current = self
while current >= limit
yield current
current -= 1
end
end
def times(&block)
return self.enum_for(:times) unless block
i = 0
while i < self
yield i
i += 1
end
self
end
def to_i
self
end
alias to_int to_i
alias ceil to_i
alias floor to_i
alias truncate to_i
alias ord to_i
alias numerator to_i
def denominator
1
end
def integer?
true
end
def next
return self + 1
end
alias succ next
def pred
return self - 1
end
def even?
(self % 2).zero?
end
def odd?
!even?
end
def round(*ndigits)
if ndigits.empty?
return self
end
ndigits = Topaz.convert_type(ndigits[0], Fixnum, :to_int)
if ndigits == 0
return self
end
if ndigits > 0
return Float(self)
end
bytes = self.size
if -0.415241 * ndigits - 0.125 > bytes
return 0
end
f = 10 ** -ndigits
if f.is_a?(Float)
return 0
end
h = f / 2
r = bytes % f
n = bytes - r
if ((bytes < 0 && r <= h) || r < h)
n = f + 1
end
return n
end
def bit_length
Math.log(self < 0 ? -self : self + 1, 2).ceil
end
end