Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
function Adding_Will_Overflow (X, Y : Integer) return Boolean is
begin
return X > 0 and Y > 0 and X > Integer'Last - Y;
end Adding_Will_Overflow;
PROBLEM: this doesn't detect negative overflow.
bool addingWillOverflow(int x, int y) {
return ((x > 0) && (y > INT_MAX - x)) ||
((x < 0) && (y < INT_MIN - x));
}
INT_MAX and INT_MIN are defined in limits.h header, bool type is defined in stdbool.h
bool addingWillOverflow(int x, int y)
{
bool willOverflow = false;
if (x > 0 && y > 0)
if (y > (int.MaxValue - x)) willOverflow = true;
if (x < 0 && y < 0)
if (y < (int.MinValue - x)) willOverflow = true;
return willOverflow;
}
bool addingWillOverflow(int x, int y)
{
bool result;
core.checkedint.adds(x, y, result);
return result;
}
logical function adding_will_overflow (x,y) result(res)
integer, intent(in) :: x, y
res = (x > 0 .and. y > huge(x) - x) .or. (x < 0 .and. y < huge(x) - x)
end function adding_will_overflow
func addingWillOverflow(x int, y int) bool {
if x > 0 {
return y > math.MaxInt-x
}
return y < math.MinInt-x
}
addingWillOverflow x y = y > maxBound - x
Integers in Haskell are arbitrary sized. But Int:s etc aren't.
This would function would work for any Numeric types with a maxbound (such as Int, Word8 etc).
PROBLEM: this doesn't detect negative overflow.
This would function would work for any Numeric types with a maxbound (such as Int, Word8 etc).
PROBLEM: this doesn't detect negative overflow.
function addingWillOverflow(x, y) {
return x > 0 && y > 0 && x > Number.MAX_SAFE_INTEGER - y
}
public boolean addingWillOverflow(int x, int y){
boolean willOverFlow = false;
if(x > 0 && y > 0){
if(y > (Integer.MAX_VALUE - x)){
willOverFlow = true;
}
}
if(x < 0 && y < 0){
if(y < (Integer.MIN_VALUE - x)){
willOverFlow = true;
}
}
return willOverFlow;
}
If both values are positive, make sure the difference between one and the MAX_VALUE is less than the other, otherwise they will overflow.
If both values are negative, make sure the difference between one and the MIN_VALUE is less than the other, otherwise they will underflow.
The case where one is negative and one is positive does not need to be checked as they will definitely not under or overflow in that scenario.
If both values are negative, make sure the difference between one and the MIN_VALUE is less than the other, otherwise they will underflow.
The case where one is negative and one is positive does not need to be checked as they will definitely not under or overflow in that scenario.
boolean willOverflow(int x, int y) {
try {
addExact(x, y);
subtractExact(x, y);
return false;
} catch (ArithmeticException e) {
return true;
}
}
local function intadding_will_overflow(x,y)
return x>0
and y>0 and x+y<x
or y<0 and x+y>x
end
Integer operation won't overflow in lua 5.1 since integer are actually float.
math.maxinteger introduced in lua 5.2.
math.maxinteger introduced in lua 5.2.
function _addingWillOverflow($x, $y): bool
{
return (PHP_INT_MAX - $y) > $x;
}
function AddingWillOverFlow(x,y: Integer): Boolean;
begin
Result := ((x > 0) and (y > 0) and (x > (High(Integer)-y))) or
((x < 0) and (y < 0) and ((Low(Integer)-x) > y));
end;
function AddingWillOverFlow(x,y: Integer): Boolean;
begin
Result := (x > 0) and (y > 0) and (x > (MaxInt-y));
end;
MaxInt is a predefined constant holding the largest possible signed integer on the current platform.
PROBLEM: this doesn't detect negative overflow.
PROBLEM: this doesn't detect negative overflow.
sub adding_will_overflow {
my ($x, $y) = @_;
return 'Inf' eq $x + $y;
}
def adding_will_overflow(x,y):
return False
Python has arbitrary precision integers so you shouldn't worry about overflow.
Also, don't bother coding this constant function!
Also, don't bother coding this constant function!
def addingWillOverflow(x,y)
false
end