Article Preview
Buy Now
FEATURE
Overflow Errors
Preventing hard-to-find errors is the best approach
Issue: 13.4 (July/August 2015)
Author: Markus Winter
Author Bio: Markus is a Molecular Biologist who taught himself REALbasic programming in 2003 to let the computer deal with some exceedingly tedious lab tasks. Some call it lazy, he thinks it smart. He still thinks of himself as an advanced beginner at best.
Article Description: No description available.
Article Length (in bytes): 4,732
Starting Page Number: 26
Article Number: 13404
Related Link(s): None
Excerpt of article text...
I tend to run into "errors" that occur because that's how the computer works. I'm especially wary of overflow errors, where a number becomes too large (or too small where negative numbers are concerned) for the variable to hold it and consequently it "flows over"—meaning a positive number becomes negative or a negative number becomes positive. If overflow occurs, then the number that you just calculated will probably mess up some other future calculation, too.
I've been there before (see Tips and Tricks 6, Tips 3 and 4), but now Boeing made headlines with its own overflow error: it turns out that the electricity generators in their Dreamliner aircrafts would shut down after 248 days because an internal counter in their software runs over, causing them to enter a fail-safe mode. It is likely due to a signed 32-bit integer counter that is increased every 10 milliseconds as 10 ms * 2^31 equals 248.55 days. Boeing now requires the generators to be restarted every three months.
The same error happens with software where a millisecond counter causes software to fail after 24 days. It seems sporadic and "not repeatable" which makes these types of errors very hard to find. If you are lucky, then the app won't fail completely, but "some periodic activities stop after about a month requiring a restart" which is a dead giveaway (and I've heard of one guy who makes good money fixing these kinds of errors).
A simple "fix" would be to use a 64-bit counter. Of course, that is not a "real" fix as the error is still there, but as a 1 ms 64-bit signed integer counter only overflows after roughly 292 million years, I think we can live with that. Not even Macs run that long without requiring a restart!
If, for whatever reason, this is not an option, then you need a simple error-detection method. An obvious solution is to cast the integer to a double and check if it exceeds the maximum value (both negative and positive):
...End of Excerpt. Please purchase the magazine to read the full article.