From: muraken@... Date: 2016-12-10T07:26:09+00:00 Subject: [ruby-core:78577] [Ruby trunk Bug#8826] BigDecimal#div and #quo different behavior and inconsistencies Issue #8826 has been updated by Kenta Murata. Description updated Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN to 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- Bug #8826: BigDecimal#div and #quo different behavior and inconsistencies https://bugs.ruby-lang.org/issues/8826#change-61962 * Author: F��ldes L��szl�� * Status: Assigned * Priority: Normal * Assignee: Kenta Murata * ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- BigDecimal's #div and #quo method behave differently despite the #div documentation says: "See BigDecimal#quo" #div returns Fixnum if there is no precision argument for #div (this parameter is not documented): ``` 2.0.0-p247 :018 > BigDecimal(5).div(5).class => Fixnum 2.0.0-p247 :031 > BigDecimal(5).div(5.1).class => Fixnum ``` #div returns Fixnum even for a Float argument: ``` 2.0.0-p247 :118 > BigDecimal(5).div(5.01) => 0 ``` It returns Fixnum even if every argument is BigDecimal: ``` 2.0.0-p247 :043 > BigDecimal(5).div(BigDecimal(5.1,5)).class => Fixnum ``` When provided the precision argument, #div returns BigDecimal: ``` 2.0.0-p247 :036 > BigDecimal(5).div(5,8).class => BigDecimal 2.0.0-p247 :131 > BigDecimal(5).div(BigDecimal(5.1,5),8).class => BigDecimal ``` But first argument cannot be Float along with precision: ``` 2.0.0-p247 :032 > BigDecimal(5).div(5.1,8).class ArgumentError: Float can't be coerced into BigDecimal without a precision from (irb):32:in `div' from (irb):32 from /home/karatedog/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `
' ``` Whereas #quo does not accept a precision argument and returns BigDecimal (hence no configurable precision here, although the documentation says that #quo applies round operation): ``` 2.0.0-p247 :121 > BigDecimal(5).quo(5.01) => # ``` Circumventing the precision with class method does not work on #quo, it's like the limit is maxed: ``` 2.0.0-p247 :135 > BigDecimal::limit(5) => 5 2.0.0-p247 :136 > BigDecimal(1).quo(3) => # 2.0.0-p247 :080 > BigDecimal::limit(50) => 5 2.0.0-p247 :081 > BigDecimal(1).quo(3) => # ``` Precision does not seem to be automatic: ``` 2.0.0-p247 :141 > BigDecimal::limit(500) => 100 2.0.0-p247 :142 > BigDecimal(1).quo(229) => # ``` 229 is a full period prime, its reciprocal yields 228 fractional digits before repetition. Whereas #div's precision can be larger than #div's: ``` 2.0.0-p247 :109 > BigDecimal(1).div(3,19) => # ``` And for 229: ``` 2.0.0-p247 :144 > BigDecimal(1).div(229,250) => #