Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix float rounding issues. #668

Merged
merged 1 commit into from Apr 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/org/jruby/RubyFloat.java
Expand Up @@ -37,6 +37,7 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import java.math.BigDecimal;
import java.math.BigInteger;
import static org.jruby.util.Numeric.f_abs;
import static org.jruby.util.Numeric.f_add;
Expand Down Expand Up @@ -824,14 +825,31 @@ public IRubyObject round() {
@JRubyMethod(name = "round", optional = 1, compat = CompatVersion.RUBY1_9)
public IRubyObject round(ThreadContext context, IRubyObject[] args) {
if (args.length == 0) return round();
double digits = num2dbl(args[0]);
// truncate floats.
double digits = num2long(args[0]);

double magnifier = Math.pow(10.0, Math.abs(digits));
double number = value;

if (Double.isInfinite(value)) {
if (digits <= 0) throw getRuntime().newFloatDomainError(value < 0 ? "-Infinity" : "Infinity");
return this;
}

if (Double.isNaN(value)) {
if (digits <= 0) throw getRuntime().newFloatDomainError("NaN");
return this;
}

// MRI flo_round logic to deal with huge precision numbers.
double binexp = Math.ceil(Math.log(value)/Math.log(2));
if (digits >= (DIG+2) - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
return RubyFloat.newFloat(context.runtime, number);
}
if (digits < -(binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
return RubyFixnum.zero(context.runtime);
}

if (Double.isInfinite(magnifier)) {
if (digits < 0) number = 0;
} else {
Expand All @@ -840,7 +858,8 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
} else {
number *= magnifier;
}
number = Math.round(number);

number = Math.round(Math.abs(number))*Math.signum(number);
if (digits < 0) {
number *= magnifier;
} else {
Expand All @@ -851,6 +870,12 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
if (digits > 0) {
return RubyFloat.newFloat(context.runtime, number);
} else {
if (number > Long.MAX_VALUE || number < Long.MIN_VALUE) {
// The only way to get huge precise values with BigDecimal is
// to convert the double to String first.
BigDecimal roundedNumber = new BigDecimal(Double.toString(number));
return RubyBignum.newBignum(context.runtime, roundedNumber.toBigInteger());
}
return dbl2num(context.runtime, (long)number);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/org/jruby/RubyNumeric.java
Expand Up @@ -224,9 +224,6 @@ private static long float2long(RubyFloat flt) {
}
}

/** rb_dbl2big + LONG2FIX at once (numeric.c)
*
*/
/** rb_dbl2big + LONG2FIX at once (numeric.c)
*
*/
Expand All @@ -237,7 +234,7 @@ public static IRubyObject dbl2num(Ruby runtime, double val) {
if (Double.isNaN(val)) {
throw runtime.newFloatDomainError("NaN");
}
return convertToNum(val,runtime);
return convertToNum(val, runtime);
}

/** rb_num2dbl and NUM2DBL
Expand Down