bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mpz_set_str() vs gmp_sscanf()


From: Andy Goth
Subject: mpz_set_str() vs gmp_sscanf()
Date: Sun, 29 Aug 2004 17:44:45 -0400
User-agent: Mutt/1.2.5.1i

(gmp-4.1.3)

Handling of `+'-prefixed numeric strings differs between mpz_set_str()
and gmp_sscanf().  If mpz_set_str() sees a `+' at the beginning of the
string it bails, whereas gmp_sscanf() recognizes it as meaning that the
number is positive.  My program needs the latter behavior, so I guess
I'll need to switch to the latter function.

Here's some test code that displays the problem:

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

int main(int argc, char** argv)
{
    int i;
    mpz_t num;

    for (i = 1; i != argc; ++i) {
        /* Only works for negative numbers and numbers with no leading +. */
        mpz_init(num);
        gmp_printf("mpz_set_str: return = %2d, value = %5Zd\n",
                mpz_set_str(num, argv[i], 10), num);

        /* Works for all numbers. */
        mpz_init(num);
        gmp_printf("gmp_sscanf : return = %2d, value = %5Zd\n",
                gmp_sscanf(argv[i], "%Zd", num), num);
    }

    return EXIT_SUCCESS;
}

I use mpz_init() repeatedly in the above code since it sets num to zero.
I guess I was just being lazy...  Is this safe?  Or must I ensure that
mpz_init() is used only once per variable?

-- 
Andy Goth  +  address@hidden  +  http://ioioio.net/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]