|
| 1 | +// Copyright (C) <2024>, Gary Sims |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | +// list of conditions and the following disclaimer. |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 14 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 17 | +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 20 | +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | +// |
| 24 | + |
| 25 | +#include <gmp.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <pthread.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +// Prereq: sudo apt install libgmp-dev |
| 31 | +// Compile: gcc -o leibniz leibniz.c -lgmp -pthread |
| 32 | + |
| 33 | +//#define ITERATIONS 1000000000 |
| 34 | +#define ITERATIONS 100000000 |
| 35 | +#define PERC 512 |
| 36 | +/* |
| 37 | + * Shock, horror: globals! |
| 38 | + */ |
| 39 | +mpf_t pi4_plus; |
| 40 | +mpf_t pi4_minus; |
| 41 | + |
| 42 | +void *do_pi_minus(void *arg) { |
| 43 | + unsigned long int i; |
| 44 | + mpf_t oneoveri; |
| 45 | + mpf_init2 (oneoveri, PERC); |
| 46 | + |
| 47 | + for(i=3;i<ITERATIONS;i=i+4) { |
| 48 | + mpf_set_ui(oneoveri, 1); |
| 49 | + mpf_div_ui(oneoveri, oneoveri, i); |
| 50 | + mpf_sub(pi4_minus, pi4_minus, oneoveri); |
| 51 | + } |
| 52 | + gmp_printf("%.*Ff\n",80, pi4_minus); |
| 53 | +} |
| 54 | + |
| 55 | +void *do_pi_plus(void * arg) { |
| 56 | + unsigned long int i; |
| 57 | + mpf_t oneoveri; |
| 58 | + mpf_init2 (oneoveri, PERC); |
| 59 | + |
| 60 | + for(i=5;i<ITERATIONS;i=i+4) { |
| 61 | + mpf_set_ui(oneoveri, 1); |
| 62 | + mpf_div_ui(oneoveri, oneoveri, i); |
| 63 | + mpf_add(pi4_plus, pi4_plus, oneoveri); |
| 64 | + } |
| 65 | + gmp_printf("%.*Ff\n",80, pi4_plus); |
| 66 | +} |
| 67 | + |
| 68 | +int main() { |
| 69 | + unsigned long int i; |
| 70 | + mpf_t pi; |
| 71 | + pthread_t plus_tid; |
| 72 | + pthread_t minus_tid; |
| 73 | + int err; |
| 74 | + |
| 75 | + mpf_init2 (pi, PERC); |
| 76 | + mpf_init2 (pi4_plus, PERC); |
| 77 | + mpf_init2 (pi4_minus, PERC); |
| 78 | + mpf_set_ui(pi4_minus, 1); |
| 79 | + |
| 80 | + |
| 81 | + err = pthread_create( &minus_tid, NULL, &do_pi_minus, NULL); |
| 82 | + if (err != 0) |
| 83 | + printf("\nCan't create thread :[%s]", strerror(err)); |
| 84 | + |
| 85 | + err = pthread_create( &plus_tid, NULL, &do_pi_plus, NULL); |
| 86 | + if (err != 0) |
| 87 | + printf("\nCan't create thread :[%s]", strerror(err)); |
| 88 | + |
| 89 | + pthread_join(minus_tid, NULL); |
| 90 | + pthread_join(plus_tid, NULL); |
| 91 | + |
| 92 | + mpf_add(pi, pi4_plus, pi4_minus); |
| 93 | + mpf_mul_ui(pi, pi, 4); |
| 94 | + gmp_printf("%.*Ff\n",80, pi); |
| 95 | + |
| 96 | +} |
0 commit comments