A representation of a float object
| Method from org.jruby.RubyFloat Detail: |
public IRubyObject abs() {
if (value < 0) {
return RubyFloat.newFloat(getRuntime(), Math.abs(value));
}
return this;
}
|
public IRubyObject ceil() {
return dbl2num(getRuntime(), Math.ceil(value));
}
|
public IRubyObject coerce(IRubyObject other) {
return getRuntime().newArray(RubyKernel.new_float(this, other), this);
}
|
protected int compareValue(RubyNumeric other) {
double otherVal = other.getDoubleValue();
return getValue() > otherVal ? 1 : getValue() < otherVal ? -1 : 0;
}
|
public RubyFloat convertToFloat() {
return this;
}
|
public static RubyClass createFloatClass(Ruby runtime) {
RubyClass floatc = runtime.defineClass("Float", runtime.getNumeric(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.setFloat(floatc);
floatc.index = ClassIndex.FLOAT;
floatc.kindOf = new RubyModule.KindOf() {
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyFloat;
}
};
floatc.getSingletonClass().undefineMethod("new");
floatc.includeModule(runtime.getPrecision());
// Java Doubles are 64 bit long:
floatc.defineConstant("ROUNDS", RubyFixnum.newFixnum(runtime, 1));
floatc.defineConstant("RADIX", RubyFixnum.newFixnum(runtime, 2));
floatc.defineConstant("MANT_DIG", RubyFixnum.newFixnum(runtime, 53));
floatc.defineConstant("DIG", RubyFixnum.newFixnum(runtime, 15));
// Double.MAX_EXPONENT since Java 1.6
floatc.defineConstant("MIN_EXP", RubyFixnum.newFixnum(runtime, -1021));
// Double.MAX_EXPONENT since Java 1.6
floatc.defineConstant("MAX_EXP", RubyFixnum.newFixnum(runtime, 1024));
floatc.defineConstant("MIN_10_EXP", RubyFixnum.newFixnum(runtime, -307));
floatc.defineConstant("MAX_10_EXP", RubyFixnum.newFixnum(runtime, 308));
floatc.defineConstant("MIN", RubyFloat.newFloat(runtime, Double.MIN_VALUE));
floatc.defineConstant("MAX", RubyFloat.newFloat(runtime, Double.MAX_VALUE));
floatc.defineConstant("EPSILON", RubyFloat.newFloat(runtime, 2.2204460492503131e-16));
floatc.defineAnnotatedMethods(RubyFloat.class);
return floatc;
}
|
public IRubyObject divmod(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double y = ((RubyNumeric) other).getDoubleValue();
double x = value;
double mod = Math.IEEEremainder(x, y);
// MRI behavior:
if (Double.isNaN(mod)) {
throw getRuntime().newFloatDomainError("NaN");
}
double div = Math.floor(x / y);
if (y * mod < 0) {
mod += y;
}
final Ruby runtime = getRuntime();
IRubyObject car = dbl2num(runtime, div);
RubyFloat cdr = RubyFloat.newFloat(runtime, mod);
return RubyArray.newArray(runtime, car, cdr);
default:
return coerceBin(context, "divmod", other);
}
}
|
public IRubyObject eql_p(IRubyObject other) {
if (other instanceof RubyFloat) {
double b = ((RubyFloat) other).value;
if (Double.isNaN(value) || Double.isNaN(b)) {
return getRuntime().getFalse();
}
if (value == b) {
return getRuntime().getTrue();
}
}
return getRuntime().getFalse();
}
|
public IRubyObject finite_p() {
if (Double.isInfinite(value) || Double.isNaN(value)) {
return getRuntime().getFalse();
}
return getRuntime().getTrue();
}
|
public IRubyObject floor() {
return dbl2num(getRuntime(), Math.floor(value));
}
|
public double getDoubleValue() {
return value;
}
|
public Class getJavaClass() {
return Double.TYPE;
}
|
public long getLongValue() {
return (long) value;
}
|
public int getNativeTypeIndex() {
return ClassIndex.FLOAT;
}
|
public double getValue() {
return this.value;
}
Getter for property value. |
public RubyFixnum hash() {
return getRuntime().newFixnum(hashCode());
}
|
public final int hashCode() {
long l = Double.doubleToLongBits(value);
return (int)(l ^ l > > > 32);
}
|
public static IRubyObject induced_from(ThreadContext context,
IRubyObject recv,
IRubyObject number) {
if (number instanceof RubyFixnum || number instanceof RubyBignum) {
return number.callMethod(context, MethodIndex.TO_F, "to_f");
}
if (number instanceof RubyFloat) {
return number;
}
throw recv.getRuntime().newTypeError(
"failed to convert " + number.getMetaClass() + " into Float");
}
|
public IRubyObject infinite_p() {
if (Double.isInfinite(value)) {
return RubyFixnum.newFixnum(getRuntime(), value < 0 ? -1 : 1);
}
return getRuntime().getNil();
}
|
public static void marshalTo(RubyFloat aFloat,
MarshalStream output) throws IOException {
output.registerLinkTarget(aFloat);
String strValue = aFloat.toString();
if (Double.isInfinite(aFloat.value)) {
strValue = aFloat.value < 0 ? "-inf" : "inf";
} else if (Double.isNaN(aFloat.value)) {
strValue = "nan";
}
output.writeString(strValue);
}
|
public IRubyObject nan_p() {
return RubyBoolean.newBoolean(getRuntime(), Double.isNaN(value));
}
|
public static RubyFloat newFloat(Ruby runtime,
double value) {
return new RubyFloat(runtime, value);
}
|
public IRubyObject op_cmp(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return dbl_cmp(getRuntime(), value, b);
default:
return coerceCmp(context, "< = >", other);
}
}
|
public IRubyObject op_equal(ThreadContext context,
IRubyObject other) {
if (Double.isNaN(value)) {
return getRuntime().getFalse();
}
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyBoolean.newBoolean(getRuntime(), value == ((RubyNumeric) other)
.getDoubleValue());
default:
// Numeric.equal
return super.op_num_equal(context, other);
}
}
|
public IRubyObject op_fdiv(ThreadContext context,
IRubyObject other) {
// don't override Numeric#div !
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyFloat.newFloat(getRuntime(), value / ((RubyNumeric) other).getDoubleValue());
default:
return coerceBin(context, "/", other);
}
}
|
public IRubyObject op_ge(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(getRuntime(), !Double.isNaN(b) && value >= b);
default:
return coerceRelOp(context, " >=", other);
}
}
|
public IRubyObject op_gt(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(getRuntime(), !Double.isNaN(b) && value > b);
default:
return coerceRelOp(context, " >", other);
}
}
|
public IRubyObject op_le(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(getRuntime(), !Double.isNaN(b) && value < = b);
default:
return coerceRelOp(context, "< =", other);
}
}
|
public IRubyObject op_lt(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(getRuntime(), !Double.isNaN(b) && value < b);
default:
return coerceRelOp(context, "< ", other);
}
}
|
public IRubyObject op_minus(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyFloat.newFloat(getRuntime(), value - ((RubyNumeric) other).getDoubleValue());
default:
return coerceBin(context, "-", other);
}
}
|
public IRubyObject op_mod(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
double y = ((RubyNumeric) other).getDoubleValue();
// Modelled after c ruby implementation (java /,% not same as ruby)
double x = value;
double mod = Math.IEEEremainder(x, y);
if (y * mod < 0) {
mod += y;
}
return RubyFloat.newFloat(getRuntime(), mod);
default:
return coerceBin(context, "%", other);
}
}
|
public IRubyObject op_mul(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyFloat.newFloat(
getRuntime(), value * ((RubyNumeric) other).getDoubleValue());
default:
return coerceBin(context, "*", other);
}
}
|
public IRubyObject op_plus(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyFloat.newFloat(getRuntime(), value + ((RubyNumeric) other).getDoubleValue());
default:
return coerceBin(context, "+", other);
}
}
|
public IRubyObject op_pow(ThreadContext context,
IRubyObject other) {
switch (other.getMetaClass().index) {
case ClassIndex.FIXNUM:
case ClassIndex.BIGNUM:
case ClassIndex.FLOAT:
return RubyFloat.newFloat(getRuntime(), Math.pow(value, ((RubyNumeric) other)
.getDoubleValue()));
default:
return coerceBin(context, "**", other);
}
}
|
public IRubyObject op_uminus() {
return RubyFloat.newFloat(getRuntime(), -value);
}
|
public IRubyObject round() {
double f = value;
if (f > 0.0) {
f = Math.floor(f + 0.5);
}
if (f < 0.0) {
f = Math.ceil(f - 0.5);
}
return dbl2num(getRuntime(), f);
}
|
public IRubyObject to_f() {
return this;
}
|
public IRubyObject to_s() {
if (Double.isInfinite(value)) {
return RubyString.newString(getRuntime(), value < 0 ? "-Infinity" : "Infinity");
}
if (Double.isNaN(value)) {
return RubyString.newString(getRuntime(), "NaN");
}
String val = ""+value;
if(val.indexOf('E") != -1) {
String v2 = FORMAT.format(value);
int ix = v2.length()-1;
while(v2.charAt(ix) == '0" && v2.charAt(ix-1) != '.") {
ix--;
}
if(ix > 15 || "0.0".equals(v2.substring(0,ix+1))) {
val = val.replaceFirst("E(\\d)","e+$1").replaceFirst("E-","e-");
} else {
val = v2.substring(0,ix+1);
}
}
return RubyString.newString(getRuntime(), val);
}
|
public IRubyObject truncate() {
double f = value;
if (f > 0.0) f = Math.floor(f);
if (f < 0.0) f = Math.ceil(f);
return dbl2num(getRuntime(), f);
}
|
public static RubyFloat unmarshalFrom(UnmarshalStream input) throws IOException {
RubyFloat result = RubyFloat.newFloat(input.getRuntime(), org.jruby.util.Convert.byteListToDouble(input.unmarshalString(),false));
input.registerLinkTarget(result);
return result;
}
|
public IRubyObject zero_p() {
return RubyBoolean.newBoolean(getRuntime(), value == 0.0);
}
|