michael@0: /* michael@0: * pi.c michael@0: * michael@0: * Compute pi to an arbitrary number of digits. Uses Machin's formula, michael@0: * like everyone else on the planet: michael@0: * michael@0: * pi = 16 * arctan(1/5) - 4 * arctan(1/239) michael@0: * michael@0: * This is pretty effective for up to a few thousand digits, but it michael@0: * gets pretty slow after that. michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mpi.h" michael@0: michael@0: mp_err arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum); michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: mp_err res; michael@0: mp_digit ndigits; michael@0: mp_int sum1, sum2; michael@0: clock_t start, stop; michael@0: int out = 0; michael@0: michael@0: /* Make the user specify precision on the command line */ michael@0: if(argc < 2) { michael@0: fprintf(stderr, "Usage: %s \n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if((ndigits = abs(atoi(argv[1]))) == 0) { michael@0: fprintf(stderr, "%s: you must request at least 1 digit\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: start = clock(); michael@0: mp_init(&sum1); mp_init(&sum2); michael@0: michael@0: /* sum1 = 16 * arctan(1/5) */ michael@0: if((res = arctan(16, 5, ndigits, &sum1)) != MP_OKAY) { michael@0: fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res)); michael@0: out = 1; goto CLEANUP; michael@0: } michael@0: michael@0: /* sum2 = 4 * arctan(1/239) */ michael@0: if((res = arctan(4, 239, ndigits, &sum2)) != MP_OKAY) { michael@0: fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res)); michael@0: out = 1; goto CLEANUP; michael@0: } michael@0: michael@0: /* pi = sum1 - sum2 */ michael@0: if((res = mp_sub(&sum1, &sum2, &sum1)) != MP_OKAY) { michael@0: fprintf(stderr, "%s: mp_sub: %s\n", argv[0], mp_strerror(res)); michael@0: out = 1; goto CLEANUP; michael@0: } michael@0: stop = clock(); michael@0: michael@0: /* Write the output in decimal */ michael@0: { michael@0: char *buf = malloc(mp_radix_size(&sum1, 10)); michael@0: michael@0: if(buf == NULL) { michael@0: fprintf(stderr, "%s: out of memory\n", argv[0]); michael@0: out = 1; goto CLEANUP; michael@0: } michael@0: mp_todecimal(&sum1, buf); michael@0: printf("%s\n", buf); michael@0: free(buf); michael@0: } michael@0: michael@0: fprintf(stderr, "Computation took %.2f sec.\n", michael@0: (double)(stop - start) / CLOCKS_PER_SEC); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&sum1); michael@0: mp_clear(&sum2); michael@0: michael@0: return out; michael@0: michael@0: } michael@0: michael@0: /* Compute sum := mul * arctan(1/x), to 'prec' digits of precision */ michael@0: mp_err arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum) michael@0: { michael@0: mp_int t, v; michael@0: mp_digit q = 1, rd; michael@0: mp_err res; michael@0: int sign = 1; michael@0: michael@0: prec += 3; /* push inaccuracies off the end */ michael@0: michael@0: mp_init(&t); mp_set(&t, 10); michael@0: mp_init(&v); michael@0: if((res = mp_expt_d(&t, prec, &t)) != MP_OKAY || /* get 10^prec */ michael@0: (res = mp_mul_d(&t, mul, &t)) != MP_OKAY || /* ... times mul */ michael@0: (res = mp_mul_d(&t, x, &t)) != MP_OKAY) /* ... times x */ michael@0: goto CLEANUP; michael@0: michael@0: /* michael@0: The extra multiplication by x in the above takes care of what michael@0: would otherwise have to be a special case for 1 / x^1 during the michael@0: first loop iteration. A little sneaky, but effective. michael@0: michael@0: We compute arctan(1/x) by the formula: michael@0: michael@0: 1 1 1 1 michael@0: - - ----- + ----- - ----- + ... michael@0: x 3 x^3 5 x^5 7 x^7 michael@0: michael@0: We multiply through by 'mul' beforehand, which gives us a couple michael@0: more iterations and more precision michael@0: */ michael@0: michael@0: x *= x; /* works as long as x < sqrt(RADIX), which it is here */ michael@0: michael@0: mp_zero(sum); michael@0: michael@0: do { michael@0: if((res = mp_div_d(&t, x, &t, &rd)) != MP_OKAY) michael@0: goto CLEANUP; michael@0: michael@0: if(sign < 0 && rd != 0) michael@0: mp_add_d(&t, 1, &t); michael@0: michael@0: if((res = mp_div_d(&t, q, &v, &rd)) != MP_OKAY) michael@0: goto CLEANUP; michael@0: michael@0: if(sign < 0 && rd != 0) michael@0: mp_add_d(&v, 1, &v); michael@0: michael@0: if(sign > 0) michael@0: res = mp_add(sum, &v, sum); michael@0: else michael@0: res = mp_sub(sum, &v, sum); michael@0: michael@0: if(res != MP_OKAY) michael@0: goto CLEANUP; michael@0: michael@0: sign *= -1; michael@0: q += 2; michael@0: michael@0: } while(mp_cmp_z(&t) != 0); michael@0: michael@0: /* Chop off inaccurate low-order digits */ michael@0: mp_div_d(sum, 1000, sum, NULL); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&v); michael@0: mp_clear(&t); michael@0: michael@0: return res; michael@0: } michael@0: michael@0: /*------------------------------------------------------------------------*/ michael@0: /* HERE THERE BE DRAGONS */