...
Code Block |
---|
|
public int do_operation(int a, int b) throws ArithmeticException {
long temp = (long)a + (long)b;
if(temp >Integer.MAX_VALUE || temp < Integer.MIN_VALUE)
throw new ArithmeticException();
elsereturn (int)temp; // Value within range can perform the addition
return (int)temp;
}
|
Compliant Solution (Bounds Checking)
...
Code Block |
---|
|
public int do_operation(int a, int b) throws ArithmeticException {
int temp;
if( (b>0 && (a > Integer.MAX_VALUE - b)) || (b<0 && (a < Integer.MIN_VALUE - b)) )
throw new ArithmeticException();
else
temp = return a + b; //Value within range can perform the addition
return temp;
}
|
Compliant Solution (Use BigInteger Class)
...
Code Block |
---|
|
public boolean overflow(long a, long b) {
BigInteger ba = new java.math.BigInteger(String.valueOf(a));
BigInteger bb = new java.math.BigInteger(String.valueOf(b));
BigInteger br = ba.add(bb);
return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
br.compareTo(BigInteger.valueOf(Long.MIN_VALUE))== -1);
}
public long do_operation(long a, long b) throws ArithmeticException {
if(overflow(a,b))
throw new ArithmeticException();
else
// Within range; safely perform the addition
return a + b;
}
|
With use of the BigInteger
class, integer overflows are definitely eliminated. However, due to increased performance costs, it should be used only when other methods are not appropriate.
...
Code Block |
---|
|
public int do_operation(int a,int b) {
long temp = (long)a - (long)b;
if(temp < Integer.MIN_VALUE || temp > Integer.MAX_VALUE)
throw new ArithmeticException();
else
result =return (int) temp;
return temp;
}
|
Compliant Solution (Bounds Checking)
...
Code Block |
---|
|
public int do_operation(int a, int b) throws ArithmeticException {
int temp;
if( (b>0 && (a < Integer.MIN_VALUE + b)) ||) || (b<0 && (a > Integer.MAX_VALUE + b)) )
throw new ArithmeticException();
else
temp = return a - b; //Value within range can perform the addition
return temp;
}
|
Compliant Code Example (Use BigInteger Class)
...
Code Block |
---|
|
public boolean underflow(long a, long b) {
BigInteger ba = new BigInteger(String.valueOf(a));
BigInteger bb = new BigInteger(String.valueOf(b));
BigInteger br = ba.subtract(bb);
return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
br.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1);
}
public long do_operation(long a, long b) throws ArithmeticException {
if(underflow(a,b))
throw new ArithmeticException();
else
// Within range; safely perform the subtraction
return a - b;
}
|
Multiplication
...
Code Block |
---|
|
int a,b,result;
long temp = (long) a * (long)b;
if(temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE)
throw new ArithmeticException(); // Overflow
else
result = (int) temp; // Value within range, safe to downcast
|
...
Code Block |
---|
|
if(a == Integer.MIN_VALUE && b == -1)
throw new ArithmeticException(); // May be Integer.MIN_VALUE and -1
else
result = a/b; // Safe operation
|
...
Code Block |
---|
|
if(result == Integer.MIN_VALUE)
throw new ArithmeticException();
else
temp = -result;
|
Absolute Value
...