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: b3681eeef2e9
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e881d928c90a
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Nov 28, 2015

  1. [Truffle] code style

    pitr-ch committed Nov 28, 2015
    Copy the full SHA
    2fdcfc9 View commit details
  2. Copy the full SHA
    5f8bbd9 View commit details
  3. Copy the full SHA
    f1193d6 View commit details
  4. Copy the full SHA
    e881d92 View commit details
6 changes: 6 additions & 0 deletions spec/ruby/library/bigdecimal/round_spec.rb
Original file line number Diff line number Diff line change
@@ -193,4 +193,10 @@
lambda { BigDecimal('Infinity').round }.should raise_error(FloatDomainError)
lambda { BigDecimal('-Infinity').round }.should raise_error(FloatDomainError)
end

it 'do not raise exception, if self is special value and precision is given' do
lambda { BigDecimal('NaN').round(2) }.should_not raise_error(FloatDomainError)
lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError)
lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError)
end
end
14 changes: 1 addition & 13 deletions spec/truffle/tags/core/float/to_s_tags.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
fails:Float#to_s emits a trailing '.0' for the mantissa in e format
fails:Float#to_s uses non-e format for a positive value with fractional part having 5 significant figures
fails:Float#to_s uses non-e format for a negative value with fractional part having 5 significant figures
fails:Float#to_s uses e format for a positive value with fractional part having 6 significant figures
fails:Float#to_s uses e format for a negative value with fractional part having 6 significant figures
fails:Float#to_s uses non-e format for a positive value with whole part having 15 significant figures
fails:Float#to_s uses non-e format for a negative value with whole part having 15 significant figures
fails:Float#to_s uses non-e format for a positive value with whole part having 16 significant figures
fails:Float#to_s uses non-e format for a negative value with whole part having 16 significant figures
fails:Float#to_s uses e format for a positive value with whole part having 18 significant figures
fails:Float#to_s uses e format for a negative value with whole part having 18 significant figures
fails:Float#to_s uses non-e format for a positive value with whole part having 17 significant figures
fails:Float#to_s uses non-e format for a negative value with whole part having 17 significant figures
fails:Float#to_s outputs the minimal, unique form necessary to recreate the value
39 changes: 20 additions & 19 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/FloatNodes.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import com.oracle.truffle.api.utilities.ConditionProfile;

import org.jcodings.specific.USASCIIEncoding;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.runtime.NotProvided;
@@ -177,7 +178,7 @@ public Object powCoerced(VirtualFrame frame, double a, DynamicObject b) {

}

@CoreMethod(names = {"/", "__slash__"}, required = 1)
@CoreMethod(names = { "/", "__slash__" }, required = 1)
public abstract static class DivNode extends CoreMethodArrayArgumentsNode {

@Child private CallDispatchHeadNode redoCoercedNode;
@@ -205,7 +206,7 @@ public double div(double a, DynamicObject b) {
"!isInteger(b)",
"!isLong(b)",
"!isDouble(b)",
"!isRubyBignum(b)"})
"!isRubyBignum(b)" })
public Object div(VirtualFrame frame, double a, Object b) {
if (redoCoercedNode == null) {
CompilerDirectives.transferToInterpreter();
@@ -317,7 +318,7 @@ public boolean lessBignum(double a, DynamicObject b) {
"!isRubyBignum(b)",
"!isInteger(b)",
"!isLong(b)",
"!isDouble(b)"})
"!isDouble(b)" })
public Object lessCoerced(VirtualFrame frame, double a, Object b) {
return ruby(frame, "b, a = math_coerce other, :compare_error; a < b", "other", b);
}
@@ -349,7 +350,7 @@ public boolean lessEqual(double a, DynamicObject b) {
"!isRubyBignum(b)",
"!isInteger(b)",
"!isLong(b)",
"!isDouble(b)"})
"!isDouble(b)" })
public Object lessEqualCoerced(VirtualFrame frame, double a, Object b) {
return ruby(frame, "b, a = math_coerce other, :compare_error; a <= b", "other", b);
}
@@ -373,7 +374,6 @@ public boolean eqlGeneral(double a, Object b) {
}
}


@CoreMethod(names = { "==", "===" }, required = 1)
public abstract static class EqualNode extends CoreMethodArrayArgumentsNode {

@@ -426,12 +426,12 @@ public DynamicObject compareSecondNaN(Object a, double b) {
return nil();
}

@Specialization(guards = {"!isNaN(a)"})
@Specialization(guards = { "!isNaN(a)" })
public int compare(double a, long b) {
return Double.compare(a, b);
}

@Specialization(guards = {"isInfinity(a)", "isRubyBignum(b)"})
@Specialization(guards = { "isInfinity(a)", "isRubyBignum(b)" })
public int compareInfinity(double a, DynamicObject b) {
if (a < 0) {
return -1;
@@ -440,17 +440,17 @@ public int compareInfinity(double a, DynamicObject b) {
}
}

@Specialization(guards = {"!isNaN(a)", "!isInfinity(a)", "isRubyBignum(b)"})
@Specialization(guards = { "!isNaN(a)", "!isInfinity(a)", "isRubyBignum(b)" })
public int compareBignum(double a, DynamicObject b) {
return Double.compare(a, Layouts.BIGNUM.getValue(b).doubleValue());
}

@Specialization(guards = {"!isNaN(a)", "!isNaN(b)"})
@Specialization(guards = { "!isNaN(a)", "!isNaN(b)" })
public int compare(double a, double b) {
return Double.compare(a, b);
}

@Specialization(guards = {"!isNaN(a)", "!isRubyBignum(b)"})
@Specialization(guards = { "!isNaN(a)", "!isRubyBignum(b)" })
public DynamicObject compare(double a, DynamicObject b) {
return nil();
}
@@ -483,7 +483,7 @@ public boolean greaterEqual(double a, DynamicObject b) {
"!isRubyBignum(b)",
"!isInteger(b)",
"!isLong(b)",
"!isDouble(b)"})
"!isDouble(b)" })
public Object greaterEqualCoerced(VirtualFrame frame, double a, Object b) {
return ruby(frame, "b, a = math_coerce other, :compare_error; a >= b", "other", b);
}
@@ -516,7 +516,7 @@ public boolean greater(double a, DynamicObject b) {
"!isRubyBignum(b)",
"!isInteger(b)",
"!isLong(b)",
"!isDouble(b)"})
"!isDouble(b)" })
public Object greaterCoerced(VirtualFrame frame, double a, Object b) {
return ruby(frame, "b, a = math_coerce(other, :compare_error); a > b", "other", b);
}
@@ -618,7 +618,7 @@ public RoundNode(RubyContext context, SourceSection sourceSection) {

@Specialization(guards = "doubleInLongRange(n)")
public long roundFittingLong(double n, NotProvided ndigits,
@Cached("createBinaryProfile()") ConditionProfile positiveProfile) {
@Cached("createBinaryProfile()") ConditionProfile positiveProfile) {
long l = (long) n;
if (positiveProfile.profile(n >= 0.0)) {
if (n - l >= 0.5) {
@@ -639,7 +639,7 @@ protected boolean doubleInLongRange(double n) {

@Specialization
public Object round(double n, NotProvided ndigits,
@Cached("createBinaryProfile()") ConditionProfile positiveProfile) {
@Cached("createBinaryProfile()") ConditionProfile positiveProfile) {
// Algorithm copied from JRuby - not shared as we want to branch profile it

if (Double.isInfinite(n)) {
@@ -721,17 +721,18 @@ public double toF(double value) {

}

@CoreMethod(names = {"to_s", "inspect"})
public abstract static class ToSNode extends CoreMethodArrayArgumentsNode {
@CoreMethod(names = { "java_to_s" }, visibility = Visibility.PRIVATE)
public abstract static class JavaToSNode extends CoreMethodArrayArgumentsNode {

public ToSNode(RubyContext context, SourceSection sourceSection) {
public JavaToSNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@TruffleBoundary
@Specialization
public DynamicObject toS(double value) {
return create7BitString(StringOperations.encodeByteList(Double.toString(value), USASCIIEncoding.INSTANCE));
public DynamicObject javaToS(double value) {
return create7BitString(
StringOperations.encodeByteList(String.format("%.15g", value), USASCIIEncoding.INSTANCE));
}

}
Original file line number Diff line number Diff line change
@@ -1842,7 +1842,7 @@ public Object round(VirtualFrame frame, DynamicObject value, int digit, int roun
}

@Specialization(guards = "!isNormal(value)")
public Object roundSpecial(VirtualFrame frame, DynamicObject value, Object unusedPrecision, Object unusedRoundingMode) {
public Object roundSpecial(VirtualFrame frame, DynamicObject value, NotProvided precision, Object unusedRoundingMode) {
switch (Layouts.BIG_DECIMAL.getType(value)) {
case NEGATIVE_INFINITY:
CompilerDirectives.transferToInterpreter();
@@ -1863,6 +1863,11 @@ public Object roundSpecial(VirtualFrame frame, DynamicObject value, Object unuse

}
}

@Specialization(guards = {"!isNormal(value)", "wasProvided(precision)"})
public Object roundSpecial(VirtualFrame frame, DynamicObject value, Object precision, Object unusedRoundingMode) {
return value;
}
}

@CoreMethod(names = "finite?")
20 changes: 19 additions & 1 deletion truffle/src/main/ruby/core/float.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2014, 2015 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1
@@ -74,4 +74,22 @@ def equal_fallback(other)

private :equal_fallback

# TODO (pitr 27-Nov-2015): needs better implementation
def to_s
return format('%g', self) if infinite? || nan?

str = java_to_s

# remove extra zeroes
str.gsub! /^(-?)(\d+\.((\d*[1-9])|0))0+/, '\1\2'

return str if str =~ /e/

# add trailing zero if none
str << '.0' unless str =~ /\./
str
end

alias_method :inspect, :to_s

end