/*=============================================================*/ DIGITS* add(DIGITS *p, DIGITS *q){ int sum, remainder, len; DIGITS *head, *big, *small; head = NULL; big = moveToEnd(p); small = moveToEnd(q); remainder = 0; len = 1; while (big != NULL){ if (small != NULL){ sum = big->digit + small->digit + remainder; small = small->prev; } else{ sum = big->digit + remainder; } if (sum > 9){ sum = 0; remainder = 1; } else{ remainder = 0; } head = insertDigit(head, sum); big = big->prev; } if (remainder == 1){ head = insertDigit(head, 1); } freeNodes(p); freeNodes(q); return head; }
BigReal &BigReal::multPow10(BRExpoType exp) { DEFINEMETHODNAME; if(!_isnormal()) { return *this; } int m = exp % LOG10_BIGREALBASE; BRExpoType n = exp / LOG10_BIGREALBASE; if(m == 0) { m_expo += n; m_low += n; if(m_expo > BIGREAL_MAXEXPO || m_expo < BIGREAL_MINEXPO) { throwBigRealInvalidArgumentException(method, _T("Invalid m_expo:%s"), format1000(m_expo).cstr()); } } else { if(m < 0) { m = -m; } const BRDigitType s = pow10(m); const BRDigitType t = BIGREALBASE / s; if(exp > 0) { // shift left Digit *p = m_first; Digit *q = p->next; // *this != 0 => p != NULL if(p->n >= t) { insertDigit(p->n / t); m_expo++; } while(q) { p->n = q->n/t + (p->n%t) * s; p = q; q = q->next; } p->n = (p->n%t) * s; } else { // exp < 0. shift right Digit *p = m_last; Digit *q = p->prev; // *this != 0 => p != NULL appendDigit((p->n % s) * t); m_low--; while(q) { p->n = p->n/s + (q->n%s) * t; p = q; q = q->prev; } p->n /= s; } m_expo += n; m_low += n; trimZeroes(); } return *this; }