File tree Expand file tree Collapse file tree 2 files changed +26
-8
lines changed
src/main/ruby/truffleruby/core Expand file tree Collapse file tree 2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change 67
67
25 . round ( -1 , half : :up ) . should eql ( 30 )
68
68
25 . round ( -1 , half : :down ) . should eql ( 20 )
69
69
25 . round ( -1 , half : :even ) . should eql ( 20 )
70
+ 25 . round ( -1 , half : nil ) . should eql ( 30 )
70
71
35 . round ( -1 , half : :up ) . should eql ( 40 )
71
72
35 . round ( -1 , half : :down ) . should eql ( 30 )
72
73
35 . round ( -1 , half : :even ) . should eql ( 40 )
74
+ 35 . round ( -1 , half : nil ) . should eql ( 40 )
73
75
( -25 ) . round ( -1 , half : :up ) . should eql ( -30 )
74
76
( -25 ) . round ( -1 , half : :down ) . should eql ( -20 )
75
77
( -25 ) . round ( -1 , half : :even ) . should eql ( -20 )
78
+ ( -25 ) . round ( -1 , half : nil ) . should eql ( -30 )
76
79
end
77
80
78
81
ruby_version_is "2.4" ..."2.5" do
90
93
35 . round ( 1 , half : :even ) . should eql ( 35 )
91
94
end
92
95
end
96
+
97
+ it "raises ArgumentError for an unknown rounding mode" do
98
+ lambda { 42 . round ( -1 , half : :foo ) } . should raise_error ( ArgumentError , /invalid rounding mode: foo/ )
99
+ lambda { 42 . round ( 1 , half : :foo ) } . should raise_error ( ArgumentError , /invalid rounding mode: foo/ )
100
+ end
93
101
end
Original file line number Diff line number Diff line change @@ -161,7 +161,7 @@ def chr(enc=undefined)
161
161
String . from_codepoint self , enc
162
162
end
163
163
164
- def round ( ndigits = undefined , half : nil )
164
+ def round ( ndigits = undefined , half : :up )
165
165
return self if undefined . equal? ndigits
166
166
167
167
if Float === ndigits && ndigits . infinite?
@@ -172,7 +172,12 @@ def round(ndigits=undefined, half: nil)
172
172
Truffle ::Type . check_int ( ndigits )
173
173
174
174
if ndigits >= 0
175
- self
175
+ case half
176
+ when :up , nil , :down , :even
177
+ self
178
+ else
179
+ raise ArgumentError , "invalid rounding mode: #{ half } "
180
+ end
176
181
else
177
182
ndigits = -ndigits
178
183
@@ -185,12 +190,17 @@ def round(ndigits=undefined, half: nil)
185
190
186
191
if kind_of? Integer and f . kind_of? Integer
187
192
x = self < 0 ? -self : self
188
- x = if ( half == :down )
189
- x / f
190
- else # :up or nil
191
- ( x + f / 2 ) / f
192
- end
193
- x = ( x / 2 ) * 2 if half == :even
193
+ case half
194
+ when :up , nil
195
+ x = ( x + ( f / 2 ) ) / f
196
+ when :down
197
+ x = x / f
198
+ when :even
199
+ x = ( x + ( f / 2 ) ) / f
200
+ x = ( x / 2 ) * 2
201
+ else
202
+ raise ArgumentError , "invalid rounding mode: #{ half } "
203
+ end
194
204
x = x * f
195
205
x = -x if self < 0
196
206
return x
You can’t perform that action at this time.
0 commit comments