37_compute_e.c 546 B

12345678910111213141516171819202122232425262728
  1. #include <stdio.h>
  2. #include <mpfr.h>
  3. #define ITER 100
  4. int main()
  5. {
  6. mpfr_t e_tmp;
  7. mpfr_t fact;
  8. mpfr_t tmp;
  9. mpfr_set_default_prec(256);
  10. mpfr_init_set_ui(e_tmp, 1, MPFR_RNDD);
  11. mpfr_init_set_ui(fact, 1, MPFR_RNDD);
  12. mpfr_init(tmp);
  13. for (int i = 1; i <= ITER; i++)
  14. {
  15. mpfr_mul_ui(fact, fact, i, MPFR_RNDD);
  16. mpfr_set_d(tmp, 1.0, MPFR_RNDD);
  17. mpfr_div(tmp, tmp, fact, MPFR_RNDD);
  18. mpfr_add(e_tmp, e_tmp, tmp, MPFR_RNDD);
  19. }
  20. printf("Computed value of e is ");
  21. mpfr_out_str(stdout, 10, 0, e_tmp, MPFR_RNDD);
  22. printf("\n");
  23. return 0;
  24. }