protected static void checkIntTypePrecision(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { if (value instanceof RubyFloat) { double doubleVal = RubyNumeric.num2dbl(value); if (Math.floor(doubleVal) != doubleVal) { throw context.runtime.newRangeError("Non-integral floating point value assigned to integer field."); } } if (type == Descriptors.FieldDescriptor.Type.UINT32 || type == Descriptors.FieldDescriptor.Type.UINT64) { if (RubyNumeric.num2dbl(value) < 0) { throw context.runtime.newRangeError("Assigning negative value to unsigned integer field."); } } }
public static int num2uint(IRubyObject value) { long longVal = RubyNumeric.num2long(value); if (longVal > UINT_MAX) throw value.getRuntime().newRangeError("Integer " + longVal + " too big to convert to 'unsigned int'"); long num = longVal; if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) // encode to UINT32 num = (-longVal ^ (-1l >>> 32) ) + 1; RubyNumeric.checkInt(value, num); return (int) num; }
val = enumDescriptor.findValueByName(value.asJavaString()); if (val == null) throw runtime.newRangeError("Enum value " + value + " is not found."); } else if(!isRubyNum(value)) { throw runtime.newTypeError("Expected number or symbol type for enum field.");
throw runtime.newRangeError("Enum value " + value + " is not found.");
/** rb_big2long * */ public static long big2long(RubyBignum value) { BigInteger big = value.getValue(); if (big.compareTo(LONG_MIN) < 0 || big.compareTo(LONG_MAX) > 0) { throw value.getRuntime().newRangeError("bignum too big to convert into `long'"); } return big.longValue(); }
public Object coerce(RubyNumeric numeric, Class target) { long value = numeric.getLongValue(); if (isLongShortable(value)) { return Short.valueOf((short)value); } throw numeric.getRuntime().newRangeError("too big for short: " + numeric); } };
final long begLen0(long len) { long beg = RubyNumeric.num2long(this.begin); if (beg < 0) { beg += len; if (beg < 0) { throw getRuntime().newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range"); } } return beg; }
public static long randomLongLimited(ThreadContext context, IRubyObject obj, long limit) { RandomType rnd = tryGetRandomType(context, obj); if (rnd == null) { RubyInteger v = Helpers.invokePublic(context, obj, "rand", context.runtime.newFixnum(limit + 1)).convertToInteger(); long r = RubyNumeric.num2long(v); if (r < 0) throw context.runtime.newRangeError("random number too small " + r); if (r > limit) throw context.runtime.newRangeError("random number too big " + r); return r; } return randLimitedFixnumInner(rnd.impl, limit); }
private IRubyObject putBytes(ThreadContext context, long off, ByteList bl, int idx, int len) { if (idx < 0 || idx > bl.length()) { throw context.runtime.newRangeError("invalid string index"); } if (len < 0 || len > (bl.length() - idx)) { throw context.runtime.newRangeError("invalid length"); } getMemoryIO().put(off, bl.getUnsafeBytes(), bl.begin() + idx, len); return this; }
public static double randomReal(ThreadContext context, IRubyObject obj) { RandomType random = tryGetRandomType(context, obj); if (random != null) return random.genrandReal(); double d = RubyNumeric.num2dbl(context, Helpers.invoke(context, obj, "rand")); if (d < 0.0 || d >= 1.0) throw context.runtime.newRangeError("random number too big: " + d); return d; }
/** nucomp_to_r * */ @JRubyMethod(name = "to_r") public IRubyObject to_r(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Rational"); } return f_to_r(context, real); }
/** nucomp_to_i * */ @JRubyMethod(name = "to_i") public IRubyObject to_i(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Integer"); } return f_to_i(context, real); }
/** nucomp_to_f * */ @JRubyMethod(name = "to_f") public IRubyObject to_f(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Float"); } return f_to_f(context, real); }
/** nucomp_to_r * */ @JRubyMethod(name = "to_r") public IRubyObject to_r(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Rational"); } return f_to_r(context, real); }
/** nucomp_rationalize * */ @JRubyMethod(name = "rationalize", optional = 1, compat = CompatVersion.RUBY1_9) public IRubyObject rationalize(ThreadContext context, IRubyObject[] args) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Rational"); } return real.callMethod(context, "rationalize", args); }
/** nucomp_rationalize * */ @JRubyMethod(name = "rationalize", optional = 1, compat = CompatVersion.RUBY1_9) public IRubyObject rationalize(ThreadContext context, IRubyObject[] args) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Rational"); } return real.callMethod(context, "rationalize", args); }
/** nucomp_to_i * */ @JRubyMethod(name = "to_i") public IRubyObject to_i(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Integer"); } return f_to_i(context, real); }
/** nucomp_to_f * */ @JRubyMethod(name = "to_f") public IRubyObject to_f(ThreadContext context) { if (k_inexact_p(image) || !f_zero_p(context, image)) { throw context.runtime.newRangeError("can't convert " + f_to_s(context, this).convertToString() + " into Float"); } return f_to_f(context, real); }
@JRubyMethod(name = {"pos=", "pointer="}) public IRubyObject set_pos(IRubyObject pos) { check(); int i = RubyNumeric.num2int(pos); int size = str.getByteList().getRealSize(); if (i < 0) i += size; if (i < 0 || i > size) throw getRuntime().newRangeError("index out of range."); this.pos = i; return RubyFixnum.newFixnum(getRuntime(), i); }
@JRubyMethod(name = {"pos=", "pointer="}) public IRubyObject set_pos(IRubyObject pos) { check(); int i = RubyNumeric.num2int(pos); int size = str.getByteList().getRealSize(); if (i < 0) i += size; if (i < 0 || i > size) throw getRuntime().newRangeError("index out of range."); this.pos = i; return RubyFixnum.newFixnum(getRuntime(), i); }