rounder.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package dec
  2. // This file implements signed multi-precision decimals.
  3. import (
  4. "math/big"
  5. )
  6. // Rounder represents a method for rounding the (possibly infinite decimal)
  7. // result of a division to a finite Dec. It is used by Dec.Round() and
  8. // Dec.Quo().
  9. //
  10. type Rounder interface {
  11. // When UseRemainder() returns true, the Round() method is passed the
  12. // remainder of the division, expressed as the numerator and denominator of
  13. // a rational.
  14. UseRemainder() bool
  15. // Round sets the rounded value of a quotient to z, and returns z.
  16. // quo is rounded down (truncated towards zero) to the scale obtained from
  17. // the Scaler in Quo().
  18. //
  19. // When the remainder is not used, remNum and remDen are nil.
  20. // When used, the remainder is normalized between -1 and 1; that is:
  21. //
  22. // -|remDen| < remNum < |remDen|
  23. //
  24. // remDen has the same sign as y, and remNum is zero or has the same sign
  25. // as x.
  26. Round(z, quo *Dec, remNum, remDen *big.Int) *Dec
  27. }
  28. type rounder struct {
  29. useRem bool
  30. round func(z, quo *Dec, remNum, remDen *big.Int) *Dec
  31. }
  32. func (r rounder) UseRemainder() bool {
  33. return r.useRem
  34. }
  35. func (r rounder) Round(z, quo *Dec, remNum, remDen *big.Int) *Dec {
  36. return r.round(z, quo, remNum, remDen)
  37. }
  38. // RoundExact returns quo if rem is zero, or nil otherwise. It is intended to
  39. // be used with ScaleQuoExact when it is guaranteed that the result can be
  40. // obtained without rounding. QuoExact is a shorthand for such a quotient
  41. // operation.
  42. //
  43. var RoundExact Rounder = roundExact
  44. // RoundDown rounds towards 0; that is, returns the Dec with the greatest
  45. // absolute value not exceeding that of the result represented by quo and rem.
  46. //
  47. // The following table shows examples of the results for
  48. // Quo(x, y, Scale(scale), RoundDown).
  49. //
  50. // x y scale result
  51. // ------------------------------
  52. // -1.8 10 1 -0.1
  53. // -1.5 10 1 -0.1
  54. // -1.2 10 1 -0.1
  55. // -1.0 10 1 -0.1
  56. // -0.8 10 1 -0.0
  57. // -0.5 10 1 -0.0
  58. // -0.2 10 1 -0.0
  59. // 0.0 10 1 0.0
  60. // 0.2 10 1 0.0
  61. // 0.5 10 1 0.0
  62. // 0.8 10 1 0.0
  63. // 1.0 10 1 0.1
  64. // 1.2 10 1 0.1
  65. // 1.5 10 1 0.1
  66. // 1.8 10 1 0.1
  67. //
  68. var RoundDown Rounder = roundDown
  69. // RoundUp rounds away from 0; that is, returns the Dec with the smallest
  70. // absolute value not smaller than that of the result represented by quo and
  71. // rem.
  72. //
  73. // The following table shows examples of the results for
  74. // Quo(x, y, Scale(scale), RoundUp).
  75. //
  76. // x y scale result
  77. // ------------------------------
  78. // -1.8 10 1 -0.2
  79. // -1.5 10 1 -0.2
  80. // -1.2 10 1 -0.2
  81. // -1.0 10 1 -0.1
  82. // -0.8 10 1 -0.1
  83. // -0.5 10 1 -0.1
  84. // -0.2 10 1 -0.1
  85. // 0.0 10 1 0.0
  86. // 0.2 10 1 0.1
  87. // 0.5 10 1 0.1
  88. // 0.8 10 1 0.1
  89. // 1.0 10 1 0.1
  90. // 1.2 10 1 0.2
  91. // 1.5 10 1 0.2
  92. // 1.8 10 1 0.2
  93. //
  94. var RoundUp Rounder = roundUp
  95. // RoundHalfDown rounds to the nearest Dec, and when the remainder is 1/2, it
  96. // rounds to the Dec with the lower absolute value.
  97. //
  98. // The following table shows examples of the results for
  99. // Quo(x, y, Scale(scale), RoundHalfDown).
  100. //
  101. // x y scale result
  102. // ------------------------------
  103. // -1.8 10 1 -0.2
  104. // -1.5 10 1 -0.1
  105. // -1.2 10 1 -0.1
  106. // -1.0 10 1 -0.1
  107. // -0.8 10 1 -0.1
  108. // -0.5 10 1 -0.0
  109. // -0.2 10 1 -0.0
  110. // 0.0 10 1 0.0
  111. // 0.2 10 1 0.0
  112. // 0.5 10 1 0.0
  113. // 0.8 10 1 0.1
  114. // 1.0 10 1 0.1
  115. // 1.2 10 1 0.1
  116. // 1.5 10 1 0.1
  117. // 1.8 10 1 0.2
  118. //
  119. var RoundHalfDown Rounder = roundHalfDown
  120. // RoundHalfUp rounds to the nearest Dec, and when the remainder is 1/2, it
  121. // rounds to the Dec with the greater absolute value.
  122. //
  123. // The following table shows examples of the results for
  124. // Quo(x, y, Scale(scale), RoundHalfUp).
  125. //
  126. // x y scale result
  127. // ------------------------------
  128. // -1.8 10 1 -0.2
  129. // -1.5 10 1 -0.2
  130. // -1.2 10 1 -0.1
  131. // -1.0 10 1 -0.1
  132. // -0.8 10 1 -0.1
  133. // -0.5 10 1 -0.1
  134. // -0.2 10 1 -0.0
  135. // 0.0 10 1 0.0
  136. // 0.2 10 1 0.0
  137. // 0.5 10 1 0.1
  138. // 0.8 10 1 0.1
  139. // 1.0 10 1 0.1
  140. // 1.2 10 1 0.1
  141. // 1.5 10 1 0.2
  142. // 1.8 10 1 0.2
  143. //
  144. var RoundHalfUp Rounder = roundHalfUp
  145. // RoundFloor rounds towards negative infinity; that is, returns the greatest
  146. // Dec not exceeding the result represented by quo and rem.
  147. //
  148. // The following table shows examples of the results for
  149. // Quo(x, y, Scale(scale), RoundFloor).
  150. //
  151. // x y scale result
  152. // ------------------------------
  153. // -1.8 10 1 -0.2
  154. // -1.5 10 1 -0.2
  155. // -1.2 10 1 -0.2
  156. // -1.0 10 1 -0.1
  157. // -0.8 10 1 -0.1
  158. // -0.5 10 1 -0.1
  159. // -0.2 10 1 -0.1
  160. // 0.0 10 1 0.0
  161. // 0.2 10 1 0.0
  162. // 0.5 10 1 0.0
  163. // 0.8 10 1 0.0
  164. // 1.0 10 1 0.1
  165. // 1.2 10 1 0.1
  166. // 1.5 10 1 0.1
  167. // 1.8 10 1 0.1
  168. //
  169. var RoundFloor Rounder = roundFloor
  170. // RoundCeil rounds towards positive infinity; that is, returns the
  171. // smallest Dec not smaller than the result represented by quo and rem.
  172. //
  173. // The following table shows examples of the results for
  174. // Quo(x, y, Scale(scale), RoundCeil).
  175. //
  176. // x y scale result
  177. // ------------------------------
  178. // -1.8 10 1 -0.1
  179. // -1.5 10 1 -0.1
  180. // -1.2 10 1 -0.1
  181. // -1.0 10 1 -0.1
  182. // -0.8 10 1 -0.0
  183. // -0.5 10 1 -0.0
  184. // -0.2 10 1 -0.0
  185. // 0.0 10 1 0.0
  186. // 0.2 10 1 0.1
  187. // 0.5 10 1 0.1
  188. // 0.8 10 1 0.1
  189. // 1.0 10 1 0.1
  190. // 1.2 10 1 0.2
  191. // 1.5 10 1 0.2
  192. // 1.8 10 1 0.2
  193. //
  194. var RoundCeil Rounder = roundCeil
  195. var intSign = []*big.Int{big.NewInt(-1), big.NewInt(0), big.NewInt(1)}
  196. var roundExact = rounder{true,
  197. func(z, q *Dec, rA, rB *big.Int) *Dec {
  198. if rA.Sign() != 0 {
  199. return nil
  200. }
  201. return z.move(q)
  202. }}
  203. var roundDown = rounder{false,
  204. func(z, q *Dec, rA, rB *big.Int) *Dec {
  205. return z.move(q)
  206. }}
  207. var roundUp = rounder{true,
  208. func(z, q *Dec, rA, rB *big.Int) *Dec {
  209. z.move(q)
  210. if rA.Sign() != 0 {
  211. z.Unscaled().Add(z.Unscaled(), intSign[rA.Sign()*rB.Sign()+1])
  212. }
  213. return z
  214. }}
  215. var roundHalfDown = rounder{true,
  216. func(z, q *Dec, rA, rB *big.Int) *Dec {
  217. z.move(q)
  218. brA, brB := rA.BitLen(), rB.BitLen()
  219. if brA < brB-1 {
  220. // brA < brB-1 => |rA| < |rB/2|
  221. return z
  222. }
  223. adjust := false
  224. srA, srB := rA.Sign(), rB.Sign()
  225. s := srA * srB
  226. if brA == brB-1 {
  227. rA2 := new(big.Int).Lsh(rA, 1)
  228. if s < 0 {
  229. rA2.Neg(rA2)
  230. }
  231. if rA2.Cmp(rB)*srB > 0 {
  232. adjust = true
  233. }
  234. } else {
  235. // brA > brB-1 => |rA| > |rB/2|
  236. adjust = true
  237. }
  238. if adjust {
  239. z.Unscaled().Add(z.Unscaled(), intSign[s+1])
  240. }
  241. return z
  242. }}
  243. var roundHalfUp = rounder{true,
  244. func(z, q *Dec, rA, rB *big.Int) *Dec {
  245. z.move(q)
  246. brA, brB := rA.BitLen(), rB.BitLen()
  247. if brA < brB-1 {
  248. // brA < brB-1 => |rA| < |rB/2|
  249. return z
  250. }
  251. adjust := false
  252. srA, srB := rA.Sign(), rB.Sign()
  253. s := srA * srB
  254. if brA == brB-1 {
  255. rA2 := new(big.Int).Lsh(rA, 1)
  256. if s < 0 {
  257. rA2.Neg(rA2)
  258. }
  259. if rA2.Cmp(rB)*srB >= 0 {
  260. adjust = true
  261. }
  262. } else {
  263. // brA > brB-1 => |rA| > |rB/2|
  264. adjust = true
  265. }
  266. if adjust {
  267. z.Unscaled().Add(z.Unscaled(), intSign[s+1])
  268. }
  269. return z
  270. }}
  271. var roundFloor = rounder{true,
  272. func(z, q *Dec, rA, rB *big.Int) *Dec {
  273. z.move(q)
  274. if rA.Sign()*rB.Sign() < 0 {
  275. z.Unscaled().Add(z.Unscaled(), intSign[0])
  276. }
  277. return z
  278. }}
  279. var roundCeil = rounder{true,
  280. func(z, q *Dec, rA, rB *big.Int) *Dec {
  281. z.move(q)
  282. if rA.Sign()*rB.Sign() > 0 {
  283. z.Unscaled().Add(z.Unscaled(), intSign[2])
  284. }
  285. return z
  286. }}