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
) ofa
.
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?
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. – Sacculeprecs
are different in the first place? – Orelia1
only (e.g.,"111111"
) and numbers whose digits are all1
's (e.g.,111111
). For fewer than10
characters,[9,18]
is returned. For fewer than10
digits,[9,27]
is returned. Between10
and18
characters or digits,[18,27]
is returned (i.e., the same tuple is returned for both strings and numbers), between19
and some greater number of characters or digits,[27,36]
is returned. Aside: I suggest you addrequire 'bigdecimal'
to your code. – Caveman