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

[Bug fix] Handle arithmetic overflow in BigDecimal#div #10628

Merged
merged 3 commits into from
Jun 4, 2021

Conversation

kellydanma
Copy link
Contributor

@kellydanma kellydanma commented Apr 12, 2021

Closes #10502

  • 1. handle overflow
  • 2. write tests

@straight-shoota
Copy link
Member

I think the implementation could be much simplerl Just don't use a variable for scale and replace with the expression directly.

There are two unrelated reads from the variable:

  • We can be sure that @scale >= other.scale in the remainder == ZERO branch (otherwise the remainder couldn't be zero). So there is no further test required and we can just use @scale - other.@scale.
  • In the final expression of the method's main branch, scale + i can be expanded and restructured to avoid an intermediary negative value: @scale + i - other.scale

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric labels Apr 12, 2021
@kellydanma
Copy link
Contributor Author

We can be sure that @scale >= other.scale in the remainder == ZERO branch (otherwise the remainder couldn't be zero)

I thought so too, but consider https://play.crystal-lang.org/#/r/auvc

@straight-shoota
Copy link
Member

Oh, right.

Taking some more inspiration from https://github.com/akubera/bigdecimal-rs/blob/fc797c302f4947e429cc331febc66fe3c7badc5e/src/lib.rs#L1273 wouldn't be a bad idea. It seems there's actually a lot missing in our implementation.
And there are only a hand full of specs which don't even go near any edge cases.

@kellydanma
Copy link
Contributor Author

Taking some more inspiration from https://github.com/akubera/bigdecimal-rs/blob/fc797c302f4947e429cc331febc66fe3c7badc5e/src/lib.rs#L1273 wouldn't be a bad idea.

I was also thinking this. But it seems out of scope for a bug fix. If there's an open issue to rewrite #div, I would be happy to work on it. I will have a lot of free time next weekend!

Do you think the current implementation is sufficient for the bug fix?

@straight-shoota
Copy link
Member

I suppose it's probably fine to merge this as is. But I'd prefer to overhaul the entire method's implementation, not just fix a single bug. There's likely more.
I don't think there's an issue to track this, but that's fine. If you want to go ahead with this, just push more changes to this PR. I'm very happy to wait for a bigger refactor.

@kellydanma
Copy link
Contributor Author

Personally, I'd prefer for this PR to only handle the bug fix.

I haven't had time to dig into lib.rs yet. I'm new to Crystal and would appreciate limiting the scope here to the original issue so there's no pressure for me to rewrite the method 😅

@straight-shoota
Copy link
Member

Well my fear is that this bug fix isn't much of a help. Because it's pretty certain that many other things are broken in that method. Maybe in other methods, too. Hence I'd like a complete overhaul.

You should not feel pressured at! Why don't you try things out, see how far you get? You can ask for help here or better in chat. And if you can't get it done, someone else can take over.

@asterite
Copy link
Member

Could we change it from UInt64 to Int64 like in Rust at least? I think that would solve most of the issues, right?

@straight-shoota
Copy link
Member

The unsigned overflow should already be fixed with the current state of this PR.

@kellydanma
Copy link
Contributor Author

kellydanma commented Apr 14, 2021

I think this bug fix, while small, still addresses the original issue entirely. I would feel more comfortable merging a small bug fix as my first contribution, to be honest. I don't understand why a #div rewrite has to be done in a single PR. Let me know if this is possible! 😄

That doesn't mean I won't help with the BigDecimal overhaul in the future though; even changing UInt64 to Int64 is beyond the scope of a #div rewrite. I would be happy to contribute more actively once school is done.

@asterite
Copy link
Member

I think this bug fix, while small, still addresses the original issue entirely

Me too. I don't think there's a need to wait to do an entire refactor for possible bugs, if those bugs are yet unknown. And this PR already fixes one of them, while waiting for a refactor won't.

@kellydanma kellydanma marked this pull request as ready for review April 14, 2021 14:12
@kellydanma kellydanma changed the title WIP : Handle arithmetic overflow in #div [#10502] Handle arithmetic overflow in #div Apr 14, 2021
@kellydanma kellydanma changed the title [#10502] Handle arithmetic overflow in #div [Bug fix] Handle arithmetic overflow in BigDecimal#div Apr 15, 2021
@kellydanma kellydanma changed the title [Bug fix] Handle arithmetic overflow in BigDecimal#div [Bug fix] Handle arithmetic overflow in BigDecimal#div Apr 15, 2021
@asterite asterite added this to the 1.1.0 milestone Jun 4, 2021
@asterite asterite merged commit 66a0e10 into crystal-lang:master Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Arithmetic overflow in BigDecimal
4 participants