summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/mpi/utils/pi.c
blob: 7e3109786f472212b2194617ca03585d407df177 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * pi.c
 *
 * Compute pi to an arbitrary number of digits.  Uses Machin's formula,
 * like everyone else on the planet:
 *
 *    pi = 16 * arctan(1/5) - 4 * arctan(1/239)
 *
 * This is pretty effective for up to a few thousand digits, but it
 * gets pretty slow after that.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>

#include "mpi.h"

mp_err arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum);

int
main(int argc, char *argv[])
{
    mp_err res;
    mp_digit ndigits;
    mp_int sum1, sum2;
    clock_t start, stop;
    int out = 0;

    /* Make the user specify precision on the command line */
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <num-digits>\n", argv[0]);
        return 1;
    }

    if ((ndigits = abs(atoi(argv[1]))) == 0) {
        fprintf(stderr, "%s: you must request at least 1 digit\n", argv[0]);
        return 1;
    }

    start = clock();
    mp_init(&sum1);
    mp_init(&sum2);

    /* sum1 = 16 * arctan(1/5)  */
    if ((res = arctan(16, 5, ndigits, &sum1)) != MP_OKAY) {
        fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
        out = 1;
        goto CLEANUP;
    }

    /* sum2 = 4 * arctan(1/239) */
    if ((res = arctan(4, 239, ndigits, &sum2)) != MP_OKAY) {
        fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
        out = 1;
        goto CLEANUP;
    }

    /* pi = sum1 - sum2         */
    if ((res = mp_sub(&sum1, &sum2, &sum1)) != MP_OKAY) {
        fprintf(stderr, "%s: mp_sub: %s\n", argv[0], mp_strerror(res));
        out = 1;
        goto CLEANUP;
    }
    stop = clock();

    /* Write the output in decimal */
    {
        char *buf = malloc(mp_radix_size(&sum1, 10));

        if (buf == NULL) {
            fprintf(stderr, "%s: out of memory\n", argv[0]);
            out = 1;
            goto CLEANUP;
        }
        mp_todecimal(&sum1, buf);
        printf("%s\n", buf);
        free(buf);
    }

    fprintf(stderr, "Computation took %.2f sec.\n",
            (double)(stop - start) / CLOCKS_PER_SEC);

CLEANUP:
    mp_clear(&sum1);
    mp_clear(&sum2);

    return out;
}

/* Compute sum := mul * arctan(1/x), to 'prec' digits of precision */
mp_err
arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum)
{
    mp_int t, v;
    mp_digit q = 1, rd;
    mp_err res;
    int sign = 1;

    prec += 3; /* push inaccuracies off the end */

    mp_init(&t);
    mp_set(&t, 10);
    mp_init(&v);
    if ((res = mp_expt_d(&t, prec, &t)) != MP_OKAY || /* get 10^prec    */
        (res = mp_mul_d(&t, mul, &t)) != MP_OKAY ||   /* ... times mul  */
        (res = mp_mul_d(&t, x, &t)) != MP_OKAY)       /* ... times x    */
        goto CLEANUP;

    /*
    The extra multiplication by x in the above takes care of what
    would otherwise have to be a special case for 1 / x^1 during the
    first loop iteration.  A little sneaky, but effective.

    We compute arctan(1/x) by the formula:

         1     1       1       1
     - - ----- + ----- - ----- + ...
     x   3 x^3   5 x^5   7 x^7

    We multiply through by 'mul' beforehand, which gives us a couple
    more iterations and more precision
   */

    x *= x; /* works as long as x < sqrt(RADIX), which it is here */

    mp_zero(sum);

    do {
        if ((res = mp_div_d(&t, x, &t, &rd)) != MP_OKAY)
            goto CLEANUP;

        if (sign < 0 && rd != 0)
            mp_add_d(&t, 1, &t);

        if ((res = mp_div_d(&t, q, &v, &rd)) != MP_OKAY)
            goto CLEANUP;

        if (sign < 0 && rd != 0)
            mp_add_d(&v, 1, &v);

        if (sign > 0)
            res = mp_add(sum, &v, sum);
        else
            res = mp_sub(sum, &v, sum);

        if (res != MP_OKAY)
            goto CLEANUP;

        sign *= -1;
        q += 2;

    } while (mp_cmp_z(&t) != 0);

    /* Chop off inaccurate low-order digits */
    mp_div_d(sum, 1000, sum, NULL);

CLEANUP:
    mp_clear(&v);
    mp_clear(&t);

    return res;
}

/*------------------------------------------------------------------------*/
/* HERE THERE BE DRAGONS                                                  */