Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #85 Check if integer addition will overflow

Write boolean function addingWillOverflow which takes two integers x, y and return true if (x+y) overflows.

An overflow may be above the max positive value, or below the min negative value.

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;
#include <limits.h>
#include <stdbool.h>
bool addingWillOverflow(int x, int y) {
  return ((x > 0) && (y > INT_MAX - x)) ||
         ((x < 0) && (y < INT_MIN - x));
}
#include <limits>
bool addingWillOverflow(int x, int y) {
  return ((x > 0) && (y > std::numeric_limits<int>::max() - x)) ||
         ((x < 0) && (y < std::numeric_limits<int>::min() - x));
}
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;
}
import core.checkedint;
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
import "math"
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
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;
}
import static java.lang.Math.addExact;
import static java.lang.Math.subtractExact;
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
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;
sub adding_will_overflow {
    my ($x, $y) = @_;
    return 'Inf' eq $x + $y;
}
def adding_will_overflow(x,y):
    return False
def addingWillOverflow(x,y)
  false
end
fn adding_will_overflow(x: usize, y: usize) -> bool {
    x.checked_add(y).is_none()
}

New implementation...
< >
deleplace