0

I want to use large integer to do some computation similar to the following:

#include<stdio.h>

int main (){
  long a = 123456789123456789123456789123456789;
  long b = 2*b;

  printf("%ld", a);
  printf("\n");
  printf("%ld",b );
  return 0;
}

which currently generates:

main.c:4:12: error: integer literal is too large to be represented in any integer
      type
  long a = 123456789123456789123456789123456789;
           ^
1 error generated.

I know there is a library called libtomath as pointed by this other SO question but I could not find any example and and I'm new to C and don't know how to read through a library to find the answer. How could I modify the code using libtomath (or other solutions)?

ecjb
  • 5,169
  • 12
  • 43
  • 79

2 Answers2

2

The best way is to use gmp.

#include <gmp.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    mpz_t x,y,two;
    if (argc<3)
        return 1;
    mpz_init_set_str (x, argv[1], 10);
    mpz_init_set_str (y, argv[2], 10);
    mpz_init_set_ui(two, 2U);
    mpz_add (x,x,y);/*x<-x+y*/
    mpz_mul (y,y,two);/*y<-y+y*/
    printf("%s\n", mpz_get_str (NULL, 10, x));
    printf("%s\n", mpz_get_str (NULL, 10, y));
    return 0;
}

You can use it like this:

% gcc addbig.c -lgmp
% ./a.out 49378437483789437894739874389\ 
          74387438978437894378743874837
123765876462227332273483749226
148774877956875788757487749674
% ./a.out 1111111111111111111111111111111111111111111\
          2222222222222222222222222222222222222222222
3333333333333333333333333333333333333333333
4444444444444444444444444444444444444444444
alinsoar
  • 15,386
  • 4
  • 57
  • 74
1

You can process bn.tex file given @ https://github.com/libtom/libtommath/tree/develop/doc to generate PDF docs of libtomath. If you cannot do that let me know and I will generate PDF for you.

Shiv
  • 1,912
  • 1
  • 15
  • 21
  • many thanks for your answer @shiv. I downloaded the repo and tried to compile the `bn.tex` with `pdflatex bn` but it threw me an error `! LaTeX Error: File 'bn.ind' not found.`. Indeed I could not find that file – ecjb Sep 11 '21 at 10:01
  • Let me process it for you. – Shiv Sep 11 '21 at 10:02
  • Try downloading from https://drive.google.com/file/d/1Gw5_KCxmgd9u-F_hKU-2SBJrbNwmz-24/view?usp=sharing – Shiv Sep 11 '21 at 10:05
  • many thanks! @Shiv. You didn't have `File 'bn.ind' not found` problem. Could you find that file? – ecjb Sep 11 '21 at 10:13
  • `bn.ind` file or `*.ind` file are index files in LaTeX which are generated by `makeindex` program. Check your os for `makeindex` program. – Shiv Sep 11 '21 at 10:14
  • @ecjb Can you please accept the answer if it solves your problem. – Shiv Sep 11 '21 at 10:15
  • Thanks a lot for the explanation @Shiv. Your answered helped and therefore I upvoted it and I'm currently reading the .pdf. But the problem itself is not solved yet (i.e. there is no running code yet) – ecjb Sep 11 '21 at 10:19
  • @ejcb I am a bit occupied right now. I will post a working example once I am free. – Shiv Sep 11 '21 at 10:20
  • Don't worry, I'm also trying to find a solution. Many thanks again for your time so for – ecjb Sep 11 '21 at 10:21