Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d807cb25e341
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4a290112d463
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Nov 29, 2015

  1. Copy the full SHA
    f6db901 View commit details
  2. Copy the full SHA
    4a29011 View commit details
Showing with 42 additions and 17 deletions.
  1. +1 −1 lib/ruby/truffle/truffle/bigdecimal.rb
  2. +4 −0 spec/ruby/library/bigdecimal/new_spec.rb
  3. +37 −16 truffle/src/main/java/org/jruby/truffle/nodes/ext/BigDecimalNodes.java
2 changes: 1 addition & 1 deletion lib/ruby/truffle/truffle/bigdecimal.rb
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ def ==(o)
alias_method :===, :==

def coerce(other)
[BigDecimal(other), self]
[BigDecimal(other, 20), self]
end

# TODO (pitr 28-may-2015): compare with pure Java versions
4 changes: 4 additions & 0 deletions spec/ruby/library/bigdecimal/new_spec.rb
Original file line number Diff line number Diff line change
@@ -93,4 +93,8 @@
BigDecimal.new("-12345.6E-1").should == -reference
end

it 'raises ArgumentError when Float is used without precision' do
lambda { BigDecimal(1.0) }.should raise_error(ArgumentError)
end

end
Original file line number Diff line number Diff line change
@@ -164,12 +164,7 @@ protected DynamicObject createBigDecimal(VirtualFrame frame, Object value) {
return createBigDecimal.executeCreate(frame, value);
}

protected DynamicObject initializeBigDecimal(VirtualFrame frame, Object value, DynamicObject self) {
setupCreateBigDecimal();
return createBigDecimal.executeInitialize(frame, value, self);
}

protected DynamicObject initializeBigDecimal(VirtualFrame frame, Object value, DynamicObject self, int digits) {
protected DynamicObject initializeBigDecimal(VirtualFrame frame, Object value, DynamicObject self, Object digits) {
setupCreateBigDecimal();
return createBigDecimal.executeInitialize(frame, value, self, digits);
}
@@ -267,7 +262,7 @@ private void setBigDecimalValue(DynamicObject bigdecimal, Type type) {
Layouts.BIG_DECIMAL.setType(bigdecimal, type);
}

public abstract DynamicObject executeInitialize(VirtualFrame frame, Object value, DynamicObject alreadyAllocatedSelf, int digits);
public abstract DynamicObject executeInitialize(VirtualFrame frame, Object value, DynamicObject alreadyAllocatedSelf, Object digits);

public final DynamicObject executeCreate(VirtualFrame frame, Object value) {
if (allocateNode == null) {
@@ -276,11 +271,12 @@ public final DynamicObject executeCreate(VirtualFrame frame, Object value) {
}

DynamicObject rubyClass = (getBigDecimalClass());
return executeInitialize(frame, value, (DynamicObject) allocateNode.call(frame, rubyClass, "allocate", null));
return executeInitialize(frame, value, (DynamicObject) allocateNode.call(frame, rubyClass, "allocate", null), NotProvided.INSTANCE);
}

public final DynamicObject executeInitialize(VirtualFrame frame, Object value, DynamicObject alreadyAllocatedSelf) {
return executeInitialize(frame, value, alreadyAllocatedSelf, 0);
@Specialization
public DynamicObject create(VirtualFrame frame, long value, DynamicObject self, NotProvided digits) {
return create(frame, value, self, 0);
}

@Specialization
@@ -290,6 +286,12 @@ public DynamicObject create(VirtualFrame frame, long value, DynamicObject self,
return self;
}

@Specialization
public DynamicObject create(VirtualFrame frame, double value, DynamicObject self, NotProvided digits) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().argumentError("can't omit precision for a Float.", this));
}

@Specialization
public DynamicObject create(VirtualFrame frame, double value, DynamicObject self, int digits) {
setBigDecimalValue(self,
@@ -298,41 +300,60 @@ public DynamicObject create(VirtualFrame frame, double value, DynamicObject self
}

@Specialization(guards = "value == NEGATIVE_INFINITY || value == POSITIVE_INFINITY")
public DynamicObject createInfinity(VirtualFrame frame, Type value, DynamicObject self, int digits) {
public DynamicObject createInfinity(VirtualFrame frame, Type value, DynamicObject self, Object digits) {
return createWithMode(frame, value, self, "EXCEPTION_INFINITY", "Computation results to 'Infinity'");
}

@Specialization(guards = "value == NAN")
public DynamicObject createNaN(VirtualFrame frame, Type value, DynamicObject self, int digits) {
public DynamicObject createNaN(VirtualFrame frame, Type value, DynamicObject self, Object digits) {
return createWithMode(frame, value, self, "EXCEPTION_NaN", "Computation results to 'NaN'(Not a Number)");
}

@Specialization(guards = "value == NEGATIVE_ZERO")
public DynamicObject createNegativeZero(VirtualFrame frame, Type value, DynamicObject self, int digits) {
public DynamicObject createNegativeZero(VirtualFrame frame, Type value, DynamicObject self, Object digits) {
setBigDecimalValue(self, value);
return self;
}

@Specialization
public DynamicObject create(VirtualFrame frame, BigDecimal value, DynamicObject self, NotProvided digits) {
return create(frame, value, self, 0);
}
@Specialization
public DynamicObject create(VirtualFrame frame, BigDecimal value, DynamicObject self, int digits) {
setBigDecimalValue(self, value.round(new MathContext(digits, getRoundMode(frame))));
return self;
}

@Specialization(guards = "isRubyBignum(value)")
public DynamicObject createBignum(VirtualFrame frame, DynamicObject value, DynamicObject self, NotProvided digits) {
return createBignum(frame, value, self, 0);
}

@Specialization(guards = "isRubyBignum(value)")
public DynamicObject createBignum(VirtualFrame frame, DynamicObject value, DynamicObject self, int digits) {
setBigDecimalValue(self,
getBignumBigDecimalValue(value).round(new MathContext(digits, getRoundMode(frame))));
return self;
}

@Specialization(guards = "isRubyBigDecimal(value)")
public DynamicObject createBigDecimal(VirtualFrame frame, DynamicObject value, DynamicObject self, NotProvided digits) {
return createBigDecimal(frame, value, self, 0);
}

@Specialization(guards = "isRubyBigDecimal(value)")
public DynamicObject createBigDecimal(VirtualFrame frame, DynamicObject value, DynamicObject self, int digits) {
setBigDecimalValue(self,
Layouts.BIG_DECIMAL.getValue(value).round(new MathContext(digits, getRoundMode(frame))));
return self;
}

@Specialization(guards = "isRubyString(value)")
public DynamicObject createString(VirtualFrame frame, DynamicObject value, DynamicObject self, NotProvided digits) {
return createString(frame, value, self, 0);
}

@Specialization(guards = "isRubyString(value)")
public DynamicObject createString(VirtualFrame frame, DynamicObject value, DynamicObject self, int digits) {
return executeInitialize(frame, getValueFromString(value.toString(), digits), self, digits);
@@ -460,7 +481,7 @@ public InitializeNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public Object initialize(VirtualFrame frame, DynamicObject self, Object value, NotProvided digits) {
return initializeBigDecimal(frame, value, self);
return initializeBigDecimal(frame, value, self, digits);
}

@Specialization
@@ -1864,7 +1885,7 @@ public Object roundSpecial(VirtualFrame frame, DynamicObject value, NotProvided
}
}

@Specialization(guards = {"!isNormal(value)", "wasProvided(precision)"})
@Specialization(guards = { "!isNormal(value)", "wasProvided(precision)" })
public Object roundSpecial(VirtualFrame frame, DynamicObject value, Object precision, Object unusedRoundingMode) {
return value;
}
@@ -2073,7 +2094,7 @@ public BigDecimal doBigDecimal(DynamicObject value) {
return Layouts.BIG_DECIMAL.getValue(value);
}

@Specialization(guards = {"!isRubyBignum(value)", "!isRubyBigDecimal(value)"})
@Specialization(guards = { "!isRubyBignum(value)", "!isRubyBigDecimal(value)" })
public Object doOther(VirtualFrame frame, DynamicObject value) {
final Object result = ruby(
frame,