The OpenD Programming Language

subs

Subtract 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 subs(int x, int y, bool overflow)
    nothrow @safe @nogc pure pragma(inline, true)
    int
    subs
    ()
    (
    int x
    ,
    int y
    ,
    ref bool overflow
    )
  2. long subs(long x, long y, bool overflow)
  3. cent subs(cent x, cent y, bool overflow)

Parameters

x int

left operand

y int

right operand

overflow bool

set if an overflow occurs, is not affected otherwise

Return Value

Type: int

the difference

Examples

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

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

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

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

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

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

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

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

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

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

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

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

Meta