source: EcnlProtoTool/trunk/mruby-1.3.0/mrblib/numeric.rb@ 331

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 2.6 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=nil, 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 num == nil
109 while true
110 block.call(i)
111 i+=step
112 end
113 return self
114 end
115 if step > 0
116 while i <= num
117 block.call(i)
118 i += step
119 end
120 else
121 while i >= num
122 block.call(i)
123 i += step
124 end
125 end
126 self
127 end
128end
129
130##
131# Integer
132#
133# ISO 15.2.8
134class Integer
135 include Integral
136 ##
137 # Returns the receiver simply.
138 #
139 # ISO 15.2.8.3.14
140 def ceil
141 self
142 end
143
144 ##
145 # Returns the receiver simply.
146 #
147 # ISO 15.2.8.3.17
148 def floor
149 self
150 end
151
152 ##
153 # Returns the receiver simply.
154 #
155 # ISO 15.2.8.3.24
156 alias round floor
157
158 ##
159 # Returns the receiver simply.
160 #
161 # ISO 15.2.8.3.26
162 alias truncate floor
163end
164
165##
166# Float
167#
168# ISO 15.2.9
169class Float
170 # mruby special - since mruby integers may be upgraded to floats,
171 # floats should be compatible to integers.
172 include Integral
173end
Note: See TracBrowser for help on using the repository browser.