cpp_int error for large integer constant with boost multiprecision
I need to save a very large integer in a variable, after i found that i can use boost::multiprecision::cpp_int i tried to use this in my program, but i'm not getting the proper value when i run the program. My program to test this method is:
Code:
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <iomanip>
#include <vector>
void print_factorials()
{
using boost::multiprecision::cpp_int;
cpp_int limit = (cpp_int(1) << 128) - 1;
std::cout<<limit<<std::endl;
cpp_int factorial = 100000000000000000000000000u;
std::cout<<factorial<<std::endl;
}
int main() {
print_factorials();
return 0;
Its output are:
Code:
340282366920938463463374607431768211455
15908979783594147840
with one warning message:
Code:
warning: integer constant is too large for its type [enabled by default]
cpp_int factorial = 100000000000000000000000000u;
My real program where i need to use this is like:
Code:
I1=70; I2=70; I3=70; I4=70; I5=456; I6=456; I7=456; I8=456; I9=456; I10=456; I11=456; I12=456;
cpp_int key = i12+I12*(i11+I11*(i10+I10*(i9+I9*(i8+I8*(i7+I7*(i6+I6*(i5+I5*(i4+I4*(i3+I3*(i2+I2*i1))))))))))
;
Where i1..i12 are positive integers in from 0 to its corresponding I. eg. i1<=>I1. I am doing this for raw-major order calculation where i have got 13 dimension array and i am calculating the offset value of the given address in that array instead of saving given value in 13 dimension array, cas using 13 dimension array, or map is not practical for me since i also need it to write it in file and read it in another program using boot::serialization.
Re: cpp_int error for large integer constant with boost multiprecision
Quote:
I am doing this for row-major order calculation where i have got 13 dimension array and i am calculating the offset value of the given address in that array
Whoa! Calculating the offset value within a 13 dimension matrix assumes that the matrix is stored in contiguous memory - so that the required offset is from the start of the memory. For anything other than very small dimension sizes, this isn't possible as the memory needed will be far greater than that available. That's where the idea of using a map to store the values came in. As I indicated at the time, the associated bigarray class is a simple class. If you require full matrix operations then as was previously suggested, you should consider using an existing sparse-matrix library. Using the map idea as the basis for the sparse matrix has limitations - one being that you can't index into it directly by calculating an offset.
It is possible to simulate having the required amount of memory to hold the full matrix by using a disk-based 'paging' file. But this is non-trivial to implement.
Using a matrix (other than simple get/set of a value as per bigarray class) with 13 dimensions of values detailed in post #1 is going to be non-trivial. IMO I would suggest that you first define the minimum set of matrix operations required, and then look at how these might be provided - by 3rd party library, by extending an existing class (eg bigarray or other) or by writing a new class based upon some other way of storing the data.
Re: cpp_int error for large integer constant with boost multiprecision
Out of interest, I calculated the value of key
Code:
I1=70; I2=70; I3=70; I4=70; I5=456; I6=456; I7=456; I8=456; I9=456; I10=456; I11=456; I12=456;
cpp_int key = i12+I12*(i11+I11*(i10+I10*(i9+I9*(i8+I8*(i7+I7*(i6+I6*(i5+I5*(i4+I4*(i3+I3*(i2+I2*i1))))))))));
Taking in = In for all n, and using short int for storage (2 bytes), this gives a potential maximum value for key of
91073042644961410315842560016
which is about 91,000 yottabytes! So using memory or a disk-based paging file is probably not a practical proposition! :eek: