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

Support BigDecimal comparison with and initialization from BigRational #5675

Merged
merged 1 commit into from Feb 10, 2018

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Feb 2, 2018

This PR allows below code to work:

def add_numbers(*numbers : Number)
  numbers.reduce(0.to_big_d) { |sum, n| sum + n.to_big_d }
end

add_numbers(1, 1.23, 3.to_big_d, 3.3.to_big_r) # => 8.53

@@ -133,6 +134,11 @@ struct BigDecimal < Number
initialize(num.to_s)
end

# Creates a new `BigDecimal` from `BigRational`.
def initialize(num : BigRational)
initialize(num.to_f64)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about num.numerator.to_big_d / num.denominator.to_big_d?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done & force-pushed

Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

Oops

@Sija Sija force-pushed the bigdecimal-with-bigrational branch from 5477b41 to ac84c3b Compare February 2, 2018 21:25
@@ -133,6 +134,13 @@ struct BigDecimal < Number
initialize(num.to_s)
end

# Creates a new `BigDecimal` from `BigRational`.
def initialize(num : BigRational)
Copy link
Contributor

Choose a reason for hiding this comment

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

but why not make this a self.new then you can just return the original number instead of copying the value and scale...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True indeed, I've forgot about that, thx! force-pushed again

@Sija Sija force-pushed the bigdecimal-with-bigrational branch from ac84c3b to 7503522 Compare February 2, 2018 21:32
@Sija
Copy link
Contributor Author

Sija commented Feb 10, 2018

Is it GTG? Could we have a 2nd review here?

@asterite asterite merged commit ee271d8 into crystal-lang:master Feb 10, 2018
@RX14 RX14 added this to the Next milestone Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants