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; }
switch (fieldDescriptor.getType()) { case INT32: val = RubyNumeric.num2int(value); break; case INT64: val = RubyNumeric.num2long(value); break; case UINT32: if (!Utils.isRubyNum(value)) throw runtime.newTypeError("Expected number type for float field."); val = (float) RubyNumeric.num2dbl(value); break; case DOUBLE: if (!Utils.isRubyNum(value)) throw runtime.newTypeError("Expected number type for double field."); val = RubyNumeric.num2dbl(value); break; case BOOL: val = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value)); } else if (value instanceof RubySymbol || value instanceof RubyString) { val = enumDescriptor.findValueByName(value.asJavaString());
/** * Return the value of this numeric as a 32-bit long. If the value does not * fit in 32 bits, it will be truncated. */ public int getIntValue() { return (int) getLongValue(); }
private static final int getNativeLibraryFlags(IRubyObject rbFlags) { int f = 0, flags = RubyNumeric.fix2int(rbFlags); f |= (flags & RTLD_LAZY) != 0 ? Library.LAZY : 0; f |= (flags & RTLD_NOW) != 0 ? Library.NOW : 0; f |= (flags & RTLD_LOCAL) != 0 ? Library.LOCAL : 0; f |= (flags & RTLD_GLOBAL) != 0 ? Library.GLOBAL : 0; return f; } public DynamicLibrary(Ruby runtime, RubyClass klass, String name, Library library) {
@JRubyMethod(name = {"length", "size"}) public RubyNumeric length(ThreadContext context) { initializedCheck(); return RubyNumeric.int2fix(context.runtime, count.get()); }
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); } }
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."); } } }
@JRubyMethod(name = "Integer", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_8) public static IRubyObject new_integer(ThreadContext context, IRubyObject recv, IRubyObject object) { if (object instanceof RubyFloat) { double val = ((RubyFloat)object).getDoubleValue(); if (val >= (double) RubyFixnum.MAX || val < (double) RubyFixnum.MIN) { return RubyNumeric.dbl2num(context.runtime,((RubyFloat)object).getDoubleValue()); } } else if (object instanceof RubyFixnum || object instanceof RubyBignum) { return object; } else if (object instanceof RubyString) { return RubyNumeric.str2inum(context.runtime,(RubyString)object,0,true); } IRubyObject tmp = TypeConverter.convertToType(object, context.runtime.getInteger(), "to_int", false); if (tmp.isNil()) return object.convertToInteger("to_i"); return tmp; }
private int normalizeArrayIndex(IRubyObject index) { int arrIndex = RubyNumeric.num2int(index); int arrSize = this.storage.size(); if (arrIndex < 0 && arrSize > 0) { arrIndex = arrSize + arrIndex; } return arrIndex; }
@JRubyMethod(name = "Integer", module = true, visibility = PRIVATE, compat = RUBY1_9) public static IRubyObject new_integer19(ThreadContext context, IRubyObject recv, IRubyObject object, IRubyObject base) { int bs = RubyNumeric.num2int(base); if(object instanceof RubyString) { return RubyNumeric.str2inum(context.runtime,(RubyString)object,bs,true); } else { IRubyObject tmp = object.checkStringType(); if(!tmp.isNil()) { return RubyNumeric.str2inum(context.runtime,(RubyString)tmp,bs,true); } } throw context.runtime.newArgumentError("base specified for non string value"); }
public static RubyInteger str2inum(Ruby runtime, RubyString str, int base) { return str2inum(runtime, str, base, false); }
public IRubyObject allocate(Ruby runtime, RubyClass klass) { return new RubyNumeric(runtime, klass); } };
public static int fix2int(RubyFixnum arg) { long num = arg.getLongValue(); checkInt(arg, num); return (int) num; }
@JRubyMethod(name = "ldexp", required = 2, module = true, visibility = Visibility.PRIVATE) public static RubyFloat ldexp(ThreadContext context, IRubyObject recv, IRubyObject mantissa, IRubyObject exponent) { return RubyFloat.newFloat(context.runtime, RubyNumeric.num2dbl(context, mantissa) * Math.pow(2.0, RubyNumeric.num2int(exponent))); }
/** num_abs/1.9 * */ @JRubyMethod(name = "magnitude") public IRubyObject magnitude(ThreadContext context) { return abs(context); }
public int getInt(String propertyName, Object... args) { IRubyObject result = getRubyProperty(propertyName, args); if (result instanceof RubyNil) { return 0; } else { return (int) ((RubyNumeric) result).getLongValue(); } }
@JRubyMethod(name = "pos=", required = 1) public IRubyObject set_pos(IRubyObject newPos) { int pos2 = RubyNumeric.fix2int(newPos); if (pos2 >= 0) this.pos = pos2; return newPos; }