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

[Truffle] Add special values of BigDecimal #2985

Merged
merged 6 commits into from
Jun 1, 2015
Merged

Conversation

pitr-ch
Copy link
Member

@pitr-ch pitr-ch commented May 25, 2015

not ready for merge

@@ -110,21 +168,23 @@ public RubyBasicObject initialize(RubyBasicObject self, RubyBasicObject v) {

@Specialization(guards = "isRubyString(v)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put @TruffleBoundary on this. The v.toString() is not going to compile well.

@chrisseaton
Copy link
Contributor

Looks great!

public RubyBasicObject initializeFromString(RubyBasicObject self, RubyBasicObject v) {
// TODO (pitr 20-May-2015): add NaN, Infinity handling
// TODO (pitr 21-May-2015): add profile?
// TODO (pitr 25-May-2015): construction of positive and negative zero
switch (v.toString()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Is toString() redefined somehow for BigDecimal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you want is really v as a RubyString, right?
Then it makes sense to call toString() although it is not very efficient (good enough for now for debug output like this).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I missed the guard with Chris' note. I think it's clearer to have RubyString as the argument type in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RubyString#toString is the exception to that note. What we don't want to do is things like to_s as toString or hash as hatched - we don't want to map the Ruby API into the Java API like that (because those methods don't give us anywhere to store state for optimisation).

@pitr-ch pitr-ch force-pushed the bigdecimal branch 2 times, most recently from 0bdce40 to 72a6979 Compare May 28, 2015 17:03
@pitr-ch
Copy link
Member Author

pitr-ch commented May 28, 2015

I had to add coerce specializations to other numerical classes, to make > work. If that's ok I'll finish rest of the comparisons and operations with special values to make this merge-able.

@chrisseaton
Copy link
Contributor

Looks good. Do you mean you'll add specialisations to classes like Float for comparing against BigDecimal? That doesn't sound right - as in MRI and Rubinius BigDecimal is a C extension and can't modify the other class methods like that. Look at the way Rational is handled - I think in some cases Fixnum > BigDecimal is done as BigDecimal <= Fixnum in some way.

@eregon
Copy link
Member

eregon commented May 29, 2015

Yes coerce should be used for these cases and should already be handled in Fixnum/Bignum/Float.

@eregon
Copy link
Member

eregon commented May 29, 2015

But I think @pitr-ch meant BigDecimal < Float for instance (in <=>), in which case it's fine to optimize that direction and probably even expected (to not need to call coerce).

@pitr-ch
Copy link
Member Author

pitr-ch commented May 29, 2015

I mean changes like https://github.com/jruby/jruby/pull/2985/files#diff-03ee9d17196ada218812f3319c5c3f0c. It was either raising always an exception or missing completely. So for 1 > BigDecimal("0.5") it would raise argument error or missing node for FixnumNodes.Greater(int,BigDecimal). With this changes, it calls coerce on BigDecimal so 1 > BigDecimal("0.5") is translated to BigDecimal(1) > BigDecimal("0.5") and evaluated successfully. The other way BigDecimal("0.5") > 1 is done usually ending up in @Specialization(guards = "isNormal(a)") public int compare(RubyBasicObject a, int b) { return compareBigDecimal(a, getBigDecimalValue(b)); }

@eregon
Copy link
Member

eregon commented May 29, 2015

great!

@pitr-ch
Copy link
Member Author

pitr-ch commented Jun 1, 2015

This PR can be merged, assuming that I can ignore the two failing jobs on Travis.


def coerce(other)
[BigDecimal(other), self]
# TODO (pitr 28-may-2015): detect what cannot be coerced and raise?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigDecimal() should raise so that implementation should be fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks, I'll remove it.

@chrisseaton
Copy link
Contributor

Great - yeah those failures aren't our problem - do you want to merge it yourself? Might as well rebase instead of creating a merge commit.

@chrisseaton chrisseaton added this to the truffle-dev milestone Jun 1, 2015
@eregon
Copy link
Member

eregon commented Jun 1, 2015

(it might be hard to rebase with the renames etc)

@pitr-ch
Copy link
Member Author

pitr-ch commented Jun 1, 2015

Right I can push directly now :) I've already rebased this PR in the morning so I should not be more than few commits behind, rebase should be fine.

@pitr-ch pitr-ch merged commit efe48bc into jruby:master Jun 1, 2015
@enebo enebo added this to the Non-Release milestone Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants