From 8e7f8300f82f9d93f94813cd717bf2943e5ad07a Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 5 Oct 2016 21:18:19 +0200 Subject: Now division ACTUALLY works --- bigint.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- bigint.h | 7 ++++++ main.cpp | 27 ++++++++++++++++++++-- primes.cpp | 4 ++-- 4 files changed, 105 insertions(+), 10 deletions(-) diff --git a/bigint.cpp b/bigint.cpp index 9fdafe0..0827e1e 100644 --- a/bigint.cpp +++ b/bigint.cpp @@ -21,10 +21,35 @@ Bigint::Bigint(slongdigit_t v) "longdigit_t should be twice as large as digit_t"); v=abs(v); if(v>digits[0])digits.push_back(v>>digit_bits); + else if(v==0){ + digits.clear(); + sign=1; + } + checkconsistent(); +} + +Bigint::Bigint(longdigit_t v) + :digits(1,(digit_t)v),sign(1){ + if(v>digits[0])digits.push_back(v>>digit_bits); else if(v==0)digits.clear(); checkconsistent(); } +Bigint::Bigint(sdigit_t v) + :digits(1,abs(v)),sign(v>=0?1:-1){ + if(v==0){ + digits.clear(); + sign=1; + } + checkconsistent(); +} + +Bigint::Bigint(digit_t v) + :digits(1,v),sign(1){ + if(v==0)digits.clear(); + checkconsistent(); +} + void Bigint::add(Bigint &a,const Bigint &b){ if(a.digits.size()=0?1:-1; v*=sign; digits[0]=v; if(v>digits[0])digits.push_back(v>>digit_bits); - shrink(); - normalise(); + checkconsistent(); + return *this; +} + +Bigint& Bigint::operator=(longdigit_t v){ + digits.clear(); + if(v!=0){ + digits.push_back((digit_t)v); + if(v>digits[0])digits.push_back(v>>digit_bits); + } + checkconsistent(); + return *this; +} + +Bigint& Bigint::operator=(sdigit_t v){ + digits.clear(); + if(v==0)sign=1; + else { + sign=v>=0?1:-1; + v*=sign; + digits.push_back(v); + } + checkconsistent(); + return *this; +} + +Bigint& Bigint::operator=(digit_t v){ + digits.clear(); + sign=1; + if(v!=0)digits.push_back(v); checkconsistent(); return *this; } @@ -335,20 +393,27 @@ pair Bigint::divmod(const Bigint &div,int depth) const { divhead>>=digit_bits-__builtin_clz(div.digits.back()); //cerr<<"divhead="<