Safe Haskell | Safe-Inferred |
---|
Penny quantities. A quantity is simply a count (possibly fractional) of something. It does not have a commodity or a Debit/Credit.
- data Qty
- data NumberStr
- toQty :: NumberStr -> Maybe Qty
- mantissa :: Qty -> Integer
- places :: Qty -> Integer
- newQty :: Mantissa -> Places -> Maybe Qty
- type Mantissa = Integer
- type Places = Integer
- add :: Qty -> Qty -> Qty
- mult :: Qty -> Qty -> Qty
- data Difference
- = LeftBiggerBy Qty
- | RightBiggerBy Qty
- | Equal
- equivalent :: Qty -> Qty -> Bool
- difference :: Qty -> Qty -> Difference
- allocate :: Qty -> NonEmpty Qty -> NonEmpty Qty
- type TotSeats = Integer
- type PartyVotes = Integer
- type SeatsWon = Integer
- largestRemainderMethod :: TotSeats -> [PartyVotes] -> [SeatsWon]
Documentation
A quantity is always greater than zero. Various odd questions happen if quantities can be zero. For instance, what if you have a debit whose quantity is zero? Does it require a balancing credit that is also zero? And how can you have a debit of zero anyway?
I can imagine situations where a quantity of zero might be useful; for instance maybe you want to specifically indicate that a particular posting in a transaction did not happen (for instance, that a paycheck deduction did not take place). I think the better way to handle that though would be through an addition to DebitCredit - maybe DebitCredit/Zero. Barring the addition of that, though, the best way to indicate a situation such as this would be through transaction memos.
The Eq instance is derived. Therefore q1 == q2 only if q1 and q2
have both the same mantissa and the same exponent. You may instead
want equivalent
.
Whole String | A whole number only. No radix point. |
WholeRad String | A whole number and a radix point, but nothing after the radix point. |
WholeRadFrac String String | A whole number and something after the radix point. |
RadFrac String | A radix point and a fractional value after it, but nothing before the radix point. |
toQty :: NumberStr -> Maybe QtySource
Converts strings to Qty. Fails if any of the strings have non-digits, or if any are negative, or if the result is not greater than zero, or if the strings are empty.
equivalent :: Qty -> Qty -> BoolSource
Compares Qty after equalizing their exponents.
difference :: Qty -> Qty -> DifferenceSource
Subtract the second Qty from the first, after equalizing their exponents.
:: Qty | The result will add up to this Qty. |
-> NonEmpty Qty | Allocate using this list of Qty. |
-> NonEmpty Qty | The length of this list will be equal to the length of the list of allocations. Each item will correspond to the original allocation. |
Allocate a Qty proportionally so that the sum of the results adds up to a given Qty. Fails if the allocation cannot be made (e.g. if it is impossible to allocate without overflowing Decimal.) The result will always add up to the given sum.
type PartyVotes = IntegerSource
:: TotSeats | Total number of seats in the legislature. This is the integer that will be allocated. This number must be positive or this function will fail at runtime. |
-> [PartyVotes] | The total seats will be allocated proportionally depending on how many votes each party received. The sum of this list must be positive, and each member of the list must be at least zero; otherwise a runtime error will occur. |
-> [SeatsWon] | The sum of this list will always be equal to the total number of seats, and its length will always be equal to length of the PartyVotes list. |
Allocates integers using the largest remainder method. This is the method used to allocate parliamentary seats in many countries, so the types are named accordingly.