Whether to use String or Integer in BigDecimal in Ruby
Asked Answered
O

1

10

Referring to the documentation of BigDecimal class,

n,m = a.precs
prec returns number of significant digits (n) and maximum number of significant digits (m) of a.

I am puzzled by the following output related to BigDecimal.

require 'bigdecimal'    
BigDecimal.new('1').precs # => [9, 18] 
BigDecimal.new(1).precs   # => [9, 27]

I cannot figure out why when a String is passed, the maximum number of significant digits is less compared to when a Fixnum is passed.

Also will it result in any precision issues?

Orelia answered 18/12, 2015 at 4:6 Comment(4)
The constructor does take a second argument if you want consistency: BigDecimal.new('1', 27).precs for example. And the documentation does say that the "number of significant digits is determined from the initial value" if you don't specify it yourself.Saccule
@muistooshort your right that we can specify that. But my question was why the precs are different in the first place?Orelia
Here are more data points. The following observations refer to strings containing the character 1 only (e.g., "111111") and numbers whose digits are all 1's (e.g., 111111). For fewer than 10 characters, [9,18] is returned. For fewer than 10 digits, [9,27] is returned. Between10 and 18 characters or digits, [18,27] is returned (i.e., the same tuple is returned for both strings and numbers), between 19 and some greater number of characters or digits, [27,36] is returned. Aside: I suggest you add require 'bigdecimal' to your code.Caveman
@CarySwoveland, thanks for your findings.Orelia
P
4

If you can read C code, you can start at https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2509 - that's the initializer for any BigDecimal object. If you follow that code to the next method, which is BigDecimal_new you'll notice that when passed an integer argument there are a few more steps to go through before allocating and creating an internal big decimal object as opposed to when passing a string argument.

In any case, you shouldn't worry about loss of precision - the significant digits attributes are more like hints than absolute values. Even the documentation mentions it: The actual number of significant digits used in computation is usually larger than the specified number.

Pelagic answered 18/12, 2015 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.