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."); } } }
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:
@JRubyMethod(name = "log", module = true, visibility = Visibility.PRIVATE) public static RubyFloat log(ThreadContext context, IRubyObject recv, IRubyObject val, IRubyObject base) { double value = RubyNumeric.num2dbl(context, val); double _base = RubyNumeric.num2dbl(context, base); return logCommon(context, value, _base, "log"); }
@JRubyMethod(name = "atan2", required = 2, module = true, visibility = Visibility.PRIVATE) public static RubyFloat atan2(ThreadContext context, IRubyObject recv, IRubyObject x, IRubyObject y) { double valuea = RubyNumeric.num2dbl(context, x); double valueb = RubyNumeric.num2dbl(context, y); return RubyFloat.newFloat(context.runtime, Math.atan2(valuea, valueb)); }
@JRubyMethod(name = "cosh", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat cosh(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); return RubyFloat.newFloat(context.runtime, (Math.exp(value) + Math.exp(-value)) / 2.0); }
@JRubyMethod(name = "sinh", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat sinh(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); return RubyFloat.newFloat(context.runtime, (Math.exp(value) - Math.exp(-value)) / 2.0); }
/** Returns the natural logarithm of x. * */ @JRubyMethod(name = "log", module = true, visibility = Visibility.PRIVATE) public static RubyFloat log(ThreadContext context, IRubyObject recv, IRubyObject val) { double value = RubyNumeric.num2dbl(context, val); return logCommon(context, value, Math.E, "log"); }
/** Returns the base 2 logarithm of x. * */ @JRubyMethod(name = "log2", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat log2(ThreadContext context, IRubyObject recv, IRubyObject x) { return logCommon(context, RubyNumeric.num2dbl(context, x), 2, "log2"); }
/** Returns the base 10 logarithm of x. * */ @JRubyMethod(name = "log10", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat log10(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); if (value < 0) { throw context.runtime.newMathDomainError("log10"); } return RubyFloat.newFloat(context.runtime, Math.log10(value)); }
@JRubyMethod(name = "sqrt", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat sqrt(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); if (value < 0) throw context.runtime.newMathDomainError("sqrt"); return RubyFloat.newFloat(context.runtime, value == 0.0 ? 0.0 : Math.sqrt(value)); }
@JRubyMethod(name = "sqrt", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat sqrt(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); if (value < 0) throw context.runtime.newMathDomainError("sqrt"); return RubyFloat.newFloat(context.runtime, value == 0.0 ? 0.0 : Math.sqrt(value)); }
@JRubyMethod(name = "acos", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat acos(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); if (value < -1.0 || value > 1.0) throw context.runtime.newMathDomainError("acos"); return RubyFloat.newFloat(context.runtime, Math.acos(value)); }
@JRubyMethod(name = "asin", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat asin(ThreadContext context, IRubyObject recv, IRubyObject x) { double value = RubyNumeric.num2dbl(context, x); if (value < -1.0 || value > 1.0) throw context.runtime.newMathDomainError("asin"); return RubyFloat.newFloat(context.runtime, Math.asin(value)); }
@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))); }
@JRubyMethod(name = "cbrt", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat cbrt(ThreadContext context, IRubyObject recv, IRubyObject x) { double result = Math.cbrt(RubyNumeric.num2dbl(context, x)); domainCheck(recv, result, "cbrt"); return RubyFloat.newFloat(context.runtime, result); }
@JRubyMethod(name = "-", required = 1) public IRubyObject op_minus(ThreadContext context, IRubyObject other) { if (other instanceof RubyTime) return opMinus(context.runtime, (RubyTime) other); return opMinus(context.runtime, RubyNumeric.num2dbl(context, numExact(context, other))); }
@JRubyMethod(name = "cbrt", required = 1, module = true, visibility = Visibility.PRIVATE) public static RubyFloat cbrt(ThreadContext context, IRubyObject recv, IRubyObject x) { double result = Math.cbrt(RubyNumeric.num2dbl(context, x)); domainCheck(recv, result, "cbrt"); return RubyFloat.newFloat(context.runtime, result); }
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; }
@JRubyMethod(name = "-", required = 1) public IRubyObject op_minus(ThreadContext context, IRubyObject other) { if (other instanceof RubyTime) return opMinus(context.runtime, (RubyTime) other); return opMinus(context.runtime, RubyNumeric.num2dbl(context, numExact(context, other))); }
@JRubyMethod(name = "+", required = 1) public IRubyObject op_plus(ThreadContext context, IRubyObject other) { if (other instanceof RubyTime) { throw context.runtime.newTypeError("time + time?"); } double adjustMillis = RubyNumeric.num2dbl(context, numExact(context, other)) * 1000; return opPlusMillis(context.runtime, adjustMillis); }