The OpenD Programming Language

adds

Add two signed integers, checking for overflow.

The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.

  1. int adds(int x, int y, bool overflow)
  2. long adds(long x, long y, bool overflow)
  3. cent adds(cent x, cent y, bool overflow)
    nothrow @safe @nogc pure pragma(inline, true)
    static if(is(cent))
    cent
    adds
    ()
    (
    cent x
    ,
    cent y
    ,
    ref bool overflow
    )

Parameters

x cent

left operand

y cent

right operand

overflow bool

set if an overflow occurs, is not affected otherwise

Return Value

Type: cent

the sum

Examples

bool overflow;
assert(adds(2, 3, overflow) == 5);
assert(!overflow);

assert(adds(1, int.max - 1, overflow) == int.max);
assert(!overflow);

assert(adds(int.min + 1, -1, overflow) == int.min);
assert(!overflow);

assert(adds(int.max, 1, overflow) == int.min);
assert(overflow);

overflow = false;
assert(adds(int.min, -1, overflow) == int.max);
assert(overflow);

assert(adds(0, 0, overflow) == 0);
assert(overflow);                   // sticky
bool overflow;
assert(adds(2L, 3L, overflow) == 5);
assert(!overflow);

assert(adds(1L, long.max - 1, overflow) == long.max);
assert(!overflow);

assert(adds(long.min + 1, -1, overflow) == long.min);
assert(!overflow);

assert(adds(long.max, 1, overflow) == long.min);
assert(overflow);

overflow = false;
assert(adds(long.min, -1, overflow) == long.max);
assert(overflow);

assert(adds(0L, 0L, overflow) == 0);
assert(overflow);                   // sticky

Meta