public RubyNumeric roundShared(ThreadContext context, int ndigits, RoundingMode roundingMode) { Ruby runtime = context.runtime; RubyNumeric f, h, n, r; if (int_round_zero_p(context, ndigits)) { return RubyFixnum.zero(runtime); } f = (RubyNumeric) int_pow(context, 10, -ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = fix2long(this), y = fix2long(f); boolean neg = x < 0; if (neg) x = -x; x = doRound(context, roundingMode, x, y); if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } if (f instanceof RubyFloat) { /* then int_pow overflow */ return RubyFixnum.zero(runtime); } h = (RubyNumeric) f.idiv(context, 2); r = (RubyNumeric) this.op_mod(context, f); n = (RubyNumeric) this.op_minus(context, r); r = (RubyNumeric) r.op_cmp(context, h); if (r.isPositive(context).isTrue() || (r.isZero() && doRoundCheck(context, roundingMode, this, n, f))) { n = (RubyNumeric) n.op_plus(context, f); } return n; }
public RubyNumeric roundShared(ThreadContext context, int ndigits, RoundingMode roundingMode) { Ruby runtime = context.runtime; RubyNumeric f, h, n, r; if (int_round_zero_p(context, ndigits)) { return RubyFixnum.zero(runtime); } f = (RubyNumeric) int_pow(context, 10, -ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = fix2long(this), y = fix2long(f); boolean neg = x < 0; if (neg) x = -x; x = doRound(context, roundingMode, x, y); if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } if (f instanceof RubyFloat) { /* then int_pow overflow */ return RubyFixnum.zero(runtime); } h = (RubyNumeric) f.idiv(context, 2); r = (RubyNumeric) this.op_mod(context, f); n = (RubyNumeric) this.op_minus(context, r); r = (RubyNumeric) r.op_cmp(context, h); if (r.isPositive(context).isTrue() || (r.isZero() && doRoundCheck(context, roundingMode, this, n, f))) { n = (RubyNumeric) n.op_plus(context, f); } return n; }