0

I have this little code with a big integer:

int main()
{
mpz_clears;
mpf_clears;


  mpz_class a;
        a=130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191;


return 0;

and got this error, so that I cannot do anything with the integer...

error: integer literal is too large to be represented in any
  integer type

I am on mac, compiled with the tool of xcode and use the last 6.0.0a gmp library.

What did I miss?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
magox
  • 1
  • 7

5 Answers5

1

Using GMP doesn't magically make C/C++ understand literals larger than the base language spec. Luckily, with C++11 with user defined literals, GMP has a literal syntax defined so the literal type is mpz_class and not int. Just add _mpz to the end of the integer literal, e.g.

a=130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191_mpz;
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • how can I use C+11? I just added _mpz to the integer and still have the same error.. – magox Aug 23 '16 at 05:14
  • it says "cpp:32:71: error: invalid suffix '_mpz' on integer constant a=1306377883863080690468614492602605712916784585156713644368053759_mpz;" – magox Aug 23 '16 at 05:19
  • 1
    @ThomasLover See the documentation where it says, "With C++11 compilers..." - you have one of those, yes? Make sure it is compiling in C++11 or later compliance mode. Check your compiler. For many (gcc, clang) it is simply `-std=c++11` for a compiler switch. – WhozCraig Aug 23 '16 at 06:48
  • compilation seems good when I add "-std=c++11" to the compilation code line but now I get this error: libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: mpz_set_str Abort trap: 6 – magox Aug 23 '16 at 07:15
  • @ThomasLover: That makes it seem like you're using a literal with invalid characters in it. I don't see anything wrong here, but you could easily have made a mistake in your actual code that would need correcting. – ShadowRanger Aug 23 '16 at 07:23
0

C++ types can't handle the big number which you are passing and its throwing that error.

see this answer for the details. What range of values can integer types store in C++

and in the document its clearly mentioned that.

Construct an mpz_class. All the standard C++ types may be used, except long long and long double, and all the GMP C++ classes can be used, although conversions from mpq_class and mpf_class are explicit. Any necessary conversion follows the corresponding C function, for example double follows mpz_set_d (see Assigning Integers).

Community
  • 1
  • 1
smali
  • 4,687
  • 7
  • 38
  • 60
  • how can I use C+11? I just added _mpz to the integer and still have the same error.. – magox Aug 23 '16 at 05:14
  • it says "cpp:32:71: error: invalid suffix '_mpz' on integer constant a=1306377883863080690468614492602605712916784585156713644368053759_mpz;" – magox Aug 23 '16 at 05:19
  • @ThomasLover, it depends on the compiler, but on Mac the compiler will be either gcc or clang and those both use the same `-std=c++11` option. There should also be an option somewhere in XCode gui if you are using that, but I don't, so I don't know where. – Jan Hudec Aug 23 '16 at 07:06
  • compilation seems good when I add "-std=c++11" to the compilation code line but now I get this error: libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: mpz_set_str Abort trap: 6 – magox Aug 23 '16 at 07:14
0

Just because you use a big-integer class does not mean that the compiler will suddenly become capable of working with ultra-large integer literals. Your suffix-less literal is treated as an int by the compiler, as usual. And it is too large for int.

You should either use C++11 literal support and supply your literal value with _mpz suffix, or represent your values as strings.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • how can I use C+11? I just added _mpz to the integer and still have the same error.. – magox Aug 23 '16 at 05:14
  • it says "cpp:32:71: error: invalid suffix '_mpz' on integer constant a=1306377883863080690468614492602605712916784585156713644368053759_mpz;" – magox Aug 23 '16 at 05:19
  • compilation seems good when I add "-std=c++11" to the compilation code line but now I get this error: libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: mpz_set_str Abort trap: 6 – magox Aug 23 '16 at 07:15
0

Well, even with the _mpz option, there might be a limit to the number of digits you can assign. I reduced to this size the number of digits and I could store the integer:

130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191443885715160918938894478991212554338474935909274442208280226020332302710637502228813106477844481700372333640604211874260838332822176968781235304962300880267221110401606508880971834777831402249082184410637749400023282419270071233303228854128584088916313729295257781669730936517951304701393525757057288415991731506781288275420005462290126288405806701552761743270631625705587885293887371663631869096785158480771725887503591755610

magox
  • 1
  • 7
  • The limit should be nowhere near this low though. Does it fail if you do `mpz_set_str(a.get(), "int digits go here")`? Because that's basically what the literal syntax should do; GMP internals support values with bit lengths in the billions of bits range; 300 decimal digits is nothing. – ShadowRanger Aug 23 '16 at 07:43
  • @shadowranger, I get this error when I use more digits than above with _mpz at the end, do u know why? libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: mpz_set_str Abort trap: 6 – magox Aug 24 '16 at 10:33
  • now I got this error lol, this thing is a code mess.... just by saying mpz_t a; i have "member reference base type 'mpz_t' (aka '__mpz_struct [1]') is not a structure or union" – magox Aug 24 '16 at 10:36
  • You should be using `mpz_class` with these literals, not `mpz_t`. – ShadowRanger Aug 24 '16 at 11:11
  • yes done, but then I have this error "no member named 'get' in '__gmp_expr' 1 error generated." lol help me !! – magox Aug 24 '16 at 11:18
  • and that again" error: integer literal is too large to be represented in any integer type" – magox Aug 24 '16 at 11:20
  • You probably violated the rule that you can't declare a GMP type using C++11 `auto` when the initializer is the result of a math operation, e.g. `auto mpz3 = mpz1 + mpz2;` is wrong because GMP operations return expression types, not `mpz_class` (the expression type has an implicit conversion to `mpz_class`, but if you use `auto`, that's never performed). – ShadowRanger Aug 24 '16 at 13:17
  • I also misremembered the function name; it [should be `.get_mpz_t()`](https://gmplib.org/manual/C_002b_002b-Interface-General.html#C_002b_002b-Interface-General), not `.get()`, but the error you're seeing is due to [the GMP expression related C++ limitations](https://gmplib.org/manual/C_002b_002b-Interface-Limitations.html#C_002b_002b-Interface-Limitations). – ShadowRanger Aug 24 '16 at 13:20
  • ok I am not getting everything, actually I only have one or two lines of code just trying to declare a huge integer ... and i don't succeed.. no addition or either operation to be done... only have this int main() { mpz_clears; mpz_class a; mpz_set_str(a.get_mpz_class(),130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093 and the rest of the integer... – magox Aug 25 '16 at 03:04
  • Read the code you just sent me. Did you call `get_mpz_t`? No. Did you pass it a string? No. – ShadowRanger Aug 25 '16 at 04:14
  • well I tried with .get_mpz_t and a string i got this :error: no member named 'get' in '__gmp_expr' mpz_set_str(a.get.mpz_t,"130927593275932759723"); – magox Aug 25 '16 at 08:43
  • so u said I canno use auto? because it is the only way i succeeded. The thing is I cannot use it for another operation. I just tell you want I want to do. I want to do "9493475947359475943759475943759437594375943759437594794573975493" power "3" power "an integer" entered by the user. How would you do that? – magox Aug 25 '16 at 08:45
-1

To operate with big numbers you better look at crypto-software, like openssl/bn.h

$ sudo apt-get install libssl-dev

MUL.cpp

#include <openssl/bn.h>
#include <ctime>
#include <string>
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;


// man bn
int main(int argc, char *argv[]) {
        BN_CTX *ctx = BN_CTX_new( );

        BIGNUM *a = NULL;
        BIGNUM *b = NULL;

        BIGNUM *mul = BN_new( );

        BN_dec2bn(&a, argv[1]);
        BN_dec2bn(&b, argv[2]);

        BN_mul( mul, a, b, ctx);

        string mul_str = BN_bn2dec( mul );
        cout << mul_str << endl;

        BN_free( a );
        BN_free( b );
        BN_free( mul );

        BN_CTX_free( ctx );
        return 0;
}

Thereafter,

$ g++ -v -o MUL MUL.cpp -lcrypto -lrt
$ ./MUL 1111111111111111111111 11111111111111111111111111111111111111
12345679012345679012344444444444444444320987654320987654321

Or your numbers:

$ ./MUL 130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191443885715160918938894478991212554338474935909274442208280226020332302710637502228813106477844481700372333640604211874260838332822176968781235304962300880267221110401606508880971834777831402249082184410637749400023282419270071233303228854128584088916313729295257781669730936517951304701393525757057288415991731506781288275420005462290126288405806701552761743270631625705587885293887371663631869096785158480771725887503591755610130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191443885715160918938894478991212554338474935909274442208280226020332302710637502228813106477844481700372333640604211874260838332822176968781235304962300880267221110401606508880971834777831402249082184410637749400023282419270071233303228854128584088916313729295257781669730936517951304701393525757057288415991731506781288275420005462290126288405806701552761743270631625705587885293887371663631869096785158480771725887503591755610 130637788386308069046861449260260571291678458515671364436805375996643405376682659882150140370119739570729696093810308688223886144781635348688713392214619435345787110033188140509357535583193264801721383236152235906221860161085667905721519797609516199295279707992563172152784123713076584911245631751842633105652153513186684155079079372385923352208421842040532051768902602579344300869529063620569896872621227499787666438515766191438772844982077590564825560915004123788524793626088046688154064374425340131073611440941376503643793012676721171310302652283866154666880487476095144107907540698417260347310774677574064007810935083421437442654204085311165490420993090855747058348793757769523336364858305492927387281493416741250273266926840468154062676311322374882380011804120628601384191443885715160918938894478991212554338474935909274442208280226020332302710637502228813106477844481700372333640604211874260838332822176968781235304962300880267221110401606508880971834777831402249082184410637749400023282419270071233303228854128584088916313729295257781669730936517951304701393525757057288415991731506781288275420005462290126288405806701552761743270631625705587885293887371663631869096785158480771725887503591755610
17066231754465807402900420358384027625063828147296398970430735667107038565643532131029262436685775911914313380489264979150809498798501933158462145225528180551769789575574797447397445413420626589337764710733729094394489883580640065014326441503312557193537990957436917477463646907709648408216434453480535184067598892956346170907569131377903528411111853111366291979626086698383298307345834392212231046634063413666384508324419179368057389725477538706904589815650381926976015993762890040156790405503832003268981872633663593094738685190702324170124859881504272985658187233430078072074850856278428071856485129710354193089376603399949840797557269752404324626163227377490362755801388382649764643331010423816641928437135475590362260226952505888042595117331291587671568162632704073970431619934337538342580157600657762301792637641467188915728074681445126309949503670453577866959321057221058159530832970764856624653513410124322745428166790635701352502052256269558858525352939994015481804509533547301113993717358659764948833130929069642122698202129401970681574382516490304799923426506769877124709868229514010141819989355368364493152148025681360589017051459966742937386231341355101280349206933137558553397562834428892415453233148641176771563712134361104022807550303488698075859517457925640610491054979460976360319484563783286684527208609132950696416209270231736324410954950684352328008061594161733388271145413633779170942608933174781023093090166766097051317310022316050630811133583173184115645711370920550992076973922926411319105318826733544096711620052335678566140497081147253812896893420327059981797167126306338464682511753397155370448528529202208913712194212784573851259886750498508574755936271159501806286495680771291521438702898980393172655721876464226059755372992679372981146919582948873581755274102131988783345665739059331911729901433053990191883389538118335419875393994271684373681810615128035637305671352255611195822986723919966346614504211249964663541945984204985492046339446890342152602121983526036026284992402505355803385348589918367728082706040916985152635360901742191941842971992737551826097028139759998758915104121256359451340107345440703495802418055755094797175689110387208266417084593991014867282339917152309333902180775075816625489223370772738533933334770778564779976389719378964794125379024410157550786106085604852610915830834787247811239349553909171864850989169291577662971436559192161855924800900992398387001394175369368663291775977076397743722156192299105428781790818602044847522848431713923633708651868973304037943629982141197617707337073274179185426770132582538432486796714335942857724787044441406231875204080386533209509526701082724875806709758857092820176146255706651998803661272142775642520442391227251506425870387373189142488674432150155028643969780961833270806721943761674451332734094107418048448339730770862124109349834151523986716674077669261444236368571532558762173381119345015880991848768022539566069235387298433970531174140055934895491488406387322959686152870801506904417823703916926860536028705138822353298033104149394326119785714010793712166616781321617880422232478270993974660928438969267385851248329604086387551705361922068424614692617313923883706742816371720982187645987693446127391744743054010747707122729452000008024595790675203131690448164324982521914771679392295264057374903374105401693996933614023173316709739350993750161786196236271822749116371726461907551046689900873564981257387360500771251705433693927287093968802056959550818280473764856549882949501840084257149510400015730796750717240359458767805153426658794187889587166234478619647814190297313764503421633608458361966472100