public long GetCurrentValue(IChannel ch, long count, long valMax) { // Note: This method should not modify any fields. AssertValid(valMax); Contracts.Assert(count >= _cna); // If the sum is zero, return zero. if ((_sumHi | _sumLo) == 0) { // If all values in a given column are NAs issue a warning. if (count == _cna) { ch.Warning("All values in this column are NAs, using default value for imputation"); } return(0); } Contracts.Assert(count > _cna); count -= _cna; Contracts.Assert(count > 0); ulong sumHi = _sumHi; ulong sumLo = _sumLo; bool neg = (long)sumHi < 0; if (neg) { // Negative value. Work with the absolute value. sumHi = ~sumHi; sumLo = ~sumLo; IntUtils.Add(ref sumHi, ref sumLo, 1); } // If this assert triggers, the caller did something wrong. Update should have been // called at most count times and each value should have been within the range of // a ulong, so the absolute value of the sum can't possibly be so large that sumHi // reaches or exceeds count. This assert implies that the Div part of the DivRound // call won't throw. Contracts.Assert(sumHi < (ulong)count); ulong res = IntUtils.DivRound(sumLo, sumHi, (ulong)count); Contracts.Assert(0 <= res && res <= (ulong)valMax); return(neg ? -(long)res : (long)res); }