money.tex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. \section{Quantity and Money}
  2. \label{sec:quantity}
  3. \code{Quantity} is used to represent amounts of anything.
  4. Three attributes allow \code{Quantity} to specify everything: a numerical value (\code{BigDecimal}), a (measurement) unit or metric (\code{Metric}), and a type specifying the rounding of the numerical type (\code{RoundingStrategy}), as can be seen in Figure \ref{quantity_overview}.
  5. \code{Quantity} objects are immutable and the class implements the \code{Comparable} interface.
  6. \begin{figure}[ht]
  7. \centering
  8. \includegraphics[width=1.0\textwidth]{images/Quantity_Overview.eps}
  9. \label{quantity_overview}
  10. \caption{Quantity - Class Overview}
  11. \end{figure}
  12. \subsection{\code{BigDecimal} - Representing numerical values}
  13. \code{BigDecimal} was chosen as datatype for the \code{amount} attribute (see Figure~\ref{quantity_overview}) over \code{float} or \code{double} because of its arbitraty precision.
  14. Moreover, objects of \code{BigDecimal} are immutable and the \code{BigDecimal} class provides operations for including, but not limited to: arithmetic, rounding, and comparison.
  15. \subsection{\code{Metric} - What is represented}
  16. The composite type \code{Metric} contains all information pertaining to the unit or metric of the represented object.
  17. Examples for units or metrics are: m (meter), s (second), pcs (pieces).
  18. For example consider the unit of length "meter": represented by an object of the class \code{Metric} the symbol would be set to "\code{m}" and the name to "\code{meter}".
  19. Furthermore, an object of type \code{Metric} has a description field, to explain the meaning of the metric in detail.
  20. For the example of a meter a possible description could be "\code{The meter is the length of the path travelled by light in vacuum during a time interval of 1/299 792 458 of a second.}".
  21. Convenience instances exist for euros, pieces and units, namely \code{EURO}, \code{PIECES}, and \code{UNIT} (see Figure \ref{quantity_overview}).
  22. %\subsection{\code{RoundingStrategy} - How to handle half a person}
  23. %When handling quantities of unkown metric, standard rounding rules cannot always be employed.
  24. %The case of natural persons is just one example, when rounding rules have to be restricted to yield a useful result.
  25. %You can round in four general directions: away from zero (\code{RoundUpStrategy}), towards zero (\code{RoundDownStrategy}), towards positive infinity (\code{RoundCeilStrategy}), and towards negative infinity (\code{RoundFloorStrategy}).
  26. %Additionally, you can specify the digits after the decimal delimiter (\code{numberOfDigits} in Figure~\ref{quantity_overview}).
  27. %Monetary values in \euro{} or \$US are often just represented with two digits after the decimal delimiter.
  28. %Other values, such as kilo grams may be required to be specified to four digits after the decimal delimiter or even more.
  29. %In case of (natural) persons, the digits after the decimal delimiter is usually zero, except you are working in statistics (1.45 children per couple) or you are a serial killer dismembering your victims.\footnote{Of course you can only kill an integral number of people. However, only a part of a victims body may be found. Anyway, in case you have not noticed: that was a joke. Haha. Fat chance.}
  30. %The third parameter for rounding is the rounding digit, i.e. the number specifying when you round up or down.
  31. %Usually, this number is five, but it can be specified using the attribute \code{roundingDigit} and the \code{RoundStrategy} as strategy.
  32. %In case of persons, it is one: if you have $n.0\,persons$, you round down, otherwise up.
  33. %If you are calculating a capacity for persons, you will have to round down.
  34. %This can be achieved by specifying the correct rounding direction.
  35. %\\
  36. \subsection{Rounding}
  37. Rounding is an arithmetic operation where a numberical value is replaced by another, simpler representation.
  38. Rounding is often results in a number which is easier to handle than the original value, for example $3.1415$ instead of $\pi$, or $1.1414$ instead of $\sqrt{2}$.
  39. Also, rounding may be employed to indicate the accuracy of a computed or measured value.
  40. For example a value is calculated to be $3.4563$ but it is known the result is only accurate to one hundredth because of measurement errors, so the result may be rounded to $3.46$.
  41. Rounding may also be used to limit the number of significant digits, for example in statistics.
  42. The german population is rounded to full thousands by the Federal Statistical Office and the statistical Offices of the L\"ander\footnote{\url{http://www.statistik-portal.de/Statistik-Portal/en/en_zs01_bund.asp}}.
  43. \\
  44. Different types of rounding exist. A number can be rounded to a specific precision, to an (integer) multiple of a step or increment, or to the nearest available value ot of a set of preferred values.
  45. Thus, rounding is implemented as strategy pattern, allowing a consistent interface to be used with different algorithms.
  46. So far, the only strategy implemented is the \code{BasicRoundingStrategy} which supports rounding to a specific precision or rounding to a multiple of a pre-determined multiple of a step.
  47. \\
  48. Rounding to precision zero is also known as rounding to integer.
  49. %When doing so, five general strategies, as outlined in Table \ref{roundstrat} can be employed.
  50. When doing so, five general strategies can be employed:
  51. \begin{itemize}
  52. \item{\textbf{up} - result is the next integer, away from zero}
  53. \item{\textbf{down} (also known as ``truncation'') - result is the next integer, towards zero}
  54. \item{\textbf{ceil} - result is the next integer, towards positive infinity}
  55. \item{\textbf{floor} - result is the next integer, towards negative infinity}
  56. \item{\textbf{half} - result is the nearest integer}
  57. \end{itemize}
  58. %\begin{table}
  59. %\begin{tabular}{c}
  60. %oo \\
  61. %foo \\
  62. %\end{tabular}
  63. %\caption{\label{roundstrat}foo-table}
  64. %\end{table}
  65. All strategies except the last yield unambiguous results.
  66. If a number ends in $x.5$ and is rounded to the nearest integer two results exist: $x$ and $x+1$, since both have the same absolute difference to $x.5$.
  67. To resolve the ambiguity, tie breaking rules have to be employed.
  68. The first four strategies to round integers (up, down, ceil, floor) can also be used for tie-breaking.
  69. Additionally, more sophisticated methods may be employed, for example:
  70. \begin{itemize}
  71. \item{\textbf{even} - result is the nearest even integer}
  72. \item{\textbf{odd} - result is the nearest odd integer}
  73. \item{\textbf{stochastic} - result randomly chosen to be $x$ or $x+1$}
  74. \item{\textbf{alternate} - using strategies up and down intermittently}
  75. \end{itemize}
  76. A technique called dithering, which is related to stochastic rounding tie-breaking rule, is used when instead of the accuracy of single rounding operation is less important than the distribution of rounding results across a set of values.
  77. Dithering is often used in quantizing image or audio data.
  78. For example, if a continous stream of values of approximately $0.88$ should be rounded to zero digits after the radix delimiter, that is to integer, would result in contiuous $1$ or $0$ result, depending on the strategy.
  79. Using dithering, a number is rounded up (or down) with the probability of the fraction.
  80. In our example, this would result in a stream containing $88\%$ ones and $12\%$ zeroes, randomly distributed.
  81. A similar technique, which reduces the average error, is called ``error diffusion''.
  82. As mentioned earlier, only one strategy is implemented as of yet, namely \code{BasicRoundingStrategy}.
  83. Instantiating an object of this strategy, it is possible to specify either a rounding step or a precision in digits after the radix delimiter.
  84. Internally, the following algorithm is used to round numbers:
  85. \begin{equation*}
  86. x = round(q/m) * m
  87. \end{equation*}
  88. where $q$ is the number to be rounded, $m$ is the increment or rounding step, $x$ is the result, and $round$ is a rounding strategy to round the quotient $q/m$ to an integer value.
  89. When the number of digits after the radix delimiter are specified, the appropriate rounding step is calculated as follows:
  90. \begin{equation*}
  91. step = \frac{1}{10^{digits}} = 10^{-digits}
  92. \end{equation*}
  93. For example, rounding a number to four digits after the radix delimiter is equal to rounding the number to the next (integer) multiple of $0.0001$.
  94. Rounding to a nearest step or increment, can be used if something is sold in fixed quantities.
  95. For example, if an item is sold in packs of $50$, and someone punches in $40$, you will have to round up to $50$.
  96. So your rounding step is $50$.
  97. Another example is material, which is sold by the meter or yard.
  98. You have to round the amount specified by your customer accordingly.
  99. Of course, a rounding step can be smaller than $1$, i.e. $0.25$.
  100. \\
  101. Two convenience rounding strategies exist so far: \code{RoundingStrategy.MONETARY} rounding with four digits after the decimal delimiter and rounding towards zero, and \code{RoundingStrategy.ROUND\_ONE} with zero digits after the decimal delimiter and also rounding towards zero.
  102. \\
  103. Rounding strategies which are not yet implemented, but may be interesting to \salespoint{} users and developers are scaled rounding, rounding to nearest value of a predetermined set and a sum preserving rounding strategy.
  104. Implementing these strategies may require extending the \code{RoundingStrategy} interface.
  105. Scaled rounding is used to round values on a logarithmic scale.
  106. Near zero a high precision is required, for example using three digits after the radix delimiter.
  107. But farther away precision requirements drop, so only one digit after the radix delimiter may suffice.
  108. Thus, the precision to which is rounded depends on the magnitude of the value to be rounded.
  109. Rounding to nearest preferred value is used, when a calculated value is rounded to the nearest standard value.
  110. Examples of preferred values are the E series, the Renard series, powers of two, metric paper sizes and pen sizes, tuning systems in music, or film speed, aperture sizes and shutter speeds in photography.
  111. E series are most commonly used in electronics industry for resistor, capacitor, and inductor values.\footnote{IEC 60063}
  112. In computer science, powers of two are often used as preferred values for sizes.
  113. Paper is sized so that neighboring dimensions have a ratio of $\sqrt2$.\footnote{ISO 216}
  114. This also applies for pen sizes, so that a pen size is available for a scaled drawing.
  115. Sum preserving rounding is necessary if, for example for every item on an invoice the tax is explicitly listed.
  116. The tax for each item has to be rounded, introducing a rounding error.
  117. When the taxes for each item are summed up, the sum has to match the tax calculated for the summed prices of all items.
  118. This is seldomly the case, so the rounding of the individual tax has to be fitted.
  119. The method of least squares may be used to achieve fitting.
  120. Another algorithm which may be used is minimizing the round-off error.
  121. \subsection{\code{Money} - A usecase for \code{Quantity}}
  122. An object of the class \code{Money} is used to represent an amount of currency.
  123. The following paragraphs detail the intended use, internal modelling and implementation of \code{Money}.
  124. The UML model is given in Figure \ref{money_overview}.
  125. \begin{figure}[ht]
  126. \centering
  127. \includegraphics[width=0.75\textwidth]{images/Money_Overview.eps}
  128. \label{money_overview}
  129. \caption{Money - Class Overview}
  130. \end{figure}
  131. A \code{Money} object can be instantiated by passing the numerical value as constructor parameter.
  132. In this case, the metric \code{Metric.EURO} is used, as well as \code{RoundingStrategy.MONETARY} for the rounding strategy attribute.
  133. For other currencies, a \code{Metric} parameter can be passed to the constructor along with a numerical paramter.
  134. However, conversion between currencies is not supported, as it was not deemed necessary.
  135. The rounding strategy cannot be overridden.
  136. Internally, \code{Money} objects use four digits after the decimal delimiter for arithmetic operations to minimize the rounding error.
  137. The \code{toString()} method, however, limits the output to the expected two digits after the decimal delimiter and appends the symbol of the associated \code{Metric}.
  138. Two convenience instances exist: \code{Money.ZERO}, representing \euro{0,00}, and \code{Money.OVER9000}, representing an amount greater than \euro{9000,00}.
  139. \subsection[]{\code{Unit} - Representing persons or other integral\protect\footnote{whole-number} items}
  140. To represent integral items conveniently, the objects of class \code{Unit} can be used.
  141. The rounding strategy is fixed for all instances to \code{RoundingStrategy.ROUND\_ONE} (Figure~\ref{quantity_overview}) and \code{Metric.PIECES} (Figure~\ref{money_overview}) is used as metric.
  142. Convenience instances for amounts of zero, one and ten unit(s) exist (\code{Unit.ZERO}, \code{Unit.ONE}, and \code{Unit.TEN}; see Figure~\ref{quantity_overview}).