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 bigdecimal infinity round #4379

Merged
merged 1 commit into from Dec 14, 2016
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
Expand Up @@ -1354,14 +1354,12 @@ public IRubyObject precs(ThreadContext context) {

@JRubyMethod(name = "round", optional = 2)
public IRubyObject round(ThreadContext context, IRubyObject[] args) {
final int scale = args.length > 0 ? num2int(args[0]) : 0;

// Special treatment for BigDecimal::NAN and BigDecimal::INFINITY
//
// If round is called without any argument, we should raise a
// FloatDomainError. Otherwise, we don't have to call round ;
// we can simply return the number itself.
if (scale == 0 && isInfinity()) {
if (args.length == 0 && isInfinity()) {
StringBuilder message = new StringBuilder("Computation results to ");
message.append('\'').append(callMethod(context, "to_s")).append('\'');

Expand All @@ -1373,6 +1371,7 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
}
}

final int scale = args.length > 0 ? num2int(args[0]) : 0;
RoundingMode mode = (args.length > 1) ? javaRoundingModeFromRubyRoundingMode(context.runtime, args[1]) : getRoundingMode(context.runtime);
// JRUBY-914: Java 1.4 BigDecimal does not allow a negative scale, so we have to simulate it
final RubyBigDecimal bigDecimal;
Expand Down
7 changes: 7 additions & 0 deletions test/mri/bigdecimal/test_bigdecimal.rb
Expand Up @@ -946,6 +946,13 @@ def test_split
assert_equal([-1, "Infinity", 10, 0], BigDecimal.new("-Infinity").split)
end

def test_round_infinity
assert_equal "Infinity", BigDecimal("Infinity").round(0).to_s
assert_raise(FloatDomainError) do
BigDecimal("Infinity").round
end
end

def test_exponent
x = BigDecimal.new('-123.45678901234567890')
assert_equal(3, x.exponent)
Expand Down