Skip to content

Support BigDecimal comparison with and initialization from BigRational#5675

Merged
asterite merged 1 commit intocrystal-lang:masterfrom
Sija:bigdecimal-with-bigrational
Feb 10, 2018
Merged

Support BigDecimal comparison with and initialization from BigRational#5675
asterite merged 1 commit intocrystal-lang:masterfrom
Sija:bigdecimal-with-bigrational

Conversation

@Sija
Copy link
Copy Markdown
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

Comment thread src/big/big_decimal.cr Outdated

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

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
Copy Markdown
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
Copy Markdown
Member

@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
Comment thread src/big/big_decimal.cr Outdated
end

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

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
Copy Markdown
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
Copy Markdown
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.

3 participants