22 #if defined(BLISS_USE_GMP) 48 #if defined(BLISS_USE_GMP) 67 void assign(
unsigned int n) {mpz_set_ui(v, n); }
72 void multiply(
unsigned int n) {mpz_mul_ui(v, v, n); }
77 size_t print(FILE*
const fp)
const {
return mpz_out_str(fp, 10, v); }
84 void get(mpz_t& result)
const {mpz_set(result, v); }
87 #elif defined(BLISS_BIGNUM_APPROX) 101 void assign(
unsigned int n) {v = (
long double)n; }
106 void multiply(
unsigned int n) {v *= (
long double)n; }
111 size_t print(FILE*
const fp)
const {
return fprintf(fp,
"%Lg", v); }
122 std::vector<unsigned int> factors;
128 factors.push_back(0);
141 factors.push_back(n);
148 factors.push_back(n);
156 size_t print(FILE*
const fp)
const {
157 assert(not factors.empty());
167 r += fprintf(fp,
"%c", d);
184 std::vector<unsigned int> result;
186 for(
unsigned int factor: factors) {
187 std::vector<unsigned int> summand;
188 unsigned int offset = 0;
190 const unsigned int multiplier = factor % 100;
192 std::vector<unsigned int> product;
193 for(
unsigned int i = 0; i < offset; i++)
194 product.push_back(0);
195 unsigned int carry = 0;
196 for(
unsigned int digit: result) {
197 unsigned int v = digit * multiplier + carry;
198 product.push_back(v % 100);
202 product.push_back(carry);
204 add(summand, product);
206 factor = factor / 100;
211 return _string(result);
215 static void add(std::vector<unsigned int>& num,
const std::vector<unsigned int>& summand) {
216 unsigned int carry = 0;
218 while(i < num.size() and i < summand.size()) {
219 const unsigned int v = carry + num[i] + summand[i];
224 while(i < summand.size()) {
225 const unsigned int v = carry + summand[i];
226 num.push_back(v % 100);
230 while(i < num.size()) {
231 const unsigned int v = carry + num[i];
237 num.push_back(carry);
241 static std::string _string(
const std::vector<unsigned int> n) {
242 const char digits[] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
245 for(
auto it = n.crbegin(); it != n.crend(); it++) {
246 unsigned int digit = *it;
247 unsigned int high = digit / 10;
248 if(not first or high > 0)
249 r.push_back(digits[high]);
251 r.push_back(digits[digit % 10]);
A simple wrapper class for non-negative big integers (or approximation of them).
Definition: bignum.hh:117
Definition: abstractgraph.cc:35
void assign(unsigned int n)
Definition: bignum.hh:139
void multiply(unsigned int n)
Definition: bignum.hh:147
~BigNum()
Definition: bignum.hh:134
BigNum()
Definition: bignum.hh:127
size_t print(FILE *const fp) const
Definition: bignum.hh:156
const std::vector< unsigned int > & get_factors() const
Definition: bignum.hh:174
std::string to_string() const
Definition: bignum.hh:182