-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix: add % method to BigDecimal #9784
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Could you add some tests?
Sure! I assume I'd just add some files in the spec directory? |
All done, should have some specs now! |
Thanks! A couple of things:
|
My bad, first time contributing. I'll go ahead and fix that! |
I was looking at the BigDecimal specs though, and most of them are written similar to this: BigDecimal.new(1).should eq(BigDecimal.new(1) / BigDecimal.new(1)) |
Yeah, I think I was absent when BigDecimal was introduced, the specs could have been written in a better way. For starters, we never have a huge single spec to test different methods. There's so much to improve in this class. Since I don't have knowledge of this and I have very little time now, I'll leave others to review this. Thank you again! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
It might be nice to also have some tests that check that decimals are properly divided. For example checking the remainder of 5678.912 % 12.34. After all these are decimal types :-)
Thanks @sugarfi , I think that we need a couple of more specs before merging this to ensure things works as in float. In describe "modulo" do
it "raises when mods by zero" do
expect_raises(DivisionByZeroError) { 1.2.modulo 0.0 }
end
it { (13.0.modulo 4.0).should eq(1.0) }
it { (13.0.modulo -4.0).should eq(-3.0) }
it { (-13.0.modulo 4.0).should eq(3.0) }
it { (-13.0.modulo -4.0).should eq(-1.0) }
it { (11.5.modulo 4.0).should eq(3.5) }
it { (11.5.modulo -4.0).should eq(-0.5) }
it { (-11.5.modulo 4.0).should eq(0.5) }
it { (-11.5.modulo -4.0).should eq(-3.5) }
end The same test cases in BigDecimal would be enough. float also has |
@bcardiff ok, will update the spec. I did implement this modulo using the built-in |
These are the specs I just added, anything I need to change before I test and commit? it "handles modulus correctly" do
(BigDecimal.new(13.0) % BigDecimal.new(4.0)).should eq(BigDecimal.new(1.0))
(BigDecimal.new(13.0) % BigDecimal.new(-4.0)).should eq(BigDecimal.new(-3.0))
(BigDecimal.new(-13.0) % BigDecimal.new(4.0)).should eq(BigDecimal.new(3.0))
(BigDecimal.new(-13.0) % BigDecimal.new(-4.0)).should eq(BigDecimal.new(-1.0))
(BigDecimal.new(11.5) % BigDecimal.new(4.0)).should eq(BigDecimal.new(3.5))
(BigDecimal.new(11.5) % BigDecimal.new(-4.0)).should eq(BigDecimal.new(-0.5))
(BigDecimal.new(-11.5) % BigDecimal.new(4.0)).should eq(BigDecimal.new(0.5))
(BigDecimal.new(-11.5) % BigDecimal.new(-4.0)).should eq(BigDecimal.new(-3.5))
end |
The I think they will all work directly thanks to the implementation, but better be safe. I don't follow why you said: will that cause floating point noise issues?. The BigDecimal % implementation uses BigInt one. There are no floats involved I think. |
end | ||
end | ||
|
||
def %(other : Int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same overload could be added for Float
perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll add that. Thanks!
Just realized that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is good to go. But since you add BigDecimal#**(BigDecimal)
, aren't we also missing BigDecimal#**(Float)
? It can be added later, just pointing out the lack of it.
Also, when I went to the integer division refactor I started leaving only the operations within the same type in the main file of the type, and moving to big/number.cr
all the convenient operations to operate between different types. There is no need to change things on this, but I wanted to share the criteria.
Oh, please run the |
Will do!
…On Sat, Oct 3, 2020 at 8:35 PM Brian J. Cardiff ***@***.***> wrote:
Oh, please run the $ crystal tool format ./src/big/big_decimal.cr to make
the CI happy. The formatter can be integrated with the editor of your
preference.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9784 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKXXAMTKYWRBESOCI2YZLRTSI67LVANCNFSM4SAJGELA>
.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the ** changes. Please keep PR simple. I already reviewed this, now I need to review it again, it's much more work for reviewers. It's much simpler to send one PR per thing to discuss/add.
Oh, my bad. Will revert!
…On Sun, Oct 4, 2020 at 6:34 AM Ary Borenszweig ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Please revert the ** changes. Please keep PR simple. I already reviewed
this, now I need to review it again, it's much more work for reviewers.
It's much simpler to send one PR per thing to discuss/add.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9784 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKXXAMVG4VPVUZC7XR33LRLSJBFU3ANCNFSM4SAJGELA>
.
|
@sugarfi with the force push I think you discarded the changes requested in #9784 (comment) |
@bcardiff sorry for the delay, was caught up with other stuff. The specs should be re-added now. Thank you for your patience! |
This should be reopened, I guess. |
Hm, I don't think this reopening works. The original repo and branch appear to have completely vanished. And GitHub even fails to reconstruct the commit contents. |
From ghosted PR crystal-lang#9784
Closing in favor of #13255. |
This PR adds a
%
method to theBigDecimal
class insrc/big/big_decimal.cr
. Should fix #9782 .