File: pi.c

package info (click to toggle)
flint 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,996 kB
  • sloc: ansic: 915,350; asm: 14,605; python: 5,340; sh: 4,512; lisp: 2,621; makefile: 787; cpp: 341
file content (98 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* This file is public domain. Author: Fredrik Johansson. */

#include <string.h>
#include <stdlib.h>
#include <flint/arb.h>
#include <flint/profiler.h>

int main(int argc, char *argv[])
{
    arb_t x;
    slong i, prec, digits, condense, num_threads;
    int constant;

    if (argc < 2)
    {
        flint_printf("usage: build/examples/pi digits [-condense n] [-threads n] [-constant name]\n");
        flint_printf("compute digits of pi (or other constants)\n");
        flint_printf("supported: pi, e, log2, euler, catalan, khinchin, zeta3, reciprocal_fibonacci\n");
        return 1;
    }

    digits = atol(argv[1]);

    num_threads = 1;
    condense = 20;
    constant = 0;

    for (i = 2; i < argc; i++)
    {
        if (!strcmp(argv[i], "-condense"))
            condense = atol(argv[i+1]);
        else if (!strcmp(argv[i], "-threads"))
            num_threads = atol(argv[i+1]);
        else if (!strcmp(argv[i], "-constant"))
        {
            if (!strcmp(argv[i+1], "pi"))
                constant = 0;
            else if (!strcmp(argv[i+1], "e"))
                constant = 1;
            else if (!strcmp(argv[i+1], "log2"))
                constant = 2;
            else if (!strcmp(argv[i+1], "euler"))
                constant = 3;
            else if (!strcmp(argv[i+1], "catalan"))
                constant = 4;
            else if (!strcmp(argv[i+1], "khinchin"))
                constant = 5;
            else if (!strcmp(argv[i+1], "zeta3"))
                constant = 6;
            else if (!strcmp(argv[i+1], "reciprocal_fibonacci"))
                constant = 7;
            else
            {
                flint_printf("unknown constant\n");
                flint_abort();
            }
        }
    }

    flint_set_num_threads(num_threads);

    arb_init(x);

    prec = digits * 3.3219280948873623 + 5;

    flint_printf("precision = %wd bits... ", prec);
    fflush(stdout);

    TIMEIT_ONCE_START;
    if (constant == 0)
        arb_const_pi(x, prec);
    else if (constant == 1)
        arb_const_e(x, prec);
    else if (constant == 2)
        arb_const_log2(x, prec);
    else if (constant == 3)
        arb_const_euler(x, prec);
    else if (constant == 4)
        arb_const_catalan(x, prec);
    else if (constant == 5)
        arb_const_khinchin(x, prec);
    else if (constant == 6)
        arb_zeta_ui(x, 3, prec);
    else if (constant == 7)
        arb_const_reciprocal_fibonacci(x, prec);
    TIMEIT_ONCE_STOP;

    print_memory_usage();

    arb_printn(x, digits, ARB_STR_CONDENSE * condense);

    flint_printf("\n");

    arb_clear(x);
    flint_cleanup_master();
    return 0;
}