X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/blobdiff_plain/3939466e952e19024a4856cfa7c6ca2b07bc297a..618a87c0a485f2973ac44b867f74735facbaa941:/src/number.h diff --git a/src/number.h b/src/number.h index a079e8f..71705cc 100644 --- a/src/number.h +++ b/src/number.h @@ -50,21 +50,21 @@ struct number // Constructores (después de construído, el chunk siempre tiene al // menos un elemento). // Constructor default (1 'átomo con valor 0) - number(): chunk(1, 0) {} + number(): chunk(1, 0), sign(positive) {} // Constructor a partir de buffer (de 'átomos') y tamaño // Copia cada elemento del buffer como un 'átomo' del chunk // (el átomo menos significativo es el chunk[0] == buf[0]) - number(native_type* buf, size_type len, sign_type sign = positive): - chunk(buf, buf + len), sign(sign) + number(native_type* buf, size_type len, sign_type s = positive): + chunk(buf, buf + len), sign(s) { fix_empty(); } // Constructor a partir de un 'átomo' (lo asigna como único elemento // del chunk). Copia una vez N en el vector. - number(native_type n, sign_type sign = positive): - chunk(1, n), sign(sign) {} + number(native_type n, sign_type s = positive): + chunk(1, n), sign(s) {} number(const std::string& str); @@ -80,7 +80,7 @@ struct number number& operator<<= (const size_type n); number& operator-= (const number& n); bool operator< (const number& n); - bool operator==(const number& n) const; + bool operator==(const number& n) const; // Devuelve referencia a 'átomo' i del chunk (no debería ser necesario // si la multiplicación es un método de este objeto). @@ -202,6 +202,23 @@ number< N, E >::number(const std::string& origen) template < typename N, typename E > number< N, E >& number< N, E >::operator+= (const number< N, E >& n) { + // Si tienen distinto signo, restamos... + if (sign != n.sign) + { + if (sign == positive) // n es negativo + { + number< N, E > tmp = n; + tmp.sign = positive; + *this -= tmp; + } + else // n es positivo, yo negativo + { + sign = positive; + *this = n - *this; + } + return *this; + } + native_type c = 0; size_type ini = 0; size_type fin = std::min(chunk.size(), n.chunk.size()); @@ -297,7 +314,6 @@ number< N, E >& number< N, E >::operator-= (const number< N, E >& n) minuend.sign = positive; } - native_type c = 0; size_type ini = 0; size_type fin = std::min(minuend.chunk.size(), subtrahend.chunk.size()); size_type i; //problema de VC++, da error de redefinición @@ -540,8 +556,7 @@ number < N, E > naif(const number< N, E > &u, const number< N, E > &v) sign_type sign; - if ( (u.sign == positive && v.sign == positive) || - (u.sign == negative && v.sign == negative) ) { + if (u.sign == v.sign) { sign = positive; } else { sign = negative; @@ -621,8 +636,7 @@ number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) sign_type sign; - if ( (u.sign == positive && v.sign == positive) || - (u.sign == negative && v.sign == negative) ) { + if (u.sign == v.sign) { sign = positive; } else { sign = negative; @@ -631,7 +645,7 @@ number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) if (chunk_size == 1) { E tmp; tmp = static_cast< E >(u.chunk[0]) * static_cast< E >(v.chunk[0]); - num_type tnum = num_type(static_cast< N* >(&tmp), 2, sign); + num_type tnum = num_type(reinterpret_cast< N* >(&tmp), 2, sign); return tnum; }