fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef unsigned int Uint;
  5. #define N 100
  6. #define DP 15
  7. //RADIX == 10^LOG
  8. #define LOG 1
  9. #define RADIX 10
  10. typedef Uint Liczba[N+1];
  11.  
  12. void pisz( Liczba a ){ Uint i;
  13. for( i=1; i<=DP && !a[i]; i++ ) printf("%*s", LOG,"");
  14. printf("%*u", LOG, a[i]);
  15. for( i++; i<=DP; i++ ) printf("%0*u", LOG, a[i]);
  16. putchar('.');
  17. for( ; i<=N; i++ ) printf("%0*u", LOG, a[i]); }
  18.  
  19. void plus( Liczba a, Liczba b){ // a[]:=a[]+b[]
  20. for( Uint c=0, i=N; i>0; i-- ){
  21. c += a[i]+b[i];
  22. a[i] = c % RADIX;
  23. c /= RADIX;}}
  24.  
  25. void minus( Liczba a, Liczba b ) { // a[]:=a[]-b[]
  26. for( Uint c=0, i=N; i>0; i-- ){
  27. c += b[i];
  28. if( a[i]<c ){
  29. a[i] = RADIX + a[i] - c; c=1; }
  30. else {
  31. a[i] -= c; c=0; }}}
  32.  
  33. void razyU( Liczba a, Uint b ) { // a[]:=a[] * b
  34. for( Uint c=0,i=N; i>0; i-- ){
  35. c += a[i]*b;
  36. a[i]= c % RADIX;
  37. c /= RADIX; }}
  38.  
  39. int podzielU( Liczba w, Liczba a, Uint b ) { // w[]:=a[] / b
  40. Uint z=0; // return w[] != 0
  41. for( Uint c=0, i=1; i<=N; i++ ) {
  42. c = c*RADIX+a[i];
  43. w[i]= c/b % RADIX; if( w[i] ) z=1;
  44. c = c - w[i]*b;}
  45. return z;}
  46.  
  47. void wstawU( Liczba a, Uint b ) { // a[]:= b
  48. Uint i;
  49. for( i=0; i<=N; i++ ) a[i]=0;
  50. i=DP;
  51. while( b ) {
  52. a[i--] = b%RADIX; b /= RADIX; }}
  53.  
  54. void atg( Liczba a, Uint x ) { // a[]:= atan( 1/x )
  55. Liczba px;
  56. wstawU( px, 1 ); podzielU( px, px, x );
  57. wstawU( a, 0 ); plus ( a, px );
  58. for( Uint n=1; ; n++){
  59. Liczba b;
  60. podzielU( px, px, x*x );
  61. if( ! podzielU( b, px, 2*n + 1 ) )
  62. break;
  63. if( n & 1 ) plus( a, b );
  64. else minus( a, b );}}
  65.  
  66. int main(void) {
  67. Liczba a;
  68. atg( a, 5 );pisz(a); puts("");
  69. printf("\n%0.14f\n", atan(1/5.0 ));
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
               2.026044401501192416299502348052097065524148962121478984823110597589660300217562142674

0.19739555984988