dec.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // Package inf (type inf.Dec) implements "infinite-precision" decimal
  2. // arithmetic.
  3. // "Infinite precision" describes two characteristics: practically unlimited
  4. // precision for decimal number representation and no support for calculating
  5. // with any specific fixed precision.
  6. // (Although there is no practical limit on precision, inf.Dec can only
  7. // represent finite decimals.)
  8. //
  9. // This package is currently in experimental stage and the API may change.
  10. //
  11. // This package does NOT support:
  12. // - rounding to specific precisions (as opposed to specific decimal positions)
  13. // - the notion of context (each rounding must be explicit)
  14. // - NaN and Inf values, and distinguishing between positive and negative zero
  15. // - conversions to and from float32/64 types
  16. //
  17. // Features considered for possible addition:
  18. // + formatting options
  19. // + Exp method
  20. // + combined operations such as AddRound/MulAdd etc
  21. // + exchanging data in decimal32/64/128 formats
  22. //
  23. // API Changes
  24. //
  25. // Compared to http://code.google.com/p/godec , the following APIs have changed:
  26. //
  27. // godec inf.Dec rationale
  28. // ---------------------------------------------------------------------------
  29. // NewDecInt64(u) NewDec(u,0) favor common case for short API
  30. // NewDec(u,s) NewDecBig(b,i) favor common case for short API
  31. // z.SetUnscaled(u) z.SetUnscaledBig(u) favor common case for short API
  32. // (n/a) z.SetUnscaled(u) favor common case for short API
  33. // x.Unscaled() x.UnscaledBig() consistency (NewDec*/SetUnscaled*)
  34. // (n/a) x.Unscaled() consistency (NewDec*/SetUnscaled*)
  35. // z.Quo(x,y,s,r) z.QuoRound(x,y,s,r) consistency (combined operation)
  36. // type Scaler (unexported) hide implementation details
  37. //
  38. package inf
  39. // TODO:
  40. // - avoid excessive deep copying (quo and rounders)
  41. // - use Round (not Quo) in rounding examples
  42. import (
  43. "fmt"
  44. "io"
  45. "math/big"
  46. "strings"
  47. )
  48. // A Dec represents a signed arbitrary-precision decimal.
  49. // It is a combination of a sign, an arbitrary-precision integer coefficient
  50. // value, and a signed fixed-precision exponent value.
  51. // The sign and the coefficient value are handled together as a signed value
  52. // and referred to as the unscaled value.
  53. // (Positive and negative zero values are not distinguished.)
  54. // Since the exponent is most commonly non-positive, it is handled in negated
  55. // form and referred to as scale.
  56. //
  57. // The mathematical value of a Dec equals:
  58. //
  59. // unscaled * 10**(-scale)
  60. //
  61. // Note that different Dec representations may have equal mathematical values.
  62. //
  63. // unscaled scale String()
  64. // -------------------------
  65. // 0 0 "0"
  66. // 0 2 "0.00"
  67. // 0 -2 "0"
  68. // 1 0 "1"
  69. // 100 2 "1.00"
  70. // 10 0 "10"
  71. // 1 -1 "10"
  72. //
  73. // The zero value for a Dec represents the value 0 with scale 0.
  74. //
  75. // Operations are typically performed through the *Dec type.
  76. // The semantics of the assignment operation "=" for "bare" Dec values is
  77. // undefined and should not be relied on.
  78. //
  79. // Methods are typically of the form:
  80. //
  81. // func (z *Dec) Op(x, y *Dec) *Dec
  82. //
  83. // and implement operations z = x Op y with the result as receiver; if it
  84. // is one of the operands it may be overwritten (and its memory reused).
  85. // To enable chaining of operations, the result is also returned. Methods
  86. // returning a result other than *Dec take one of the operands as the receiver.
  87. //
  88. // A "bare" Quo method (quotient / division operation) is not provided, as the
  89. // result is not always a finite decimal and thus in general cannot be
  90. // represented as a Dec.
  91. // Instead, in the common case when rounding is (potentially) necessary,
  92. // QuoRound should be used with a Scale and a Rounder.
  93. // QuoExact or QuoRound with RoundExact can be used in the special cases when it
  94. // is known that the result is always a finite decimal.
  95. //
  96. type Dec struct {
  97. unscaled big.Int
  98. scale Scale
  99. }
  100. // Scale represents the type used for the scale of a Dec.
  101. type Scale int32
  102. const scaleSize = 4 // bytes in a Scale value
  103. // Scaler represents a method for obtaining the scale to use for the result of
  104. // an operation on x and y.
  105. type scaler interface {
  106. Scale(x *Dec, y *Dec) Scale
  107. }
  108. var bigInt = [...]*big.Int{
  109. big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4),
  110. big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9),
  111. big.NewInt(10),
  112. }
  113. var exp10cache [64]big.Int = func() [64]big.Int {
  114. e10, e10i := [64]big.Int{}, bigInt[1]
  115. for i, _ := range e10 {
  116. e10[i].Set(e10i)
  117. e10i = new(big.Int).Mul(e10i, bigInt[10])
  118. }
  119. return e10
  120. }()
  121. // NewDec allocates and returns a new Dec set to the given int64 unscaled value
  122. // and scale.
  123. func NewDec(unscaled int64, scale Scale) *Dec {
  124. return new(Dec).SetUnscaled(unscaled).SetScale(scale)
  125. }
  126. // NewDecBig allocates and returns a new Dec set to the given *big.Int unscaled
  127. // value and scale.
  128. func NewDecBig(unscaled *big.Int, scale Scale) *Dec {
  129. return new(Dec).SetUnscaledBig(unscaled).SetScale(scale)
  130. }
  131. // Scale returns the scale of x.
  132. func (x *Dec) Scale() Scale {
  133. return x.scale
  134. }
  135. // Unscaled returns the unscaled value of x for u and true for ok when the
  136. // unscaled value can be represented as int64; otherwise it returns an undefined
  137. // int64 value for u and false for ok. Use x.UnscaledBig().Int64() to avoid
  138. // checking the validity of the value when the check is known to be redundant.
  139. func (x *Dec) Unscaled() (u int64, ok bool) {
  140. u = x.unscaled.Int64()
  141. var i big.Int
  142. ok = i.SetInt64(u).Cmp(&x.unscaled) == 0
  143. return
  144. }
  145. // UnscaledBig returns the unscaled value of x as *big.Int.
  146. func (x *Dec) UnscaledBig() *big.Int {
  147. return &x.unscaled
  148. }
  149. // SetScale sets the scale of z, with the unscaled value unchanged, and returns
  150. // z.
  151. // The mathematical value of the Dec changes as if it was multiplied by
  152. // 10**(oldscale-scale).
  153. func (z *Dec) SetScale(scale Scale) *Dec {
  154. z.scale = scale
  155. return z
  156. }
  157. // SetUnscaled sets the unscaled value of z, with the scale unchanged, and
  158. // returns z.
  159. func (z *Dec) SetUnscaled(unscaled int64) *Dec {
  160. z.unscaled.SetInt64(unscaled)
  161. return z
  162. }
  163. // SetUnscaledBig sets the unscaled value of z, with the scale unchanged, and
  164. // returns z.
  165. func (z *Dec) SetUnscaledBig(unscaled *big.Int) *Dec {
  166. z.unscaled.Set(unscaled)
  167. return z
  168. }
  169. // Set sets z to the value of x and returns z.
  170. // It does nothing if z == x.
  171. func (z *Dec) Set(x *Dec) *Dec {
  172. if z != x {
  173. z.SetUnscaledBig(x.UnscaledBig())
  174. z.SetScale(x.Scale())
  175. }
  176. return z
  177. }
  178. // Sign returns:
  179. //
  180. // -1 if x < 0
  181. // 0 if x == 0
  182. // +1 if x > 0
  183. //
  184. func (x *Dec) Sign() int {
  185. return x.UnscaledBig().Sign()
  186. }
  187. // Neg sets z to -x and returns z.
  188. func (z *Dec) Neg(x *Dec) *Dec {
  189. z.SetScale(x.Scale())
  190. z.UnscaledBig().Neg(x.UnscaledBig())
  191. return z
  192. }
  193. // Cmp compares x and y and returns:
  194. //
  195. // -1 if x < y
  196. // 0 if x == y
  197. // +1 if x > y
  198. //
  199. func (x *Dec) Cmp(y *Dec) int {
  200. xx, yy := upscale(x, y)
  201. return xx.UnscaledBig().Cmp(yy.UnscaledBig())
  202. }
  203. // Abs sets z to |x| (the absolute value of x) and returns z.
  204. func (z *Dec) Abs(x *Dec) *Dec {
  205. z.SetScale(x.Scale())
  206. z.UnscaledBig().Abs(x.UnscaledBig())
  207. return z
  208. }
  209. // Add sets z to the sum x+y and returns z.
  210. // The scale of z is the greater of the scales of x and y.
  211. func (z *Dec) Add(x, y *Dec) *Dec {
  212. xx, yy := upscale(x, y)
  213. z.SetScale(xx.Scale())
  214. z.UnscaledBig().Add(xx.UnscaledBig(), yy.UnscaledBig())
  215. return z
  216. }
  217. // Sub sets z to the difference x-y and returns z.
  218. // The scale of z is the greater of the scales of x and y.
  219. func (z *Dec) Sub(x, y *Dec) *Dec {
  220. xx, yy := upscale(x, y)
  221. z.SetScale(xx.Scale())
  222. z.UnscaledBig().Sub(xx.UnscaledBig(), yy.UnscaledBig())
  223. return z
  224. }
  225. // Mul sets z to the product x*y and returns z.
  226. // The scale of z is the sum of the scales of x and y.
  227. func (z *Dec) Mul(x, y *Dec) *Dec {
  228. z.SetScale(x.Scale() + y.Scale())
  229. z.UnscaledBig().Mul(x.UnscaledBig(), y.UnscaledBig())
  230. return z
  231. }
  232. // Round sets z to the value of x rounded to Scale s using Rounder r, and
  233. // returns z.
  234. func (z *Dec) Round(x *Dec, s Scale, r Rounder) *Dec {
  235. return z.QuoRound(x, NewDec(1, 0), s, r)
  236. }
  237. // QuoRound sets z to the quotient x/y, rounded using the given Rounder to the
  238. // specified scale.
  239. //
  240. // If the rounder is RoundExact but the result can not be expressed exactly at
  241. // the specified scale, QuoRound returns nil, and the value of z is undefined.
  242. //
  243. // There is no corresponding Div method; the equivalent can be achieved through
  244. // the choice of Rounder used.
  245. //
  246. func (z *Dec) QuoRound(x, y *Dec, s Scale, r Rounder) *Dec {
  247. return z.quo(x, y, sclr{s}, r)
  248. }
  249. func (z *Dec) quo(x, y *Dec, s scaler, r Rounder) *Dec {
  250. scl := s.Scale(x, y)
  251. var zzz *Dec
  252. if r.UseRemainder() {
  253. zz, rA, rB := new(Dec).quoRem(x, y, scl, true, new(big.Int), new(big.Int))
  254. zzz = r.Round(new(Dec), zz, rA, rB)
  255. } else {
  256. zz, _, _ := new(Dec).quoRem(x, y, scl, false, nil, nil)
  257. zzz = r.Round(new(Dec), zz, nil, nil)
  258. }
  259. if zzz == nil {
  260. return nil
  261. }
  262. return z.Set(zzz)
  263. }
  264. // QuoExact sets z to the quotient x/y and returns z when x/y is a finite
  265. // decimal. Otherwise it returns nil and the value of z is undefined.
  266. //
  267. // The scale of a non-nil result is "x.Scale() - y.Scale()" or greater; it is
  268. // calculated so that the remainder will be zero whenever x/y is a finite
  269. // decimal.
  270. func (z *Dec) QuoExact(x, y *Dec) *Dec {
  271. return z.quo(x, y, scaleQuoExact{}, RoundExact)
  272. }
  273. // quoRem sets z to the quotient x/y with the scale s, and if useRem is true,
  274. // it sets remNum and remDen to the numerator and denominator of the remainder.
  275. // It returns z, remNum and remDen.
  276. //
  277. // The remainder is normalized to the range -1 < r < 1 to simplify rounding;
  278. // that is, the results satisfy the following equation:
  279. //
  280. // x / y = z + (remNum/remDen) * 10**(-z.Scale())
  281. //
  282. // See Rounder for more details about rounding.
  283. //
  284. func (z *Dec) quoRem(x, y *Dec, s Scale, useRem bool,
  285. remNum, remDen *big.Int) (*Dec, *big.Int, *big.Int) {
  286. // difference (required adjustment) compared to "canonical" result scale
  287. shift := s - (x.Scale() - y.Scale())
  288. // pointers to adjusted unscaled dividend and divisor
  289. var ix, iy *big.Int
  290. switch {
  291. case shift > 0:
  292. // increased scale: decimal-shift dividend left
  293. ix = new(big.Int).Mul(x.UnscaledBig(), exp10(shift))
  294. iy = y.UnscaledBig()
  295. case shift < 0:
  296. // decreased scale: decimal-shift divisor left
  297. ix = x.UnscaledBig()
  298. iy = new(big.Int).Mul(y.UnscaledBig(), exp10(-shift))
  299. default:
  300. ix = x.UnscaledBig()
  301. iy = y.UnscaledBig()
  302. }
  303. // save a copy of iy in case it to be overwritten with the result
  304. iy2 := iy
  305. if iy == z.UnscaledBig() {
  306. iy2 = new(big.Int).Set(iy)
  307. }
  308. // set scale
  309. z.SetScale(s)
  310. // set unscaled
  311. if useRem {
  312. // Int division
  313. _, intr := z.UnscaledBig().QuoRem(ix, iy, new(big.Int))
  314. // set remainder
  315. remNum.Set(intr)
  316. remDen.Set(iy2)
  317. } else {
  318. z.UnscaledBig().Quo(ix, iy)
  319. }
  320. return z, remNum, remDen
  321. }
  322. type sclr struct{ s Scale }
  323. func (s sclr) Scale(x, y *Dec) Scale {
  324. return s.s
  325. }
  326. type scaleQuoExact struct{}
  327. func (sqe scaleQuoExact) Scale(x, y *Dec) Scale {
  328. rem := new(big.Rat).SetFrac(x.UnscaledBig(), y.UnscaledBig())
  329. f2, f5 := factor2(rem.Denom()), factor(rem.Denom(), bigInt[5])
  330. var f10 Scale
  331. if f2 > f5 {
  332. f10 = Scale(f2)
  333. } else {
  334. f10 = Scale(f5)
  335. }
  336. return x.Scale() - y.Scale() + f10
  337. }
  338. func factor(n *big.Int, p *big.Int) int {
  339. // could be improved for large factors
  340. d, f := n, 0
  341. for {
  342. dd, dm := new(big.Int).DivMod(d, p, new(big.Int))
  343. if dm.Sign() == 0 {
  344. f++
  345. d = dd
  346. } else {
  347. break
  348. }
  349. }
  350. return f
  351. }
  352. func factor2(n *big.Int) int {
  353. // could be improved for large factors
  354. f := 0
  355. for ; n.Bit(f) == 0; f++ {
  356. }
  357. return f
  358. }
  359. func upscale(a, b *Dec) (*Dec, *Dec) {
  360. if a.Scale() == b.Scale() {
  361. return a, b
  362. }
  363. if a.Scale() > b.Scale() {
  364. bb := b.rescale(a.Scale())
  365. return a, bb
  366. }
  367. aa := a.rescale(b.Scale())
  368. return aa, b
  369. }
  370. func exp10(x Scale) *big.Int {
  371. if int(x) < len(exp10cache) {
  372. return &exp10cache[int(x)]
  373. }
  374. return new(big.Int).Exp(bigInt[10], big.NewInt(int64(x)), nil)
  375. }
  376. func (x *Dec) rescale(newScale Scale) *Dec {
  377. shift := newScale - x.Scale()
  378. switch {
  379. case shift < 0:
  380. e := exp10(-shift)
  381. return NewDecBig(new(big.Int).Quo(x.UnscaledBig(), e), newScale)
  382. case shift > 0:
  383. e := exp10(shift)
  384. return NewDecBig(new(big.Int).Mul(x.UnscaledBig(), e), newScale)
  385. }
  386. return x
  387. }
  388. var zeros = []byte("00000000000000000000000000000000" +
  389. "00000000000000000000000000000000")
  390. var lzeros = Scale(len(zeros))
  391. func appendZeros(s []byte, n Scale) []byte {
  392. for i := Scale(0); i < n; i += lzeros {
  393. if n > i+lzeros {
  394. s = append(s, zeros...)
  395. } else {
  396. s = append(s, zeros[0:n-i]...)
  397. }
  398. }
  399. return s
  400. }
  401. func (x *Dec) String() string {
  402. if x == nil {
  403. return "<nil>"
  404. }
  405. scale := x.Scale()
  406. s := []byte(x.UnscaledBig().String())
  407. if scale <= 0 {
  408. if scale != 0 && x.unscaled.Sign() != 0 {
  409. s = appendZeros(s, -scale)
  410. }
  411. return string(s)
  412. }
  413. negbit := Scale(-((x.Sign() - 1) / 2))
  414. // scale > 0
  415. lens := Scale(len(s))
  416. if lens-negbit <= scale {
  417. ss := make([]byte, 0, scale+2)
  418. if negbit == 1 {
  419. ss = append(ss, '-')
  420. }
  421. ss = append(ss, '0', '.')
  422. ss = appendZeros(ss, scale-lens+negbit)
  423. ss = append(ss, s[negbit:]...)
  424. return string(ss)
  425. }
  426. // lens > scale
  427. ss := make([]byte, 0, lens+1)
  428. ss = append(ss, s[:lens-scale]...)
  429. ss = append(ss, '.')
  430. ss = append(ss, s[lens-scale:]...)
  431. return string(ss)
  432. }
  433. // Format is a support routine for fmt.Formatter. It accepts the decimal
  434. // formats 'd' and 'f', and handles both equivalently.
  435. // Width, precision, flags and bases 2, 8, 16 are not supported.
  436. func (x *Dec) Format(s fmt.State, ch rune) {
  437. if ch != 'd' && ch != 'f' && ch != 'v' && ch != 's' {
  438. fmt.Fprintf(s, "%%!%c(dec.Dec=%s)", ch, x.String())
  439. return
  440. }
  441. fmt.Fprintf(s, x.String())
  442. }
  443. func (z *Dec) scan(r io.RuneScanner) (*Dec, error) {
  444. unscaled := make([]byte, 0, 256) // collects chars of unscaled as bytes
  445. dp, dg := -1, -1 // indexes of decimal point, first digit
  446. loop:
  447. for {
  448. ch, _, err := r.ReadRune()
  449. if err == io.EOF {
  450. break loop
  451. }
  452. if err != nil {
  453. return nil, err
  454. }
  455. switch {
  456. case ch == '+' || ch == '-':
  457. if len(unscaled) > 0 || dp >= 0 { // must be first character
  458. r.UnreadRune()
  459. break loop
  460. }
  461. case ch == '.':
  462. if dp >= 0 {
  463. r.UnreadRune()
  464. break loop
  465. }
  466. dp = len(unscaled)
  467. continue // don't add to unscaled
  468. case ch >= '0' && ch <= '9':
  469. if dg == -1 {
  470. dg = len(unscaled)
  471. }
  472. default:
  473. r.UnreadRune()
  474. break loop
  475. }
  476. unscaled = append(unscaled, byte(ch))
  477. }
  478. if dg == -1 {
  479. return nil, fmt.Errorf("no digits read")
  480. }
  481. if dp >= 0 {
  482. z.SetScale(Scale(len(unscaled) - dp))
  483. } else {
  484. z.SetScale(0)
  485. }
  486. _, ok := z.UnscaledBig().SetString(string(unscaled), 10)
  487. if !ok {
  488. return nil, fmt.Errorf("invalid decimal: %s", string(unscaled))
  489. }
  490. return z, nil
  491. }
  492. // SetString sets z to the value of s, interpreted as a decimal (base 10),
  493. // and returns z and a boolean indicating success. The scale of z is the
  494. // number of digits after the decimal point (including any trailing 0s),
  495. // or 0 if there is no decimal point. If SetString fails, the value of z
  496. // is undefined but the returned value is nil.
  497. func (z *Dec) SetString(s string) (*Dec, bool) {
  498. r := strings.NewReader(s)
  499. _, err := z.scan(r)
  500. if err != nil {
  501. return nil, false
  502. }
  503. _, _, err = r.ReadRune()
  504. if err != io.EOF {
  505. return nil, false
  506. }
  507. // err == io.EOF => scan consumed all of s
  508. return z, true
  509. }
  510. // Scan is a support routine for fmt.Scanner; it sets z to the value of
  511. // the scanned number. It accepts the decimal formats 'd' and 'f', and
  512. // handles both equivalently. Bases 2, 8, 16 are not supported.
  513. // The scale of z is the number of digits after the decimal point
  514. // (including any trailing 0s), or 0 if there is no decimal point.
  515. func (z *Dec) Scan(s fmt.ScanState, ch rune) error {
  516. if ch != 'd' && ch != 'f' && ch != 's' && ch != 'v' {
  517. return fmt.Errorf("Dec.Scan: invalid verb '%c'", ch)
  518. }
  519. s.SkipSpace()
  520. _, err := z.scan(s)
  521. return err
  522. }
  523. // Gob encoding version
  524. const decGobVersion byte = 1
  525. func scaleBytes(s Scale) []byte {
  526. buf := make([]byte, scaleSize)
  527. i := scaleSize
  528. for j := 0; j < scaleSize; j++ {
  529. i--
  530. buf[i] = byte(s)
  531. s >>= 8
  532. }
  533. return buf
  534. }
  535. func scale(b []byte) (s Scale) {
  536. for j := 0; j < scaleSize; j++ {
  537. s <<= 8
  538. s |= Scale(b[j])
  539. }
  540. return
  541. }
  542. // GobEncode implements the gob.GobEncoder interface.
  543. func (x *Dec) GobEncode() ([]byte, error) {
  544. buf, err := x.UnscaledBig().GobEncode()
  545. if err != nil {
  546. return nil, err
  547. }
  548. buf = append(append(buf, scaleBytes(x.Scale())...), decGobVersion)
  549. return buf, nil
  550. }
  551. // GobDecode implements the gob.GobDecoder interface.
  552. func (z *Dec) GobDecode(buf []byte) error {
  553. if len(buf) == 0 {
  554. return fmt.Errorf("Dec.GobDecode: no data")
  555. }
  556. b := buf[len(buf)-1]
  557. if b != decGobVersion {
  558. return fmt.Errorf("Dec.GobDecode: encoding version %d not supported", b)
  559. }
  560. l := len(buf) - scaleSize - 1
  561. err := z.UnscaledBig().GobDecode(buf[:l])
  562. if err != nil {
  563. return err
  564. }
  565. z.SetScale(scale(buf[l : l+scaleSize]))
  566. return nil
  567. }
  568. // MarshalText implements the encoding.TextMarshaler interface.
  569. func (x *Dec) MarshalText() ([]byte, error) {
  570. return []byte(x.String()), nil
  571. }
  572. // UnmarshalText implements the encoding.TextUnmarshaler interface.
  573. func (z *Dec) UnmarshalText(data []byte) error {
  574. _, ok := z.SetString(string(data))
  575. if !ok {
  576. return fmt.Errorf("invalid inf.Dec")
  577. }
  578. return nil
  579. }