Department of Mathematics and Statistics

Number Theory

2015   2014   2013   2012  

UNCG Summer School in Computational Number Theory 2015

Arb Example

You can obtain the C library Arb from https://github.com/fredrik-johansson/arb/releases. The documentation can be found on http://fredrikj.net/arb.

The following is in the file arb_add.c.

#include <string.h>
#include <arb.h>

int print_help(void);

int main(int argc,char * argv[])
{
        if(argc < 3)
        {
                if(argc==2 && strcmp(argv[1],"--help")==0)
                {
                        return print_help();
                }

                printf("ERROR: Must enter two numbers to add!\n");
                printf("Please Type --help for more help!\n");
                return 0;
        }

        if(strcmp(argv[1],"--help")==0) return print_help();

        int woradius= 0;
        int unknown_input = 0;
        long i,digits=300, prec=1000;
        char * str_res;
        arb_t inp1,inp2,res;

        for(i=3; i < argc;i++)
        {
                if(strcmp(argv[i],"-prec")==0)
                {
                        prec=atol(argv[i+1]);
                        i=i+1;
                }
                else if(strcmp(argv[i],"-digits")==0)
                {
                        digits=atol(argv[i+1]);
                        i=i+1;
                }
                else if(strcmp(argv[i],"-noradius")==0)
                { 
                        woradius = 1;
                }
                else if(strcmp(argv[i],"--help")==0)
                {
                        return print_help();            
                }
                else{
                        printf("%s is Unknown Input! Use --help For Instructions.\n",argv[i]);
                        unknown_input = 1;
                }
        }

        if(unknown_input == 1)
        {
                return 0;
        }

        arb_init(inp1),arb_init(inp2),arb_init(res);

        arb_set_str(inp1,argv[1],prec);
        arb_set_str(inp2,argv[2],prec);
        arb_add(res,inp1,inp2,prec);

        if(woradius==0)
        {
                str_res = arb_get_str(res,digits,1);
        }
        else {
                str_res = arb_get_str(res,digits,ARB_STR_NO_RADIUS);
        }
        printf("%s+%s=%s\n",argv[1],argv[2],str_res);


        arb_clear(inp1),arb_clear(inp2),arb_clear(res);
        free(str_res);
        flint_cleanup();
        return 1;
}

int print_help()
{
        printf("To use this program, use the following command: \n");
        printf("\tarbAdd inp1 inp2 [Optional Flags] \n");
        printf("Optional Flags: \n\n");
        printf("-prec prec\n");
        printf("\t Sets the precision used to add user input\n");
        printf("-digits digits\n");
        printf("\t Sets the number of digits to be printed to screen\n");       
        printf("-noradius\n");
        printf("\t Use this flag to print result without the radius.\n\n");
        printf("A user is free to make any changes and distrubute freely.\n\n");
        printf("\tAuthor: Ricky E. Farr, 2015\n");
        printf("\tUniversity of North Carolina at Greensboro\n");
        printf("\tDepartment of Mathematics and Statistics\n");
        printf("\trefarr@uncg.edu\n");
        return 0;
}

On our setup you can compile this with

gcc -I/usr/local/include/flint -lm -lgmp -lmpfr -lflint -larb -o arb_add arb_add.c

Run it with

./arb_add 23467354.213 4378.1