decimal.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate stringer -type RoundingMode
  5. package number
  6. import (
  7. "math"
  8. "strconv"
  9. )
  10. // RoundingMode determines how a number is rounded to the desired precision.
  11. type RoundingMode byte
  12. const (
  13. ToNearestEven RoundingMode = iota // towards the nearest integer, or towards an even number if equidistant.
  14. ToNearestZero // towards the nearest integer, or towards zero if equidistant.
  15. ToNearestAway // towards the nearest integer, or away from zero if equidistant.
  16. ToPositiveInf // towards infinity
  17. ToNegativeInf // towards negative infinity
  18. ToZero // towards zero
  19. AwayFromZero // away from zero
  20. numModes
  21. )
  22. const maxIntDigits = 20
  23. // A Decimal represents a floating point number in decimal format.
  24. // Digits represents a number [0, 1.0), and the absolute value represented by
  25. // Decimal is Digits * 10^Exp. Leading and trailing zeros may be omitted and Exp
  26. // may point outside a valid position in Digits.
  27. //
  28. // Examples:
  29. // Number Decimal
  30. // 12345 Digits: [1, 2, 3, 4, 5], Exp: 5
  31. // 12.345 Digits: [1, 2, 3, 4, 5], Exp: 2
  32. // 12000 Digits: [1, 2], Exp: 5
  33. // 12000.00 Digits: [1, 2], Exp: 5
  34. // 0.00123 Digits: [1, 2, 3], Exp: -2
  35. // 0 Digits: [], Exp: 0
  36. type Decimal struct {
  37. digits
  38. buf [maxIntDigits]byte
  39. }
  40. type digits struct {
  41. Digits []byte // mantissa digits, big-endian
  42. Exp int32 // exponent
  43. Neg bool
  44. Inf bool // Takes precedence over Digits and Exp.
  45. NaN bool // Takes precedence over Inf.
  46. }
  47. // Digits represents a floating point number represented in digits of the
  48. // base in which a number is to be displayed. It is similar to Decimal, but
  49. // keeps track of trailing fraction zeros and the comma placement for
  50. // engineering notation. Digits must have at least one digit.
  51. //
  52. // Examples:
  53. // Number Decimal
  54. // decimal
  55. // 12345 Digits: [1, 2, 3, 4, 5], Exp: 5 End: 5
  56. // 12.345 Digits: [1, 2, 3, 4, 5], Exp: 2 End: 5
  57. // 12000 Digits: [1, 2], Exp: 5 End: 5
  58. // 12000.00 Digits: [1, 2], Exp: 5 End: 7
  59. // 0.00123 Digits: [1, 2, 3], Exp: -2 End: 3
  60. // 0 Digits: [], Exp: 0 End: 1
  61. // scientific (actual exp is Exp - Comma)
  62. // 0e0 Digits: [0], Exp: 1, End: 1, Comma: 1
  63. // .0e0 Digits: [0], Exp: 0, End: 1, Comma: 0
  64. // 0.0e0 Digits: [0], Exp: 1, End: 2, Comma: 1
  65. // 1.23e4 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 1
  66. // .123e5 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 0
  67. // engineering
  68. // 12.3e3 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 2
  69. type Digits struct {
  70. digits
  71. // End indicates the end position of the number.
  72. End int32 // For decimals Exp <= End. For scientific len(Digits) <= End.
  73. // Comma is used for the comma position for scientific (always 0 or 1) and
  74. // engineering notation (always 0, 1, 2, or 3).
  75. Comma uint8
  76. // IsScientific indicates whether this number is to be rendered as a
  77. // scientific number.
  78. IsScientific bool
  79. }
  80. func (d *Digits) NumFracDigits() int {
  81. if d.Exp >= d.End {
  82. return 0
  83. }
  84. return int(d.End - d.Exp)
  85. }
  86. // normalize returns a new Decimal with leading and trailing zeros removed.
  87. func (d *Decimal) normalize() (n Decimal) {
  88. n = *d
  89. b := n.Digits
  90. // Strip leading zeros. Resulting number of digits is significant digits.
  91. for len(b) > 0 && b[0] == 0 {
  92. b = b[1:]
  93. n.Exp--
  94. }
  95. // Strip trailing zeros
  96. for len(b) > 0 && b[len(b)-1] == 0 {
  97. b = b[:len(b)-1]
  98. }
  99. if len(b) == 0 {
  100. n.Exp = 0
  101. }
  102. n.Digits = b
  103. return n
  104. }
  105. func (d *Decimal) clear() {
  106. b := d.Digits
  107. if b == nil {
  108. b = d.buf[:0]
  109. }
  110. *d = Decimal{}
  111. d.Digits = b[:0]
  112. }
  113. func (x *Decimal) String() string {
  114. if x.NaN {
  115. return "NaN"
  116. }
  117. var buf []byte
  118. if x.Neg {
  119. buf = append(buf, '-')
  120. }
  121. if x.Inf {
  122. buf = append(buf, "Inf"...)
  123. return string(buf)
  124. }
  125. switch {
  126. case len(x.Digits) == 0:
  127. buf = append(buf, '0')
  128. case x.Exp <= 0:
  129. // 0.00ddd
  130. buf = append(buf, "0."...)
  131. buf = appendZeros(buf, -int(x.Exp))
  132. buf = appendDigits(buf, x.Digits)
  133. case /* 0 < */ int(x.Exp) < len(x.Digits):
  134. // dd.ddd
  135. buf = appendDigits(buf, x.Digits[:x.Exp])
  136. buf = append(buf, '.')
  137. buf = appendDigits(buf, x.Digits[x.Exp:])
  138. default: // len(x.Digits) <= x.Exp
  139. // ddd00
  140. buf = appendDigits(buf, x.Digits)
  141. buf = appendZeros(buf, int(x.Exp)-len(x.Digits))
  142. }
  143. return string(buf)
  144. }
  145. func appendDigits(buf []byte, digits []byte) []byte {
  146. for _, c := range digits {
  147. buf = append(buf, c+'0')
  148. }
  149. return buf
  150. }
  151. // appendZeros appends n 0 digits to buf and returns buf.
  152. func appendZeros(buf []byte, n int) []byte {
  153. for ; n > 0; n-- {
  154. buf = append(buf, '0')
  155. }
  156. return buf
  157. }
  158. func (d *digits) round(mode RoundingMode, n int) {
  159. if n >= len(d.Digits) {
  160. return
  161. }
  162. // Make rounding decision: The result mantissa is truncated ("rounded down")
  163. // by default. Decide if we need to increment, or "round up", the (unsigned)
  164. // mantissa.
  165. inc := false
  166. switch mode {
  167. case ToNegativeInf:
  168. inc = d.Neg
  169. case ToPositiveInf:
  170. inc = !d.Neg
  171. case ToZero:
  172. // nothing to do
  173. case AwayFromZero:
  174. inc = true
  175. case ToNearestEven:
  176. inc = d.Digits[n] > 5 || d.Digits[n] == 5 &&
  177. (len(d.Digits) > n+1 || n == 0 || d.Digits[n-1]&1 != 0)
  178. case ToNearestAway:
  179. inc = d.Digits[n] >= 5
  180. case ToNearestZero:
  181. inc = d.Digits[n] > 5 || d.Digits[n] == 5 && len(d.Digits) > n+1
  182. default:
  183. panic("unreachable")
  184. }
  185. if inc {
  186. d.roundUp(n)
  187. } else {
  188. d.roundDown(n)
  189. }
  190. }
  191. // roundFloat rounds a floating point number.
  192. func (r RoundingMode) roundFloat(x float64) float64 {
  193. // Make rounding decision: The result mantissa is truncated ("rounded down")
  194. // by default. Decide if we need to increment, or "round up", the (unsigned)
  195. // mantissa.
  196. abs := x
  197. if x < 0 {
  198. abs = -x
  199. }
  200. i, f := math.Modf(abs)
  201. if f == 0.0 {
  202. return x
  203. }
  204. inc := false
  205. switch r {
  206. case ToNegativeInf:
  207. inc = x < 0
  208. case ToPositiveInf:
  209. inc = x >= 0
  210. case ToZero:
  211. // nothing to do
  212. case AwayFromZero:
  213. inc = true
  214. case ToNearestEven:
  215. // TODO: check overflow
  216. inc = f > 0.5 || f == 0.5 && int64(i)&1 != 0
  217. case ToNearestAway:
  218. inc = f >= 0.5
  219. case ToNearestZero:
  220. inc = f > 0.5
  221. default:
  222. panic("unreachable")
  223. }
  224. if inc {
  225. i += 1
  226. }
  227. if abs != x {
  228. i = -i
  229. }
  230. return i
  231. }
  232. func (x *digits) roundUp(n int) {
  233. if n < 0 || n >= len(x.Digits) {
  234. return // nothing to do
  235. }
  236. // find first digit < 9
  237. for n > 0 && x.Digits[n-1] >= 9 {
  238. n--
  239. }
  240. if n == 0 {
  241. // all digits are 9s => round up to 1 and update exponent
  242. x.Digits[0] = 1 // ok since len(x.Digits) > n
  243. x.Digits = x.Digits[:1]
  244. x.Exp++
  245. return
  246. }
  247. x.Digits[n-1]++
  248. x.Digits = x.Digits[:n]
  249. // x already trimmed
  250. }
  251. func (x *digits) roundDown(n int) {
  252. if n < 0 || n >= len(x.Digits) {
  253. return // nothing to do
  254. }
  255. x.Digits = x.Digits[:n]
  256. trim(x)
  257. }
  258. // trim cuts off any trailing zeros from x's mantissa;
  259. // they are meaningless for the value of x.
  260. func trim(x *digits) {
  261. i := len(x.Digits)
  262. for i > 0 && x.Digits[i-1] == 0 {
  263. i--
  264. }
  265. x.Digits = x.Digits[:i]
  266. if i == 0 {
  267. x.Exp = 0
  268. }
  269. }
  270. // A Converter converts a number into decimals according to the given rounding
  271. // criteria.
  272. type Converter interface {
  273. Convert(d *Decimal, r RoundingContext)
  274. }
  275. const (
  276. signed = true
  277. unsigned = false
  278. )
  279. // Convert converts the given number to the decimal representation using the
  280. // supplied RoundingContext.
  281. func (d *Decimal) Convert(r RoundingContext, number interface{}) {
  282. switch f := number.(type) {
  283. case Converter:
  284. d.clear()
  285. f.Convert(d, r)
  286. case float32:
  287. d.ConvertFloat(r, float64(f), 32)
  288. case float64:
  289. d.ConvertFloat(r, f, 64)
  290. case int:
  291. d.ConvertInt(r, signed, uint64(f))
  292. case int8:
  293. d.ConvertInt(r, signed, uint64(f))
  294. case int16:
  295. d.ConvertInt(r, signed, uint64(f))
  296. case int32:
  297. d.ConvertInt(r, signed, uint64(f))
  298. case int64:
  299. d.ConvertInt(r, signed, uint64(f))
  300. case uint:
  301. d.ConvertInt(r, unsigned, uint64(f))
  302. case uint8:
  303. d.ConvertInt(r, unsigned, uint64(f))
  304. case uint16:
  305. d.ConvertInt(r, unsigned, uint64(f))
  306. case uint32:
  307. d.ConvertInt(r, unsigned, uint64(f))
  308. case uint64:
  309. d.ConvertInt(r, unsigned, f)
  310. default:
  311. d.NaN = true
  312. // TODO:
  313. // case string: if produced by strconv, allows for easy arbitrary pos.
  314. // case reflect.Value:
  315. // case big.Float
  316. // case big.Int
  317. // case big.Rat?
  318. // catch underlyings using reflect or will this already be done by the
  319. // message package?
  320. }
  321. }
  322. // ConvertInt converts an integer to decimals.
  323. func (d *Decimal) ConvertInt(r RoundingContext, signed bool, x uint64) {
  324. if r.Increment > 0 {
  325. // TODO: if uint64 is too large, fall back to float64
  326. if signed {
  327. d.ConvertFloat(r, float64(int64(x)), 64)
  328. } else {
  329. d.ConvertFloat(r, float64(x), 64)
  330. }
  331. return
  332. }
  333. d.clear()
  334. if signed && int64(x) < 0 {
  335. x = uint64(-int64(x))
  336. d.Neg = true
  337. }
  338. d.fillIntDigits(x)
  339. d.Exp = int32(len(d.Digits))
  340. }
  341. // ConvertFloat converts a floating point number to decimals.
  342. func (d *Decimal) ConvertFloat(r RoundingContext, x float64, size int) {
  343. d.clear()
  344. if math.IsNaN(x) {
  345. d.NaN = true
  346. return
  347. }
  348. // Simple case: decimal notation
  349. if r.Increment > 0 {
  350. scale := int(r.IncrementScale)
  351. mult := 1.0
  352. if scale >= len(scales) {
  353. mult = math.Pow(10, float64(scale))
  354. } else {
  355. mult = scales[scale]
  356. }
  357. // We multiply x instead of dividing inc as it gives less rounding
  358. // issues.
  359. x *= mult
  360. x /= float64(r.Increment)
  361. x = r.Mode.roundFloat(x)
  362. x *= float64(r.Increment)
  363. x /= mult
  364. }
  365. abs := x
  366. if x < 0 {
  367. d.Neg = true
  368. abs = -x
  369. }
  370. if math.IsInf(abs, 1) {
  371. d.Inf = true
  372. return
  373. }
  374. // By default we get the exact decimal representation.
  375. verb := byte('g')
  376. prec := -1
  377. // As the strconv API does not return the rounding accuracy, we can only
  378. // round using ToNearestEven.
  379. if r.Mode == ToNearestEven {
  380. if n := r.RoundSignificantDigits(); n >= 0 {
  381. prec = n
  382. } else if n = r.RoundFractionDigits(); n >= 0 {
  383. prec = n
  384. verb = 'f'
  385. }
  386. } else {
  387. // TODO: At this point strconv's rounding is imprecise to the point that
  388. // it is not useable for this purpose.
  389. // See https://github.com/golang/go/issues/21714
  390. // If rounding is requested, we ask for a large number of digits and
  391. // round from there to simulate rounding only once.
  392. // Ideally we would have strconv export an AppendDigits that would take
  393. // a rounding mode and/or return an accuracy. Something like this would
  394. // work:
  395. // AppendDigits(dst []byte, x float64, base, size, prec int) (digits []byte, exp, accuracy int)
  396. hasPrec := r.RoundSignificantDigits() >= 0
  397. hasScale := r.RoundFractionDigits() >= 0
  398. if hasPrec || hasScale {
  399. // prec is the number of mantissa bits plus some extra for safety.
  400. // We need at least the number of mantissa bits as decimals to
  401. // accurately represent the floating point without rounding, as each
  402. // bit requires one more decimal to represent: 0.5, 0.25, 0.125, ...
  403. prec = 60
  404. }
  405. }
  406. b := strconv.AppendFloat(d.Digits[:0], abs, verb, prec, size)
  407. i := 0
  408. k := 0
  409. beforeDot := 1
  410. for i < len(b) {
  411. if c := b[i]; '0' <= c && c <= '9' {
  412. b[k] = c - '0'
  413. k++
  414. d.Exp += int32(beforeDot)
  415. } else if c == '.' {
  416. beforeDot = 0
  417. d.Exp = int32(k)
  418. } else {
  419. break
  420. }
  421. i++
  422. }
  423. d.Digits = b[:k]
  424. if i != len(b) {
  425. i += len("e")
  426. pSign := i
  427. exp := 0
  428. for i++; i < len(b); i++ {
  429. exp *= 10
  430. exp += int(b[i] - '0')
  431. }
  432. if b[pSign] == '-' {
  433. exp = -exp
  434. }
  435. d.Exp = int32(exp) + 1
  436. }
  437. }
  438. func (d *Decimal) fillIntDigits(x uint64) {
  439. if cap(d.Digits) < maxIntDigits {
  440. d.Digits = d.buf[:]
  441. } else {
  442. d.Digits = d.buf[:maxIntDigits]
  443. }
  444. i := 0
  445. for ; x > 0; x /= 10 {
  446. d.Digits[i] = byte(x % 10)
  447. i++
  448. }
  449. d.Digits = d.Digits[:i]
  450. for p := 0; p < i; p++ {
  451. i--
  452. d.Digits[p], d.Digits[i] = d.Digits[i], d.Digits[p]
  453. }
  454. }
  455. var scales [70]float64
  456. func init() {
  457. x := 1.0
  458. for i := range scales {
  459. scales[i] = x
  460. x *= 10
  461. }
  462. }