Project

General

Profile

Bug #13754

bigdecimal with lower precision that Float

Added by lionel_perrin (Lionel PERRIN) over 2 years ago. Updated over 1 year ago.

Status:
Assigned
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x64-mingw32]
[ruby-core:82107]

Description

Hello,

I'm not sure if I've misunderstood the bigdecimal class but in the following example, I only get 12 significant digits using bigdecimal while using Float, I get a correct value with 17 significant digits.

# using floats
101/0.9163472602589686 # 110.22022368622177 (OK: floating point computation)

# using bigdecimal
a = BigDecimal('101'); a.precs # [9, 18] 
b = BigDecimal('0.9163472602589686'); b.precs # [18, 27]
c = a/b; c.precs # [18, 36]              (OK: I understand that c is computed with 18 significant digits)
c.to_s # "0.110220223686e3"              (Mmm: I see only 12 significant digits)
c - BigDecimal('0.110220223686e3') # 0.0 (Looks like c only stores 12 significant digits and not 18)

Using the Rational class, I've seen that the value I'm expecting is about:

BigDecimal.new(Rational(101/Rational('0.9163472602589686')), 25) # 0.1102202236862217746799312e3

History

#1

Updated by lionel_perrin (Lionel PERRIN) over 2 years ago

  • Description updated (diff)
#2

Updated by lionel_perrin (Lionel PERRIN) over 2 years ago

  • Description updated (diff)
#3

Updated by lionel_perrin (Lionel PERRIN) over 2 years ago

  • Description updated (diff)
#4

Updated by lionel_perrin (Lionel PERRIN) over 2 years ago

  • Description updated (diff)

Updated by mrkn (Kenta Murata) over 2 years ago

  • Assignee set to mrkn (Kenta Murata)
  • Status changed from Open to Assigned
#7

Updated by naruse (Yui NARUSE) almost 2 years ago

  • Target version deleted (2.6)

Updated by karatedog (Földes László) over 1 year ago

That is the same problem as here: https://bugs.ruby-lang.org/issues/8826
#/ is the same method as #quo (according to documentation both methods are defined in 'bigdecimal.c' at line 1281). Currently you can divide a bigdecimal by using #/, #quo and #div but I don't really understand the design behind these methods (on a "which should do what" level).

#div accepts a precision argument, while #quo does not. Without precision argument #div returns Fixnum even if its first argument is a Float, it even returns Fixnum if both divisor and dividend are Float..
Thus far I don't know any method that could be able to calculate a division AND set the proper precision on the result. What you can do is to manually set precision by using #div. If you set the precision to the same amount as the divisor, you will not miss any significant digits, the drawback is that you will see a lot of digit repetition for most of the numbers.

(1019 is a long prime, its reciprocal has 1018 significant digits)

> BigDecimal(1).div(1019,1019).to_s

Also available in: Atom PDF