dec.go 17 KB

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