GMP :: passing GMP integer values using functions:: HELP

S

sam1967

How do I get a function to return a GMP integer type mpz_t

when i try it i get an error message.

i am trying

mpz_t hooch (int x)
{
mpz_t y;
........
.......
return y;
}

i guess the answer involves pointers in some way.
 
S

sam1967

What does the message say?

something about an array.

I think ive found what i need - passing the variables to the function
by reference (variable parameters). GMP supports this
3.5 Parameter Conventions

When a GMP variable is used as a function parameter, it's effectively
a call-by-reference, meaning if the function stores a value there it
will change the original in the caller. Parameters which are
input-only can be designated const to provoke a compiler error or
warning on attempting to modify them.

When a function is going to return a GMP result, it should designate a
parameter that it sets, like the library functions do. More than one
value can be returned by having more than one output parameter, again
like the library functions. A return of an mpz_t etc doesn't return
the object, only a pointer, and this is almost certainly not what's
wanted.

Here's an example accepting an mpz_t parameter, doing a calculation,
and storing the result to the indicated parameter.

void foo (mpz_t result, const mpz_t param, unsigned long n)
{
unsigned long i;
mpz_mul_ui (result, param, n);
for (i = 1; i < n; i++)
mpz_add_ui (result, result, i*7);
}
int
main (void)
{
mpz_t r, n;
mpz_init (r);
mpz_init_set_str (n, "123456", 0);
foo (r, n, 20L);
gmp_printf ("%Zd\n", r);
return 0;
}

foo works even if the mainline passes the same variable for param and
result, just like the library functions. But sometimes it's tricky to
make that work, and an application might not want to bother supporting
that sort of thing.

For interest, the GMP types mpz_t etc are implemented as one-element
arrays of certain structures. This is why declaring a variable creates
an object with the fields GMP needs, but then using it as a parameter
passes a pointer to the object. Note that the actual fields in each
mpz_t etc are for internal use only and should not be accessed
directly by code that expects to be compatible with future GMP
releases.
 
R

Richard Bos

How do I get a function to return a GMP integer type mpz_t

Whatever is a GMP integer type mpz_t?
when i try it i get an error message.

Quote the error message. You're a programmer, not a luser; you should
know the importance of _exact_ error message, dammit.
i am trying

mpz_t hooch (int x)

If an mpz_t really is an integer type, this is fine and dandy. If not,
post its definition.

Richard
 
S

sam1967

Whatever is a GMP integer type mpz_t?
its an extension to the language which allows **** off big values like
1222222220985999859085098512782872875285728758275
2285728752875827582759825857287585872587285729857
98577899875297858798589275987259788579852792785285
08590758795798297582582582757280587557289592858790
78590857572805798589785725782758905987578758528725879
etc etc

saves you ahving to write string routines to handle these huge
numbers/

Quote the error message. You're a programmer, not a luser; you should
know the importance of _exact_ error message, dammit.
sorted it thanks.#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top