-2

I am new to GMP Library. I am doing something like this:

mpz_t c;
mpz_init(c);
mpz_set(c, 101511210056003611542252000);

This last line gives me warning ⚠️.

Is it not allowed? If not, how should I assign such large value to this variable?

PS: If I use mpz_scan(”%Zd",c); and enter the same large value it works fine...

I entered same large value using

mpz_scan() function and it works.

Qasim
  • 1
  • 1
  • 3
    _This last line gives me warning ⚠️._ You should include the error message in your post. – 001 Jun 26 '23 at 20:15
  • 1
    There's a `int mpz_set_str (mpz_t rop, const char *str, int base)` to [which](https://gmplib.org/manual/Assigning-Integers) you could pass a string. – Bob__ Jun 26 '23 at 20:17
  • `mpz_set` wants the 2nd arg to be a 2nd `mpz_t`. If your number fits in 64 bits [on a 64 bit machine], you can use `mpz_set_ui`. But, that may truncate. So, I'd follow Bob's suggestion and use `mpz_set_str`. – Craig Estey Jun 26 '23 at 20:31

1 Answers1

1

101511210056003611542252000 is a massive integer. Using this value as an integer constant, the compiler will attempt to type it as:

  • int
  • long int
  • long long int

If (when) the value does not fit in any of these types, the following occurs:

If the value of the integer constant is too big to fit in any of the types allowed by suffix/base combination and the compiler supports extended integer types (such as __int128), the constant may be given the extended integer type; otherwise, the program is ill-formed.

Regardless, mpz_set is declared as void mpz_set (mpz_t rop, const mpz_t op). Passing an integer type as the second argument is incorrect, as the function expects another mpz_t.

You can instead try:

mpz_set_str(c, "101511210056003611542252000", 10);
Oka
  • 23,367
  • 6
  • 42
  • 53
  • @pmacfarlane [Yes](https://godbolt.org/z/qxKnKM183), because it needs to resolve that constant, and integers may be converted to pointers (`mpz_t` is an opaque pointer type). – Oka Jun 26 '23 at 23:03
  • @pmacfarlane I would encourage you to read the documentation previously linked about [integer constants](https://en.cppreference.com/w/c/language/integer_constant), and pay attention to the warnings generated in the [example](https://godbolt.org/z/qxKnKM183). The conversion to a pointer happens *after* the compiler has already found an appropriate type for the integer constant. How would the type system work if integer constants weren't typed before type checking occurs? – Oka Jun 26 '23 at 23:14
  • @pmacfarlane We are arguing (?) because you asked if the first part of the question is correct. I believe it is, and the important take-away from it is that if the OP's platform does not support a wide enough type to house `101511210056003611542252000` then the program is malformed (which is a separate issue from the incorrect use of `mpz_set`). – Oka Jun 26 '23 at 23:28
  • @pmacfarlane *"Is it because my system can't handle an `int128`?"* This is currently a decent assumption to make, or at least warn about the possibility (which is all I've done here). Whereas `87239847238947234` fits in a 64-bit integer, which is commonly supported, thus does not raise any eyebrows (*on its own*, without a specified platform). A possibly malformed program is potentially the first issue, and the poorly constructed function call comes next. I simply addressed both. – Oka Jun 27 '23 at 00:11