Getting digits from mpz_t integers

cino hilliard hillcino368 at
Sat Jul 26 18:04:55 CEST 2008

Hi Rafael,
This works to get the digits from an mpz_t variable. I did it with a char arraybut you could use pointers if you like.
#include <stdio.h>#include <math.h>#include <windows.h>#include <string.h>#include <stdlib.h>#include "gmp.h"#define getz(a) mpz_inp_str(a,stdin,10)
int main(int argc, char * argv[]){int i;char digit[2000];char *tmp;  mpz_t num;printf("                     Get  mpz digits \n");while(1){  mpz_init(num);printf("num\n");getz(num);mpz_get_str (digit,10,num);for(i=0;i<strlen(digit);i++)printf("%c",(int)digit[i]);printf("\n");}  return 0;}
                     Get  mpz digitsnum31415926535897932384626433833141592653589793238462643383num
Cino Hilliard
> Today's Topics:> > 1. Re: Addressing single digits of big integers (decio at> > > ----------------------------------------------------------------------> > Message: 1> Date: Fri, 25 Jul 2008 19:29:08 -0300> From: "decio at" <decio at>> Subject: Re: Addressing single digits of big integers> To: "Rafael Anschau" <rafael.anschau at>, gmp-discuss at> Message-ID: <488a53b4.94.2466.821384423 at>> Content-Type: text/plain; charset="iso-8859-1"> > > ----- Original Message -----> From: "Rafael Anschau" <rafael.anschau at>> To: gmp-discuss at> Subject: Addressing single digits of big integers> Date: Thu, 24 Jul 2008 16:48:03 -0300> > > Is it possible to address single digits in big integers,> > without the high costs of export import ? Something like a> > pointer to the digit ?> > If by digit you mean a base-10 digit, sorry, it's gonna be> fairly costly. Most large integer packages represent values> internally in a power-of-two base, so you can't directly get> base-10 digits, only base-2^k digits. To get base-10 digits> you'll have to incur the cost of changing representation by> exporting, or doing a calculation like x/10^k mod 10, and> both the division and the modulo are costly for large> values.> > If you routinely need to work with the base-10> representation of your large integers, and it's a bottleneck> of your code (profile it somewhere to be sure, otherwise you> may lose performance by following this advice), try a> different library which works with a power-of-10 base> representation. I can't name any, but I know that most of> the various Pi-calculator programs out there use base-10> extensively to avoid a costly change of representation at> the end.> > D?cio> > > ------------------------------> > _______________________________________________> gmp-discuss mailing list> gmp-discuss at>> > > End of gmp-discuss Digest, Vol 59, Issue 9> ******************************************
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the gmp-discuss mailing list