arithmetic.js
function add(a, b, carryIn=0) {
  let result = a + b + carryIn;
  let fullResult = result;
  let carryOut = 0;

  // carryOut is 6th trit, truncate result to 5 trits
  if (result > MAX_TRYTE) {
    carryOut  = 1;
    result -= MAX_TRYTE * 2 + 1;
  } else if (result < MIN_TRYTE) {
    carryOut = -1; // underflow
    result += MAX_TRYTE * 2 + 1;
  }

  // overflow is set if sign is incorrect
  let overflow;
  if (Math.sign(fullResult) === Math.sign(result)) {
    overflow = 0;
  } else {
    overflow = Math.sign(fullResult) || Math.sign(result);
  }
  // note: for 5-trit + 5-trit + 1-trit will always V = C, but the logic above is generic
  if (overflow !== carryOut) throw new Error(`unexpected overflow calculation: ${overflow} !== ${carryOut}`);

  return {result, carryOut, fullResult, overflow};
}
function add(a, b, carryIn=0) {
  let result = a + b + carryIn;
  let fullResult = result;
  let carryOut = 0;

  // carryOut is 6th trit, truncate result to 5 trits
  if (result > MAX_TRYTE) {
    carryOut  = 1;
    result -= MAX_TRYTE * 2 + 1;
  } else if (result < MIN_TRYTE) {
    carryOut = -1; // underflow
    result += MAX_TRYTE * 2 + 1;
  }

  // overflow is set if sign is incorrect
  let overflow;
  if (Math.sign(fullResult) === Math.sign(result)) {
    overflow = 0;
  } else {
    overflow = Math.sign(fullResult) || Math.sign(result);
  }
  // note: for 5-trit + 5-trit + 1-trit will always V = C, but the logic above is generic
  if (overflow !== carryOut) throw new Error(`unexpected overflow calculation: ${overflow} !== ${carryOut}`);

  return {result, carryOut, fullResult, overflow};
}
function add ( a , b , carryIn = 0 ) { let result = a + b + carryIn ; let fullResult = result ; let carryOut = 0 ; // carryOut is 6th trit, truncate result to 5 trits if ( result > MAX_TRYTE ) { carryOut = 1 ; result -= MAX_TRYTE * 2 + 1 ; } else if ( result < MIN_TRYTE ) { carryOut = - 1 ; // underflow result += MAX_TRYTE * 2 + 1 ; } // overflow is set if sign is incorrect let overflow ; if ( Math . sign ( fullResult ) === Math . sign ( result ) ) { overflow = 0 ; } else { overflow = Math . sign ( fullResult ) || Math . sign ( result ) ; } // note: for 5-trit + 5-trit + 1-trit will always V = C, but the logic above is generic if ( overflow !== carryOut ) throw new Error ( ` ${ overflow } ${ carryOut } ` ) ; return { result , carryOut , fullResult , overflow } ; }
Add two trytes with optional carry, returning result and overflow
Add two trytes with optional carry returning result and overflow