Heteroclinic.net logo


Preemptive Overflow Prevention I
Introduction: the Problem

We observe a the problem, if we try to execute the following Java code.

For most engineering students even of computer science or physics, the first year college study will involve designing an addition circuit using logical gates on a breadboard. One point we should recall is that the results will not be a single bit while we try to sum up two single bits as the different operands. The result would need a bit represent the sum, another bit called carry. In later year, I remember probably (I read the book, not formally take about 8086 CPU) in an assembly language book, the sum results of two registers will be saved in a register (maybe one of the above), if there is an overflow, a register or a bit of some register will indicate there is a carry and store the carry. However, this was a post-mortem approach. You can only regret it, but no-way to reverse it. It is uncertain if most up-to-date software checks the carry (read the above-mentioned register) and set a proper exception handle etc.

This is rather a motivation than a problem. I took a couple of days figure out a way to avoid the problem at the same time practice some healthy software development methodology.

Preemptive Overflow Prevention II
A Fundamental Mathematical Analysis

To simplify the problem, we only deal with unsigned integers.

I.The mathematical condition for sum overflow:

II.The mathematical condition for multiplication overflow:

Preemptive Overflow Prevention III
A Java Implementation

Basically, we use Java and Maven build a project that enclose Javadoc, unit tests, code coverage report. These practices are good practices to write program. They are the jump-start to engage a real software engineering product but still not the sufficient. For time limit being, a solution for integer operation is left to the coming future. At this phase, we just detect and avoid the problem. The code coverage report is also an experiment. It should exclude the sample/tutorial Java files.

System requirement:
JDK1.7 or higher, Maven 3.0 or higher installed Preferably, a Java IDE that can import Maven project.

Download and unpack the source, for Windows users, you can consider 7zip for the tar.gz file. In a Linux/UNIX shell, or Cygwin etc:
Md5sum: d42cd96160d2e0b9244f75d67db12bff PreemptiveOverflowPrevention.tar.gz

Build the jar binary and generate the coverage report ( target/site/jacoco-ut/index.html):

Use the following command to generate Javadoc ( target/site/apidocs/index.html):

Preemptive Overflow Prevention IV
License or Agreement of Usage Terms

It is better to have than without.

Safe Integer Addition Analysis
For the addition of two integers a and b, we know how to safely predict an overflow instance. For two integers addition, the carry will be exactly 1, the remainder is a- (M - b + 1), for a >= b > 0; if b is 0, there won't be overflow.

Safe Integer Addition Coding Experiment
Experimenting safe addition:

Safe Integer Multiplication Analysis
Given M >= a >= b > 0, for b =0, no overflow will happen.

Safe Integer Multiplication Coding Experiment
Experimenting safe multiplication:

We found engineering is hard. It is not a soft target.
We also found how fatal the least significant digit(s) plays in deciding the prime or composite attribute of a very large number.