I keep a close eye on a lot of things, including my own Domotica system. Not just things like empty batteries in the sensors, but also the software behind it all. Things like exceptions that occur, memory usage, cpu usage and stuff like that. It keeps me informed about the ‘health’ of it all, so I won’t end up with a system with lots of memory leaks or other nasty bugs scattered throughout the system; and which will be much harder to find once it crashes due to not enough memory…
Yesterday I saw something strange happening to the memory usage:
Strange, cause all I had done was adding a few lines of code to support the hydronic balancing sensors; nothing exciting actually. I checked if all objects were destroyed after use; nothing wrong. I did a restart; same thing happened again. The only thing the code had to do was calculate 2 temperatures out of 4 integer values; what could be wrong here?
Digging a bit deeper I saw these messages in the logs, a few per hour:
Exception:Database Server Error: SQL State: 01000, SQL Error Code: 3621
Not very self-explanatory either, but it was evident that something was wrong here. Again strange, since SQL Server has been running without any problem since I use it; that’s 5 years! I looked up the SQL command that triggered this error response and there it was… a string value of ‘0.689999999999998’ was written to a fixed-length field and it didn’t fit!
But where does this ‘0.689999999999998’ come from? Working with floats of course!
The 2 temperature values are each calculated by dividing a word (2 bytes) by 100; this results in a float with a maximum of 2 decimals (duh). But by storing these 2 temperatures in float variables and subtracting those 2 (to calculate the temperature difference), the result of 28.06 – 27.37 wasn’t 0.69 but the error triggering 0.689999999999998…
Of course, I know this and I should have anticipated on that – I forgot to round the result of the subtraction to 2 decimals!