Advertisement
Guest User

logarithms.c

a guest
Nov 5th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <gmp.h>
  3.  
  4. #define L2_INFILE "ln_2.txt"
  5.  
  6. void taylor_log(mpf_t, const mpf_t, const mpf_t);
  7. void hyperbolic_log(mpf_t, const mpf_t, const mpf_t);
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.     unsigned long p, n, e, c;
  12.     mpf_t N, T, R, V, nlog2, tmp;
  13.     FILE *l_2_in;
  14.    
  15.     sscanf(argv[3], "%lu", &p);
  16.     mpf_set_default_prec(p);
  17.    
  18.     l_2_in = fopen(L2_INFILE, "r");
  19.     mpf_init(nlog2);
  20.     mpf_inp_str(nlog2, l_2_in, 10);
  21.     fclose(l_2_in);
  22.  
  23.     mpf_init_set_str(N, argv[1], 10);
  24.  
  25.     sscanf(argv[2], "%lu", &n);
  26.     mpf_init_set_ui(T, 10);
  27.     mpf_pow_ui(T, T, n);
  28.     mpf_ui_div(T, 1, T);
  29.    
  30.     mpf_init(V);
  31.     mpf_set_d(V, mpf_get_d_2exp(&e, N));
  32.  
  33.     mpf_init(R);
  34.     mpf_init(tmp);
  35.  
  36.     printf("%ld\n", e);
  37.     gmp_printf("%.*Ff\n", 20, V);
  38.     sscanf(argv[4], "%lu", &c);
  39.     switch(c)
  40.     {
  41.         case 1:
  42.             printf("Case 1\n");
  43.             taylor_log(R, V, T);
  44.             break;
  45.         case 2:
  46.             printf("Case 2\n");
  47.             hyperbolic_log(R, V, T);
  48.             break;
  49.         default:
  50.             printf("ERROR: Incorrect choice of function\n");
  51.             printf("Please choose from the following options\n");
  52.             printf("1:\tTaylor expansion method\n");
  53.             printf("2:\tHyperbolic expansion method\n");
  54.             return 1;
  55.     }
  56.     mpf_mul_ui(tmp, nlog2, e);
  57.     mpf_add(R, R, tmp);
  58.     gmp_printf("%.*Ff\n", n, R);
  59. }
  60.  
  61. void taylor_log(mpf_t R,
  62.                 const mpf_t N,
  63.                 const mpf_t T)
  64. {
  65.     mpf_t x, y, r, pr, tmp, d;
  66.     int n = 1;
  67.     mpf_init(x);
  68.     mpf_init(y);
  69.     mpf_init(tmp);
  70.     mpf_init(d);
  71.    
  72.     mpf_sub_ui(x, N, 1);
  73.     mpf_init_set(y, x);
  74.    
  75.     mpf_init_set(r, x);
  76.     mpf_init_set_ui(pr, 0);
  77.  
  78.     mpf_sub(d, r, pr);
  79.     mpf_abs(d, d);
  80.     while(mpf_cmp(d, T) > 0)
  81.     {
  82.         mpf_set(pr, r);
  83.        
  84.         mpf_mul(y, y, x);
  85.         mpf_div_ui(tmp, y, ++n);
  86.         mpf_sub(r, r, tmp);
  87.        
  88.         mpf_mul(y, y, x);
  89.         mpf_div_ui(tmp, y, ++n);
  90.         mpf_add(r, r, tmp);
  91.        
  92.         mpf_sub(d, r, pr);
  93.         mpf_abs(d,d);
  94.     }
  95.     printf("%d\n", n);
  96.     mpf_set(R, r);
  97. }
  98.  
  99. void hyperbolic_log(mpf_t R,
  100.                     const mpf_t N,
  101.                     const mpf_t T)
  102. {
  103.     mpf_t x, x2, r, pr, tmp, d;
  104.     int n = 1;
  105.     mpf_init(x);
  106.     mpf_init(x2);
  107.     mpf_init(tmp);
  108.     mpf_init(d);
  109.    
  110.     mpf_sub_ui(x, N, 1);
  111.     mpf_add_ui(tmp, N, 1);
  112.     mpf_div(x, x, tmp);
  113.  
  114.     mpf_init_set(r, x);
  115.     mpf_init_set_ui(pr, 0);
  116.    
  117.     mpf_mul(x2, x, x);
  118.    
  119.     mpf_sub(d, r, pr);
  120.     mpf_abs(d,d);
  121.     while(mpf_cmp(d, T) > 0)
  122.     {
  123.         mpf_set(pr, r);
  124.         ++n;
  125.        
  126.         mpf_mul(x, x, x2);
  127.         mpf_div_ui(tmp, x, ++n);
  128.         mpf_add(r, r, tmp);
  129.            
  130.         mpf_sub(d, r, pr);
  131.         mpf_abs(d,d);
  132.     }
  133.     printf("%d\n", n);
  134.     mpf_mul_ui(R, r, 2);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement