public static long num2ulong(Ruby runtime, IRubyObject value) { if (value instanceof RubyFloat) { RubyBignum bignum = RubyBignum.newBignum(runtime, ((RubyFloat) value).getDoubleValue()); return RubyBignum.big2ulong(bignum); } else if (value instanceof RubyBignum) { return RubyBignum.big2ulong((RubyBignum) value); } else { return RubyNumeric.num2long(value); } }
long ret = (Long) value; return ret >= 0 ? runtime.newFixnum(ret) : RubyBignum.newBignum(runtime, UINT64_COMPLEMENTARY.add(new BigInteger(ret + ""))); case FLOAT: return runtime.newFloat((Float) value);
/** rb_big_abs * */ @Override public IRubyObject abs(ThreadContext context) { return RubyBignum.newBignum(context.runtime, value.abs()); }
public static IRubyObject newUnsigned64(Ruby runtime, long value) { return value < 0 ? RubyBignum.newBignum(runtime, BigInteger.valueOf(value & 0x7fffffffffffffffL).add(UINT64_BASE)) : RubyFixnum.newFixnum(runtime, value); }
/** rb_big_norm * */ public static RubyInteger bignorm(Ruby runtime, BigInteger bi) { return (bi.compareTo(LONG_MIN) < 0 || bi.compareTo(LONG_MAX) > 0) ? newBignum(runtime, bi) : runtime.newFixnum(bi.longValue()); }
private static RubyInteger toInteger(final Ruby runtime, final BigDecimal result) { if (result.compareTo(MAX_FIX) <= 0 && result.compareTo(MIN_FIX) >= 0) { return RubyFixnum.newFixnum(runtime, result.longValue()); } return RubyBignum.newBignum(runtime, result.toBigInteger()); }
private static RubyInteger toRubyInteger(final Ruby runtime, final Number i) { if (i instanceof BigInteger) { return RubyBignum.newBignum(runtime, (BigInteger) i); } return RubyFixnum.newFixnum(runtime, i.longValue()); }
public IRubyObject convert(Ruby runtime, Object object) { if (object == null) return runtime.getNil(); return RubyBignum.newBignum(runtime, (BigInteger)object); } public IRubyObject get(Ruby runtime, Object array, int i) {
private static RubyInteger negate(final Ruby runtime, final long value) { if (value == MIN) { // a gotcha return RubyBignum.newBignum(runtime, BigInteger.valueOf(value).negate()); } return RubyFixnum.newFixnum(runtime, -value); }
public static IRubyObject newUnsigned64(Ruby runtime, long value) { return value < 0 ? RubyBignum.newBignum(runtime, BigInteger.valueOf(value & 0x7fffffffffffffffL).add(UINT64_BASE)) : RubyFixnum.newFixnum(runtime, value); }
private static RubyInteger negate(final Ruby runtime, final long value) { if (value == MIN) { // a gotcha return RubyBignum.newBignum(runtime, BigInteger.valueOf(value).negate()); } return RubyFixnum.newFixnum(runtime, -value); }
public static IRubyObject newUnsigned64(ThreadContext context, long value) { return value < 0 ? RubyBignum.newBignum(context.runtime, BigInteger.valueOf(value & 0x7fffffffffffffffL).add(UINT64_BASE)) : RubyFixnum.newFixnum(context.runtime, value); }
private static RubyBignum randomBignum(final ThreadContext context, final Number lower, final BigInteger upperExc) { BigInteger lowerBig = lower instanceof BigInteger ? (BigInteger) lower : BigInteger.valueOf(lower.longValue()); BigInteger bound = upperExc.subtract(lowerBig); BigInteger rnd = nextBigInteger(getSecureRandom(context), bound, bound.bitLength()); return RubyBignum.newBignum(context.runtime, rnd.add(lowerBig)); }
@JRubyMethod public IRubyObject ceil(ThreadContext context) { checkFloatDomain(); BigInteger ceil = value.setScale(0, RoundingMode.CEILING).toBigInteger(); if (ceil.compareTo(BigInteger.valueOf((long) ceil.intValue())) == 0) { // It fits in Fixnum return RubyInteger.int2fix(context.runtime, ceil.intValue()); } return RubyBignum.newBignum(context.runtime, ceil); }
@JRubyMethod(name = "to_r") public IRubyObject to_r(ThreadContext context) { checkFloatDomain(); int scale = value.scale(); BigInteger numerator = value.scaleByPowerOfTen(scale).toBigInteger(); BigInteger denominator = BigInteger.TEN.pow(scale); return RubyRational.newInstance(context, RubyBignum.newBignum(context.runtime, numerator), RubyBignum.newBignum(context.runtime, denominator)); }
@Override public IRubyObject op_mul(ThreadContext context, long otherValue) { Ruby runtime = context.runtime; try { return newFixnum(runtime, Math.multiplyExact(value, otherValue)); } catch (ArithmeticException ae) { return RubyBignum.newBignum(runtime, value).op_mul(context, otherValue); } }
/** rb_big_neg * */ @JRubyMethod(name = "~") public IRubyObject op_neg() { return RubyBignum.newBignum(getRuntime(), value.not()); }
private IRubyObject subtractOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBignum.newBignum(context.runtime, value).op_minus(context, ((RubyBignum) other).value); } if (other instanceof RubyFloat) { return context.runtime.newFloat((double) value - ((RubyFloat) other).getDoubleValue()); } return coerceBin(context, sites(context).op_minus, other); }
public static RubyInteger f_gcd(ThreadContext context, RubyInteger x, RubyInteger y) { if (x instanceof RubyFixnum && y instanceof RubyFixnum && isLongMinValue((RubyFixnum) x)) return RubyFixnum.newFixnum(context.runtime, i_gcd(x.getLongValue(), y.getLongValue())); BigInteger gcd = x.getBigIntegerValue().gcd(y.getBigIntegerValue()); if (gcd.compareTo(RubyBignum.LONG_MAX) <= 0) { // gcd always positive return RubyFixnum.newFixnum(context.runtime, gcd.longValue()); } return RubyBignum.newBignum(context.runtime, gcd); }
private IRubyObject lshift(long width) { if (width > BIT_SIZE - 1 || ((~0L << BIT_SIZE - width - 1) & value) != 0) { return RubyBignum.newBignum(getRuntime(), value).op_lshift(RubyFixnum.newFixnum(getRuntime(), width)); } return RubyFixnum.newFixnum(getRuntime(), value << width); }