Monday, April 17, 2017

Things that go wrong with monetary calculations...

Things that go wrong with monetary calculations...  As a programmer, it's very easy to forget about the error-producing “edge cases” in calculations.  These are all things that can occur in any calculations, including monetary calculations.  For a robust financial application, all of these must be handled in some way.  The big three:
  • Division by zero.  This is probably the classic “gotcha” in financial calculations, probably because there are so many ways for it to happen that programmers somehow fail to foresee.  The standard floating point implementations will return a value of infinity for this operation (either positive or negative).  Some libraries will throw an exception.  Either method works.
  • Invalid operation.  The canonical examples are zero divided by zero, or the square root of a negative number.  There are others, too.  I've had this error crop up in several implementations of bond and stock options models I made.  The standard floating point implementations will return a value of Not A Number (abbreviated as NaN) for these operations.  Other libraries will throw an exception.  Again, either method works.
  • Inexact result.  An inexact result occurs when the result of any calculation cannot be represented exactly because the number representation cannot hold either the number of significant digits or the exponent size, or both.  In either binary or decimal, the result of 1/3 is one example of such a result.  Standard floating point implementations silently round such results, providing no indication that the result is inexact.
For monetary calculations, the ideal situation would an implementation that allowed either special values for all three of the above situations, or would throw exceptions.  It would also be very useful if the rounding mode could be specified (as it can be in Java's BigDecimal class).  To the best of my knowledge, there is presently no Java decimal floating point library that handles all three of these cases...

Paradise ponders, walks and drives edition...

Paradise ponders, walks and drives edition...  Yesterday afternoon I took Mako (our giant field spaniel puppy) for a walk up my usual route, 1.5 miles round trip.  It was an absolutely gorgeous day, as you can see in the photos below.  Spring is springing, and green is popping out all over.  I tied Mako to a fence post to take these photos, and as you can see in the first photo he had himself all wrapped up within seconds!


Mako had an adventure on this walk that neither of us expected.  He behaves a lot like Mo'i used to, snuffling along through the grasses in search of something he can eat (a vole or mouse, perhaps).  Yesterday he came across a hole about 8" in diameter, hidden in the grass.  Before I knew what was happening, he had his head in it right down to his shoulders.  A second or so later he came flying backwards out of the hole, followed quickly by a spittin' mad groundhog.  That old ground hog charged Mako fearlessly, and looked damned effective with his teeth and claws.  Mako tumbled backwards and out of range, then promptly pooped. :)  When we continued our walk, he gave that groundhog hole very wide berth!

I saw two other interesting animals on our walk: a white-tailed kite and a very grey red fox.  Those kites are beautiful birds that were quite common where we used to live in California.  This is the first one I've seen here.  It hovered for five minutes or so in a few locations nearly straight overhead, so I had some excellent viewing.  The fox we've seen before; apparently these fields are well within its territory.  Cover is scarce right now, with the alfalfa just barely emerging, so the chances of spotting the fox now are much higher.  Mako never saw either animal. :)

When I got back from the walk, Debbie and I took a drive out toward Hardware Ranch, and then a few miles up Ant Flats Road.  We took the Tesla, and the bumpy Ant Flats Road was a challenge for her because of the pain in her knee's incision.  We probably won't do that again for a few weeks, until she's feeling better.  But ... we did see some animals, especially birds: a golden eagle, Sandhill cranes, blue herons, a pheasant, and lots of deer.  The right-hand fork of Blacksmith Fork River was running at around 8x normal volume, so the waterfalls along the way were really pretty (photos below).


The first photo is of a man-made water feature in the front yard of the cabin on Miller's Ranch.  They get to look at this out their windows.  The other photos are two angles of the same natural falls, just a mile or so from Hardware Ranch along Ant Flats Road.  We've been by this dozens of times, so we're very familiar with its normal flow – a small fraction of what you see here.