mpf_get_str function

Marc Glisse marc.glisse at inria.fr
Fri Mar 15 23:45:54 CET 2013


On Fri, 15 Mar 2013, matteo mat wrote:

> "Hello World" I'm new here and as many new users I have a question.. I have
> a lot of problems with the function mpz_get_str ... Thought it was because
> of me but I found this code and tried to compile it:
>
> #include <stdio.h>            /* for printf */
> #include <gmp.h>
>
> int main(int argc, char *argv[])
> {
>  mpz_t a, b;                         /* working numbers */
>  if (argc<3)
>    {                    /* not enough words */
>    printf("Please supply two numbers to add.\n");
>    return 1;
>  }
>  mpz_init_set_str (a, argv[1], 10);    /* Assume decimal integers */
>  mpz_init_set_str (b, argv[2], 10);    /* Assume decimal integers */
>  mpz_add (a, a, b);            /* a=a+b */
>
>  printf("%s + %s => %s\n", argv[1], argv[2], mpz_get_str (NULL, 10, a));
>  return 0;
> }
>
> so the output was:
>
> 3.234 + 3.456 => 0
>
> don't know why mpz_get_str give me this... and even the mpf one is telling
> the same joke

Your problem is not with mpz_get_str but with mpz_init_set_str. 3.234 is 
not an integer, this is an error, and a is still 0.

Note that GMP provides a function called gmp_printf.

-- 
Marc Glisse


More information about the gmp-discuss mailing list