|
-
December 31st, 2016, 04:07 PM
#1
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.
-
December 31st, 2016, 06:37 PM
#2
Re: cpp_int error for large integer constant with boost multiprecision
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 3rd, 2017, 09:42 AM
#3
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|