Tuesday, April 18, 2017

Inexact results and monetary calculations...

Inexact results and monetary calculations...  There are a few things worth pondering about making calculations with money where the results are not exact quantities.

Consider the case of inexact results that are expected to be inexact.  A common example of this is a compound interest calculation that involves logarithms, powers, and roots – not much chance of an exact answer on those!  In cases like this, what we need for money is for the answer to be rounded to the nearest conventional unit of money (for instance, the nearest penny in USD).  This is, however, a bit more complicated than you might think.

Consider a case in USD, where we want to round to the nearest penny.  Suppose our unrounded result was 0.121 – that's easy, the rounded result is 0.12, rounded down (toward zero).  Similarly, 0.346 would rounded up to 0.35.  Both of those are obvious and uncontroversial.  But suppose our unrounded result was 0.115?  Do we round that down to 0.11, or up to 0.12?  In both cases, the difference between the rounded and unrounded values is the same: 0.05.  How do we choose between rounding up or rounding down?

Most of us old enough to predate the “new math” were taught in elementary school to round such values up, all the time.  I have no idea what the hell kids are taught these days, except I'm confident it's not very useful.  However, always rounding halfway values up over a large number of results introduces a bias toward larger average results.  Bankers and their customers noticed this a long, long time ago.  Some clever accountants came up with a simple solution that's easy to implement and removes the bias: “half even” rounding.  In this kind of rounding, a result like 0.115 is rounded so that the next most significant digit is rounded to an even value – so round up to 0.12 in this example, because 2 is even and 3 is not.  If the unrounded result was 0.165, though, then we'd round down to 0.16, because 6 is even.  I should note that one could also use a “half odd” rounding to accomplish the same thing.

In some kinds of financial calculations, though, even this solution isn't what's needed.  For instance, in certain kinds of models you always want to round toward zero.  There are a half-dozen or so different kinds of rounding that are occasionally useful in monetary calculations.  The way monetary quantities are represented really needs to support all of these rounding flavors.

Now lets consider a different case: where results are expected to be exact, and an inexact result indicates a mistake of some kind.  I ran into a case like this in a stock trading application, where we multiplied the number of shares bought or sold times the sales price to get a “lot price”.  Since shares of stock are indivisible (e.g., you can't buy 1.5 shares of IBM), that result should always be exact, to the penny.  If it's inexact, then something is wrong – perhaps someone mistakenly entered a fractional share quantity, or there's a bug in the program.  For these sorts of situations, it is very useful to know whether a monetary quantity is exact or inexact (e.g., has been rounded).  The way monetary quantities are represented should support this.

Finally, sometimes in monetary calculations we really don't want inexact values to be rounded.  For example, suppose we had 10,000 USD that we want to divide in thirds and distribute to three accounts.  The rounded, inexact result of that would be 3,333.33 USD that we put into each account – but that only adds up to 9,999.99 USD – we “lost” a penny and now our books are out of whack.  The way financial applications generally solve problems of this type is to use some algorithm to choose a lucky account, and they give that account the extra penny (in this case).  These algorithms themselves are interesting, as they need to be repeatable (so you can't just roll the dice) for audits, but they are not the problem I'm discussing here.  It's knowing that we have an inexact result, and how much is “left over” to distribute that I care about today.

This sort of problem always has a division operation at the root of it.  The general solution is really simple: you need a division operation that gives you the floor of the quotient, and the remainder.  So the result of the division in the example above would be 3,333.33 with a remainder of 0.01.  The way monetary quantities are represented must include the division-with-remainder operation.  Ideally it would allow returning quotients that are floor, ceiling, nearest toward zero, or nearest away from zero (all with appropriately adjusted remainders) because all of these are useful in some financial applications.

Paradise ponders, filling station, fuzzy eyeballs, and giant wrench edition...

Paradise ponders, filling station, fuzzy eyeballs, and giant wrench edition...  Yesterday was another really busy day around the place.  I got dragged away from my own projects several times to help the guys working on our “filling station”.   There were decisions to make, some education on how the components worked, tools to lend, and hardware to scrounge for.

In the first photo below, Joe is using a fancy core drill (with a water-cooled diamond bit!) to drill holes through the back wall of the filling station.  These holes allow the two pieces of 1" black iron pipe through the wall.  I'll be grouting them later to make them water-tight.  One of the pipes is for gasoline, the other for diesel.  The second photo shows the inside of the filling station, with most of the plumbing done.  The fuel comes in through the pipes on the bottom, then on each leg (for each fuel) goes through a shutoff valve, a filter/water separator, and a gauge.  The pipes on top will be extended toward the opening, where a swiveled hose with a nozzle will be attached.  The other end of the pipes is currently just open underneath the tanks; there's a bit of plumbing left to get done there, too.  But look at all that lovely progress!  I think that by next week I should be calling the local petroleum distributor to order me up some fuel.  Woo hoo!


Yesterday Debbie and I took a jaunt up to our optometrist to pick up her reworked new glasses.  The first time we got them she was getting headaches when wearing them.  When we took them back, we discovered that the optometrist had made a prescription mistake.  He wrote it as 53° axis (for astigmatism), but it was supposed to be 153°!  They immediately ordered new lenses for her, and yesterday we got them.  Her first reaction: she couldn't see well at distance.  Now she's got to wear them for a week to see if her eyeballs will relax so she can see well with them.  If not, it's back to the optometrist we go – for round three!

Mark T. was here yesterday to finish up putting the risers.  There was a bit of a holdup, though, as he was trying to remove our old aluminum riser heads from the PVC pipe that holds them.  The pipe is thick-walled 3" ID PVC, male threaded, and the riser screws onto the pipe.  Well, these old riser heads had been in place for 20 years or so, and they weren't going to come off easily.  Mark only had one pipe wrench big enough to grip them, and that one just barely.  So I grabbed my giant water pump wrench, which has jaw that open to 5" (commercial photo at right).  I don't often need something that big, but when I do it's really hard to beat this thing.  It's got gripping power every bit as good as a conventional pipe wrench, but it's much easier to use.  Mark and I got those things apart in no time. :)