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); } }
private void checkZeroDivisionError(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat && ((RubyFloat) other).getDoubleValue() == 0.0d) { throw context.runtime.newZeroDivisionError(); } }
private static double floatValue(ThreadContext context, RubyFloat v) { final double x = v.getDoubleValue(); checkFloatValue(context, x); return x; }
/** f_signbit * */ private static boolean signbit(ThreadContext context, IRubyObject x) { if (x instanceof RubyFloat) { double value = ((RubyFloat)x).getDoubleValue(); return !Double.isNaN(value) && Double.doubleToLongBits(value) < 0; } return f_negative_p(context, x); }
public static int f32Value32(IRubyObject parameter) { if (parameter instanceof RubyFloat) { return Float.floatToRawIntBits((float) ((RubyFloat) parameter).getDoubleValue()); } else { return (int) other2long(parameter); } }
public static long f32Value64(IRubyObject parameter) { if (parameter instanceof RubyFloat) { return Float.floatToRawIntBits((float) ((RubyFloat) parameter).getDoubleValue()); } else { return other2long(parameter); } }
public static IRubyObject safe_mul(ThreadContext context, IRubyObject a, IRubyObject b, boolean az, boolean bz) { Ruby runtime = context.runtime; double v; if (!az && bz && a instanceof RubyFloat && !Double.isNaN(v = ((RubyFloat)a).getDoubleValue())) { a = v < 0.0d ? runtime.newFloat(-1.0d) : runtime.newFloat(1.0d); } if (!bz && az && b instanceof RubyFloat && !Double.isNaN(v = ((RubyFloat)b).getDoubleValue())) { b = v < 0.0d ? runtime.newFloat(-1.0) : runtime.newFloat(1.0); } return f_mul(context, a, b); }
private IRubyObject op_equalOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBoolean.newBoolean(context.runtime, BigInteger.valueOf(this.value).compareTo(((RubyBignum) other).value) == 0); } if (other instanceof RubyFloat) { return RubyBoolean.newBoolean(context.runtime, (double) value == ((RubyFloat) other).getDoubleValue()); } return super.op_num_equal(context, other); }
public static long float2long(RubyFloat flt) { double aFloat = flt.getDoubleValue(); if (aFloat <= (double) Long.MAX_VALUE && aFloat >= (double) Long.MIN_VALUE) { return (long) aFloat; } // TODO: number formatting here, MRI uses "%-.10g", 1.4 API is a must? throw flt.getRuntime().newRangeError("float " + aFloat + " out of range of integer"); }
private IRubyObject op_ltOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBoolean.newBoolean(context.runtime, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) < 0); } if (other instanceof RubyFloat) { return RubyBoolean.newBoolean(context.runtime, (double) value < ((RubyFloat) other).getDoubleValue()); } return coerceRelOp(context, sites(context).op_lt, other); }
private IRubyObject op_leOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBoolean.newBoolean(context.runtime, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) <= 0); } if (other instanceof RubyFloat) { return RubyBoolean.newBoolean(context.runtime, (double) value <= ((RubyFloat) other).getDoubleValue()); } return coerceRelOp(context, sites(context).op_le, other); }
private IRubyObject op_gtOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBoolean.newBoolean(context.runtime, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) > 0); } if (other instanceof RubyFloat) { return RubyBoolean.newBoolean(context.runtime, (double) value > ((RubyFloat) other).getDoubleValue()); } return coerceRelOp(context, sites(context).op_gt, other); }
private IRubyObject op_geOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return RubyBoolean.newBoolean(context.runtime, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) >= 0); } if (other instanceof RubyFloat) { return RubyBoolean.newBoolean(context.runtime, (double) value >= ((RubyFloat) other).getDoubleValue()); } return coerceRelOp(context, sites(context).op_ge, other); }
private static RubyBigDecimal newInstance(Ruby runtime, IRubyObject recv, RubyFloat arg, MathContext mathContext) { // precision can be no more than float digits if (mathContext.getPrecision() > RubyFloat.DIG + 1) throw runtime.newArgumentError("precision too large"); RubyBigDecimal res = newFloatSpecialCases(runtime, arg); if (res != null) return res; return new RubyBigDecimal(runtime, (RubyClass) recv, new BigDecimal(arg.getDoubleValue(), mathContext)); }
private IRubyObject compareOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return newFixnum(context.runtime, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value)); } if (other instanceof RubyFloat) { return dbl_cmp(context.runtime, (double) value, ((RubyFloat) other).getDoubleValue()); } return coerceCmp(context, sites(context).op_cmp, other); }
private static RubyBigDecimal newFloatSpecialCases(Ruby runtime, RubyFloat val) { if (val.isNaN()) return newNaN(runtime); if (val.isInfinite()) return newInfinity(runtime, val.getDoubleValue() == Double.POSITIVE_INFINITY ? 1 : -1); if (val.isZero()) return newZero(runtime, val.getDoubleValue() == -0.0 ? -1 : 1); return null; }
private int compareToOther(IRubyObject other) { if (other instanceof RubyBignum) return BigInteger.valueOf(value).compareTo(((RubyBignum) other).value); if (other instanceof RubyFloat) return Double.compare((double)value, ((RubyFloat)other).getDoubleValue()); ThreadContext context = getRuntime().getCurrentContext(); return (int) coerceCmp(context, sites(context).op_cmp, other).convertToInteger().getLongValue(); }
private IRubyObject addOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { return ((RubyBignum) other).op_plus(context, this.value); } if (other instanceof RubyFloat) { return context.runtime.newFloat((double) value + ((RubyFloat) other).getDoubleValue()); } return coerceBin(context, sites(context).op_plus, other); }
private IRubyObject multiplyOther(ThreadContext context, IRubyObject other) { Ruby runtime = context.runtime; if (other instanceof RubyBignum) { return ((RubyBignum) other).op_mul(context, this.value); } if (other instanceof RubyFloat) { return runtime.newFloat((double) value * ((RubyFloat) other).getDoubleValue()); } return coerceBin(context, sites(context).op_times, other); }
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); }