Feature #13420 ยป 0001Integerroundfloorceiltruncate.patch
numeric.c  

5053  5053 
* When the precision is negative, the returned value is an integer 
5054  5054 
* with at least <code>ndigits.abs</code> trailing zeros. 
5055  5055 
* 
5056 
* Returns a floating point number when +ndigits+ is positive, 

5057 
* +self+ for zero, and an integer for negative. 

5056 
* Returns +self+ when +ndigits+ is zero or positive. 

5058  5057 
* 
5059  5058 
* 1.round #=> 1 
5060 
* 1.round(2) #=> 1.0


5059 
* 1.round(2) #=> 1 

5061  5060 
* 15.round(1) #=> 20 
5062  5061 
* (15).round(1) #=> 20 
5063  5062 
* 
...  ...  
5085  5084 
if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num; 
5086  5085 
ndigits = NUM2INT(nd); 
5087  5086 
mode = rb_num_get_rounding_option(opt); 
5088 
if (ndigits > 0) { 

5089 
return rb_Float(num); 

5090 
} 

5091 
if (ndigits == 0) { 

5087 
if (ndigits >= 0) { 

5092  5088 
return num; 
5093  5089 
} 
5094  5090 
return rb_int_round(num, ndigits, mode); 
...  ...  
5105  5101 
* When the precision is negative, the returned value is an integer 
5106  5102 
* with at least <code>ndigits.abs</code> trailing zeros. 
5107  5103 
* 
5108 
* Returns a floating point number when +ndigits+ is positive, 

5109 
* +self+ for zero, and an integer for negative. 

5104 
* Returns +self+ when +ndigits+ is zero or positive. 

5110  5105 
* 
5111  5106 
* 1.floor #=> 1 
5112 
* 1.floor(2) #=> 1.0


5107 
* 1.floor(2) #=> 1 

5113  5108 
* 18.floor(1) #=> 10 
5114  5109 
* (18).floor(1) #=> 20 
5115  5110 
*/ 
...  ...  
5121  5116  
5122  5117 
if (!rb_check_arity(argc, 0, 1)) return num; 
5123  5118 
ndigits = NUM2INT(argv[0]); 
5124 
if (ndigits > 0) { 

5125 
return rb_Float(num); 

5126 
} 

5127 
if (ndigits == 0) { 

5119 
if (ndigits >= 0) { 

5128  5120 
return num; 
5129  5121 
} 
5130  5122 
return rb_int_floor(num, ndigits); 
...  ...  
5141  5133 
* When the precision is negative, the returned value is an integer 
5142  5134 
* with at least <code>ndigits.abs</code> trailing zeros. 
5143  5135 
* 
5144 
* Returns a floating point number when +ndigits+ is positive, 

5145 
* +self+ for zero, and an integer for negative. 

5136 
* Returns +self+ when +ndigits+ is zero or positive. 

5146  5137 
* 
5147  5138 
* 1.ceil #=> 1 
5148 
* 1.ceil(2) #=> 1.0


5139 
* 1.ceil(2) #=> 1 

5149  5140 
* 18.ceil(1) #=> 20 
5150  5141 
* (18).ceil(1) #=> 10 
5151  5142 
*/ 
...  ...  
5157  5148  
5158  5149 
if (!rb_check_arity(argc, 0, 1)) return num; 
5159  5150 
ndigits = NUM2INT(argv[0]); 
5160 
if (ndigits > 0) { 

5161 
return rb_Float(num); 

5162 
} 

5163 
if (ndigits == 0) { 

5151 
if (ndigits >= 0) { 

5164  5152 
return num; 
5165  5153 
} 
5166  5154 
return rb_int_ceil(num, ndigits); 
...  ...  
5177  5165 
* When the precision is negative, the returned value is an integer 
5178  5166 
* with at least <code>ndigits.abs</code> trailing zeros. 
5179  5167 
* 
5180 
* Returns a floating point number when +ndigits+ is positive, 

5181 
* +self+ for zero, and an integer for negative. 

5168 
* Returns +self+ when +ndigits+ is zero or positive. 

5182  5169 
* 
5183  5170 
* 1.truncate #=> 1 
5184 
* 1.truncate(2) #=> 1.0


5171 
* 1.truncate(2) #=> 1 

5185  5172 
* 18.truncate(1) #=> 10 
5186  5173 
* (18).truncate(1) #=> 10 
5187  5174 
*/ 
...  ...  
5193  5180  
5194  5181 
if (!rb_check_arity(argc, 0, 1)) return num; 
5195  5182 
ndigits = NUM2INT(argv[0]); 
5196 
if (ndigits > 0) { 

5197 
return rb_Float(num); 

5198 
} 

5199 
if (ndigits == 0) { 

5183 
if (ndigits >= 0) { 

5200  5184 
return num; 
5201  5185 
} 
5202  5186 
return rb_int_truncate(num, ndigits); 
test/ruby/test_integer.rb  

181  181 
assert_int_equal(11111, 11111.round) 
182  182 
assert_int_equal(11111, 11111.round(0)) 
183  183  
184 
assert_float_equal(11111.0, 11111.round(1))


185 
assert_float_equal(11111.0, 11111.round(2))


184 
assert_int_equal(11111, 11111.round(1))


185 
assert_int_equal(11111, 11111.round(2))


186  186  
187  187 
assert_int_equal(11110, 11111.round(1)) 
188  188 
assert_int_equal(11100, 11111.round(2)) 
...  ...  
249  249  
250  250 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(1)) 
251  251 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, (1111_1111_1111_1111_1111_1111_1111_1111).round(1)) 
252  
253 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.round(1)) 

254 
assert_int_equal(10**400, (10**400).round(1)) 

252  255 
end 
253  256  
254  257 
def test_floor 
255  258 
assert_int_equal(11111, 11111.floor) 
256  259 
assert_int_equal(11111, 11111.floor(0)) 
257  260  
258 
assert_float_equal(11111.0, 11111.floor(1))


259 
assert_float_equal(11111.0, 11111.floor(2))


261 
assert_int_equal(11111, 11111.floor(1))


262 
assert_int_equal(11111, 11111.floor(2))


260  263  
261  264 
assert_int_equal(11110, 11110.floor(1)) 
262  265 
assert_int_equal(11110, 11119.floor(1)) 
...  ...  
274  277  
275  278 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(1)) 
276  279 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, (1111_1111_1111_1111_1111_1111_1111_1111).floor(1)) 
280  
281 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.floor(1)) 

282 
assert_int_equal(10**400, (10**400).floor(1)) 

277  283 
end 
278  284  
279  285 
def test_ceil 
280  286 
assert_int_equal(11111, 11111.ceil) 
281  287 
assert_int_equal(11111, 11111.ceil(0)) 
282  288  
283 
assert_float_equal(11111.0, 11111.ceil(1))


284 
assert_float_equal(11111.0, 11111.ceil(2))


289 
assert_int_equal(11111, 11111.ceil(1))


290 
assert_int_equal(11111, 11111.ceil(2))


285  291  
286  292 
assert_int_equal(11110, 11110.ceil(1)) 
287  293 
assert_int_equal(11120, 11119.ceil(1)) 
...  ...  
299  305  
300  306 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(1)) 
301  307 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, (1111_1111_1111_1111_1111_1111_1111_1111).ceil(1)) 
308  
309 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(1)) 

310 
assert_int_equal(10**400, (10**400).ceil(1)) 

302  311 
end 
303  312  
304  313 
def test_truncate 
305  314 
assert_int_equal(11111, 11111.truncate) 
306  315 
assert_int_equal(11111, 11111.truncate(0)) 
307  316  
308 
assert_float_equal(11111.0, 11111.truncate(1))


309 
assert_float_equal(11111.0, 11111.truncate(2))


317 
assert_int_equal(11111, 11111.truncate(1))


318 
assert_int_equal(11111, 11111.truncate(2))


310  319  
311  320 
assert_int_equal(11110, 11110.truncate(1)) 
312  321 
assert_int_equal(11110, 11119.truncate(1)) 
...  ...  
324  333  
325  334 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(1)) 
326  335 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, (1111_1111_1111_1111_1111_1111_1111_1111).truncate(1)) 
336  
337 
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(1)) 

338 
assert_int_equal(10**400, (10**400).truncate(1)) 

327  339 
end 
328  340  
329  341 
MimicInteger = Struct.new(:to_int) 