Saturday, April 15, 2017

A slightly subtle issue with binary representations of money...

A slightly subtle issue with binary representations of money...  This is an issue that I ran into a few times on a stock trading application that initially used double precision floating point to represent monetary values.

Consider the fractional value (in base ten) 11/16.  In exact radix form, that's 0.6875.  Rounded to one decimal digit, that would be 0.7; to two digits 0.69, and to three digits either 0.687 or 0.688 (depending on the rounding rules you choose).  That's the way we usually think of numbers; hopefully you find none of that surprising.

But now look at what happens in binary representations of the same number.  The fractional form is 1011/10000 and the radix form is 0.1011.  If you round that form to three bits (roughly equivalent to one decimal digit), you get 0.110 (or possibly 0.101 with different rounding rules).  When you convert that value to decimal for presentation to one of those pesky humans, you get ... 0.75 (or possibly 0.625).

Oops.

There's nothing actually wrong with the way binary is rounding, it's just (very) unexpected to the average person looking at a rounded monetary value.  The “expected” value of 0.7 for single digit rounding is (in binary) 0.10110011001100...  There's simply no way to round in binary and get a result like that!

This rounding problem crops up most often when doing division operations.  In those stock trading applications, we kept getting it in two places: stock quotes in fractional values (still common in some exchanges, though thankfully not in the U.S.), and when calculating average price for a series of related trades.  The latter problem caused us much grief when the system on the other end represented money using decimal numbers – our average price calculation would give a different result than theirs, we'd get a mismatch, and a human would have to intervene to figure it all out.  That human was not a programmer, so from their point of view our binary rounding was simply wrong.  We never really fixed this problem until we switched to decimal representation...

Paradise ponders, less controlled chaos edition...

Paradise ponders, less controlled chaos edition...  Well, yesterday our furnace guys were here and worked all day long on fixing our furnace issues.  As always, there were about a bazillion little decisions that needed to be made (by me!) along the way.  On the other hand, they got nearly everything done.  The furnace now sits almost two feet higher off the ground than it used to, and underneath it is a ginormous filter.  The goal there was to reduce the resistance of air entering the furnace.  On that same note, they added two new return grills and enlarged two others, approximately tripling the square inches of return grills in our house.  The difference is very obvious down in our basement “mechanical room” (where the furnace is located): the return air duct is rattling and flexing in response to the much higher volume of air now traveling through it.  The guys also installed a large new vent (where the hot air comes out) in the cattery, and the warm air floods out of there in a way that delights the inhabitants.

That's the good furnace news.  Here's the not-so-good: all of this work was done to cure a problem our furnace had.  It cycled on-and-off every few minutes, as the plenum above the burners overheated.  The diagnosis for this problem was insufficient air flow through the furnace, which made good sense to me.  Also, we eliminated all the other causes any of us could think of.  I'm sure you've guessed by now that the furnace is still cycling.  Sigh.  They'll attack it again on Monday.  I don't really feel badly about this, though, as all of these changes are going to improve the heating in the house anyway.  But sigh nonetheless...

The heater guys were here until after 6 pm, so after they left I started my evening chores and was contemplating going to bed.  But just then the lawn sprinkler guys showed up, with a fifth-wheel trailer full of 6" irrigation pipe.  And they went right to work to install it.  I had been expecting them all day, but apparently there was something wrong with their trailer that took nearly all day to fix.  They're supposed to be here again today to finish installing the new risers.  That will make my friend and neighbor Tim D. a happy guy...