source: EcnlProtoTool/trunk/mruby-1.2.0/mrblib/numeric.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 2.9 KB
Line 
1##
2# Numeric
3#
4# ISO 15.2.7
5class Numeric
6 include Comparable
7 ##
8 # Returns the receiver simply.
9 #
10 # ISO 15.2.7.4.1
11 def +@
12 self
13 end
14
15 ##
16 # Returns the receiver's value, negated.
17 #
18 # ISO 15.2.7.4.2
19 def -@
20 0 - self
21 end
22
23 ##
24 # Returns the absolute value of the receiver.
25 #
26 # ISO 15.2.7.4.3
27 def abs
28 if self < 0
29 -self
30 else
31 self
32 end
33 end
34end
35
36##
37# Integral
38#
39# mruby special - module to share methods between Floats and Integers
40# to make them compatible
41module Integral
42 ##
43 # Calls the given block once for each Integer
44 # from +self+ downto +num+.
45 #
46 # ISO 15.2.8.3.15
47 def downto(num, &block)
48 return to_enum(:downto, num) unless block_given?
49
50 i = self.to_i
51 while i >= num
52 block.call(i)
53 i -= 1
54 end
55 self
56 end
57
58 ##
59 # Returns self + 1
60 #
61 # ISO 15.2.8.3.19
62 def next
63 self + 1
64 end
65 # ISO 15.2.8.3.21
66 alias succ next
67
68 ##
69 # Calls the given block +self+ times.
70 #
71 # ISO 15.2.8.3.22
72 def times &block
73 return to_enum :times unless block_given?
74
75 i = 0
76 while i < self
77 block.call i
78 i += 1
79 end
80 self
81 end
82
83 ##
84 # Calls the given block once for each Integer
85 # from +self+ upto +num+.
86 #
87 # ISO 15.2.8.3.27
88 def upto(num, &block)
89 return to_enum(:upto, num) unless block_given?
90
91 i = self.to_i
92 while i <= num
93 block.call(i)
94 i += 1
95 end
96 self
97 end
98
99 ##
100 # Calls the given block from +self+ to +num+
101 # incremented by +step+ (default 1).
102 #
103 def step(num, step = 1, &block)
104 raise ArgumentError, "step can't be 0" if step == 0
105 return to_enum(:step, num, step) unless block_given?
106
107 i = if num.kind_of? Float then self.to_f else self end
108 if step > 0
109 while i <= num
110 block.call(i)
111 i += step
112 end
113 else
114 while i >= num
115 block.call(i)
116 i += step
117 end
118 end
119 self
120 end
121end
122
123##
124# Integer
125#
126# ISO 15.2.8
127class Integer
128 include Integral
129 ##
130 # Returns the receiver simply.
131 #
132 # ISO 15.2.8.3.14
133 def ceil
134 self
135 end
136
137 ##
138 # Returns the receiver simply.
139 #
140 # ISO 15.2.8.3.17
141 def floor
142 self
143 end
144
145 ##
146 # Returns the receiver simply.
147 #
148 # ISO 15.2.8.3.24
149 alias round floor
150
151 ##
152 # Returns the receiver simply.
153 #
154 # ISO 15.2.8.3.26
155 alias truncate floor
156end
157
158##
159# Float
160#
161# ISO 15.2.9
162class Float
163 include Integral
164 # mruby special - since mruby integers may be upgraded to floats,
165 # floats should be compatible to integers.
166 def >> other
167 n = self.to_i
168 other = other.to_i
169 if other < 0
170 n << -other
171 else
172 other.times { n /= 2 }
173 if n.abs < 1
174 if n >= 0
175 0
176 else
177 -1
178 end
179 else
180 n.to_i
181 end
182 end
183 end
184 def << other
185 n = self.to_i
186 other = other.to_i
187 if other < 0
188 n >> -other
189 else
190 other.times { n *= 2 }
191 n
192 end
193 end
194end
Note: See TracBrowser for help on using the repository browser.