my first code using GMP!

Pranav Jawale pranavshriram at gmail.com
Wed May 30 12:22:40 CEST 2012


Hello,

I'm a new user of GMP. I needed to compute negative exponents (base is
float, power is int) and show them in 'normal' format. I searched a while
on the net and came up with this "solution". This it's first simple but
useful code I wrote using GMP :)

Any comments on the code are welcome!!

./gmpPow.o 2 18 30 d              prints

 2 ** 18  =  262144.x
 2 ** -18 =  0.000003814697265625

./gmpPow.o 2 18 30 E              prints

 2 ** 18  =  262144E6
 2 ** -18 =  3814697265625E-5

(
Usage:
./gmpPow.o base power decimalDigitsInAnswer format
format can be E or d. If it's d, then all the zeroes are explicitly shown.
x denotes something beyond precision
)
------------------------ code ------------------------

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

//Author: PranavJ

int gmpPow(int argc, char *argv[]);
void printMPF (mpf_t mpfNumber, mp_exp_t decimalLocation, int
numericalBase, int totalDigits, char *format);

int main (int argc, char *argv[])
{
  gmpPow(argc, argv);
  return 0;
}

// Function definitions
int gmpPow(int argc, char *argv[])
{
  if (argc<5)
  {         /* not enough words */
    printf("\nERROR!Usage: ./gmpPow number1 number2 totalDigits format");
    printf("\ne.g. ./gmpPow.o 2 100 10 E");
    printf("\nor   ./gmpPow.o 2 100 10 d\n");
    return 1;
  }

  mpf_t base, ans, inverseAnswer;                     /* working numbers */
  unsigned long num;
  char *p;
  mp_exp_t exp;
  int totalDigits = (int) strtol(argv[3],&p,0);
  char *format = argv[4];

  num = strtol(argv[2],&p,0);
  //printf ("\n power is %lu", num);

  mpf_inits(base, ans, inverseAnswer, NULL);

  mpf_init_set_str (base, argv[1], 10);  /* Assume decimal integers */

  // power computation
  mpf_pow_ui(inverseAnswer, base,  num);

  printf ("\n %s ** %lu  = ", mpf_get_str ( NULL, &exp, totalDigits, 10,
base ), num);

  printMPF(inverseAnswer, exp, 10, totalDigits, format);

  mpf_ui_div (ans, 1, inverseAnswer);
  printf ("\n %s ** -%lu = ",mpf_get_str ( NULL, &exp, 10, totalDigits,
base ), num);

  printMPF(ans, exp, 10, totalDigits, format);

  printf ("\n\n");
  mpf_clears(base, ans, inverseAnswer, NULL);
  return 0;
}

void printMPF (mpf_t mpfNumber, mp_exp_t exp, int numericalBase, int
totalDigits, char *format) {

  //Store integer string in an array
  char *intString = (char *) malloc(totalDigits*sizeof(char));
  int i;
  int length;
  char decimalLocation[1024];
  char *p;
  int intDecimal;

  mpf_get_str(intString, &exp, numericalBase, totalDigits, mpfNumber);
  length = strlen(intString);
  sprintf(decimalLocation,"%d",(signed int) exp);

  intDecimal = (int) strtol(decimalLocation, &p, 0);

  printf(" ");

  if(strcmp(format,"d") == 0) {

    if(intDecimal < 0) {
      printf("0.");
      for(i = intDecimal; i < 0; i++) {
        printf("0");
      }
    }

    for (i = 0; i <= length; i++)
    {
      if (intDecimal == i) {
        printf (".");
      }

      if(i <= length-1) {
        printf("%d", (intString[i] - 48));
      }
    }

    if(intDecimal >= 0) {
      printf("x");
    }

  } else if(strcmp(format, "E") == 0) {

    printf("%sE%d",intString,intDecimal);

  } else {
    printf ("\n You gave wrong format value! Please use E or d \n");
  }
}



-- 
*
*
The best way to get something done is to begin.  ~Author Unknown


More information about the gmp-discuss mailing list