public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
Factor out some repetition.
Marnen Laibow-Koser (author)
Mon Apr 28 08:09:29 -0700 2008
commit  6c811f8a9fd4c8f78fb470e2d945beabc202b2c7
tree    0f1a2e06e2368eb2f85d96720d43d20cc2b8b4d9
parent  2172e2ac20b69a97c2ad66551b3620a43bfda700
...
220
221
222
223
224
225
226
227
228
229
230
 
231
232
233
...
256
257
258
259
260
261
262
263
264
 
 
265
266
267
...
420
421
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
...
220
221
222
 
 
 
 
 
 
 
 
223
224
225
226
...
249
250
251
 
 
 
 
 
 
252
253
254
255
256
...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
0
@@ -220,14 +220,7 @@ class BigDecimal < Numeric
0
     elsif !other.finite?
0
       return other
0
     elsif self.exponent == other.exponent
0
- sd = self.digits.to_s
0
- od = other.digits.to_s
0
- diff = sd.length - od.length
0
- if diff > 0
0
- od << '0' * diff
0
- else
0
- sd << '0' * diff.abs
0
- end
0
+ sd, od = self.align(other)
0
       sum = (sd.to_i * signs[self.sign]) + (od.to_i * signs[other.sign])
0
       s = sum.abs.to_s
0
       sumdiff = s.length - sd.length
0
@@ -256,12 +249,8 @@ class BigDecimal < Numeric
0
         nzd = ('0' * nzx.abs) + nzd
0
       end
0
       
0
- diff = zd.length - nzd.length
0
- if diff > 0
0
- nzd << '0' * diff
0
- else # diff < 0
0
- zd << '0' * diff.abs
0
- end
0
+ zd, nzd = BigDecimal.align(zd, nzd)
0
+
0
       l = zd.length
0
       sum = (nzd.to_i * signs[nz.sign]) + (zd.to_i * signs[z.sign])
0
       sumsign = sum < 0 ? MINUS : PLUS
0
@@ -420,4 +409,37 @@ class BigDecimal < Numeric
0
       BigDecimal(@sign + '0' + RADIX + s + EXP + @exp.to_s)
0
     end
0
   end
0
+
0
+ ############################
0
+ # Internal utility methods #
0
+ ############################
0
+
0
+ protected
0
+
0
+ # Takes two BigDecimals and returns an array of their digit strings,
0
+ # with the shorter one right-padded with zeros so they're the same length.
0
+ # Can also take a digit string itself.
0
+ # call-seq:
0
+ # BigDecimal("12").align(BigDecimal("0.0056789")) => ["12000", "56789"]
0
+ # BigDecimal("8.765").align("43") => ["8765", "4300"]
0
+ def align(y) #:nodoc:
0
+ xd = self.digits.to_s
0
+ yd = y.kind_of?(BigDecimal) ? y.digits.to_s : y
0
+ BigDecimal.align(xd, yd)
0
+ end
0
+
0
+ # Like BigDecimal#align, but can take two digit strings.
0
+ # call-seq:
0
+ # BigDecimal.align("8765", "43") => ["8765", "4300"]
0
+ def self.align(x, y) #:nodoc:
0
+ xd = x.clone
0
+ yd = y.clone
0
+ diff = xd.length - yd.length
0
+ if diff > 0
0
+ yd << '0' * diff
0
+ else
0
+ xd << '0' * diff.abs
0
+ end
0
+ [xd, yd]
0
+ end
0
 end

Comments

    No one has commented yet.