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

Accept a Float value in BigDecimal#div when a precision is given #212

Closed
mrkn opened this issue Dec 3, 2021 · 2 comments · Fixed by #215
Closed

Accept a Float value in BigDecimal#div when a precision is given #212

mrkn opened this issue Dec 3, 2021 · 2 comments · Fixed by #215

Comments

@mrkn
Copy link
Member

mrkn commented Dec 3, 2021

See https://bugs.ruby-lang.org/issues/8826

irb(main):021:0> BigDecimal(5).div(5.1)
=> 0
irb(main):022:0> BigDecimal(5).div(5.1, 8)
(irb):22:in `div': can't omit precision for a Float. (ArgumentError)
        from (irb):22:in `<main>'
        from /home/mrkn/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/irb-1.3.8.pre.9/exe/irb:11:in `<top (required)>'
        from /home/mrkn/.rbenv/versions/3.0/bin/irb:23:in `load'
        from /home/mrkn/.rbenv/versions/3.0/bin/irb:23:in `<main>'

It is good that the second case works well.

@mrkn mrkn changed the title Accept a Float value without a precision in BigDecimal#div Accept a Float value in BigDecimal#div when a precision is given Dec 3, 2021
@mrkn
Copy link
Member Author

mrkn commented Dec 6, 2021

irb(main):001:0> BigDecimal(5).div(5.1.to_d, 8)
=> 0.98039216e0
irb(main):002:0> BigDecimal(5).div(BigDecimal(5.1, 8))
=> 0
irb(main):003:0> BigDecimal(5).div(BigDecimal(5.1, 8), 8)
=> 0.98039216e0

The last case may be the appropriate behavior of BigDecimal(5).div(5.1, 8).

@mrkn
Copy link
Member Author

mrkn commented Dec 7, 2021

The remaining problem is the case that the precision is larger than Float::DIG+1.

irb(main):001:0> BigDecimal(5).div(5.1)
=> 0
irb(main):002:0> BigDecimal(5).div(5.1, 8)
=> 0.98039216e
irb(main):003:0> BigDecimal(5).div(5.1, Float::DIG+1)
=> 0.9803921568627451e0
irb(main):004:0> BigDecimal(5).div(5.1, Float::DIG+2)
(irb):3:in `div': precision too large. (ArgumentError)
        from (irb):3:in `<main>'
        from /home/mrkn/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/irb-1.3.8.pre.9/exe/irb:11:in `<top (required)>'
        from /home/mrkn/.rbenv/versions/3.0/bin/irb:23:in `load'
        from /home/mrkn/.rbenv/versions/3.0/bin/irb:23:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant