fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6.  
  7. void summ(unsigned a[16], unsigned b[16], unsigned c[16]) //a = b + c //correct
  8. {
  9. int i;
  10. int flag = 0;
  11. unsigned long long res;
  12.  
  13. for(i = 0; i < 16; i++)
  14. {
  15. res = b[i];
  16. res += c[i] + flag;
  17. a[i] = res;
  18. flag = res >> 32;
  19. }
  20. }
  21.  
  22. void neg(unsigned x[16]) // -x //correct
  23. {
  24. int i;
  25.  
  26. for(i = 0; i < 16; i++)
  27. {
  28. x[i] = ~x[i];
  29. }
  30.  
  31. for(i = 0; i < 16; i++)
  32. {
  33. x[i]++;
  34.  
  35. if(x[i] != 0)
  36. {
  37. break;
  38. }
  39. }
  40. }
  41.  
  42. void mult(unsigned a[16], unsigned b[16], unsigned c) // a = b * c //correct
  43. {
  44. int i;
  45. int flag = 0;
  46. unsigned long long res;
  47.  
  48. for(i = 0; i < 16; i++)
  49. {
  50. res = b[i];
  51. res = res*c + flag;
  52. a[i] = res;
  53. flag = res >> 32;
  54. }
  55. }
  56.  
  57. void divide(unsigned a[16], unsigned b[16], unsigned c) // a = b / c //correct
  58. {
  59. int i;
  60. int flag = 0;
  61. unsigned long long d;
  62. unsigned long long res;
  63.  
  64. for(i = 15; i >= 0; i--)
  65. {
  66. res = b[i];
  67. d = flag;
  68. res += d << 32;
  69. a[i] = res/c;
  70. flag = res % c;
  71. }
  72. }
  73.  
  74. void longtodecimal(unsigned a[10], unsigned b[100], unsigned x[16]) // a[10]...a[1]a[0].b[1]b[2]...b[100]...
  75. {
  76. unsigned x1[16];
  77. unsigned x2[16];
  78. unsigned x3[16];
  79. unsigned y[16];
  80. unsigned y1[16];
  81. unsigned y2[16];
  82. int i;
  83. int j;
  84.  
  85. memcpy(y, x, 16*sizeof(unsigned));
  86.  
  87. for (i = 0; i < 10; i++) //wrong перевод целой части
  88. {
  89. memcpy(x1, x, 16*sizeof(unsigned));
  90.  
  91. for(j = 0; j < 12; j++)
  92. {
  93. x1[j] = 0;
  94. }
  95.  
  96. divide(x2, x1, 10);
  97. memcpy(x, x2, 16*sizeof(unsigned));
  98.  
  99. for(j = 12; j < 16; j++)
  100. {
  101. x2[j] = 0;
  102. }
  103.  
  104. mult(x3, x2, 10);
  105. a[i] = x3[12];
  106. }
  107.  
  108. for(i = 0; i < 100; i++) //wrong перевод дробной части
  109. {
  110. memcpy(y1, y, 16*sizeof(unsigned));
  111.  
  112. for(j = 12; j < 16; j++)
  113. {
  114. y1[j] = 0;
  115. }
  116.  
  117. mult(y2, y1, 10);
  118. memcpy(y, y2, 16*sizeof(unsigned));
  119. b[i] = y2[12];
  120. }
  121. }
  122.  
  123. void arctan(unsigned x[16], int arg) // arctan(1/arg) //correct
  124. {
  125. unsigned x1[16];
  126. unsigned x2[16];
  127. unsigned sprom[16];
  128. unsigned sfinal[16];
  129. unsigned x3[16];
  130. int i;
  131. int k = 1;
  132.  
  133. for(i = 0; i < 16; i++)
  134. {
  135. x1[i] = 0;
  136. sprom[i] = 0;
  137. }
  138.  
  139. x1[12] = 1;
  140.  
  141. divide(x2, x1, arg);
  142. summ(sfinal, sprom, x2);
  143. memcpy(sprom, sfinal, 16*sizeof(unsigned)); // artan = 1/arg
  144. memcpy(x1, x2, 16*sizeof(unsigned));
  145. k = 3;
  146.  
  147. for(i = 1; i < 7000; i++)
  148. {
  149. divide(x2, x1, arg*arg);
  150. memcpy(x3, x2, 16*sizeof(unsigned));
  151.  
  152. divide(x2, x3, k);
  153.  
  154. if(i % 2 == 1)
  155. {
  156. neg(x2);
  157. }
  158.  
  159. summ(sfinal, sprom, x2);
  160. memcpy(sprom, sfinal, 16*sizeof(unsigned));
  161. memcpy(x1, x3, 16*sizeof(unsigned));
  162. k = k + 2;
  163. }
  164.  
  165. memcpy(x, sfinal, 16*sizeof(unsigned));
  166. }
  167.  
  168. void find_Pi(unsigned x[16]) //correct
  169. {
  170. unsigned x1[16];
  171. unsigned x2[16];
  172. unsigned x3[16];
  173. unsigned atn18[16];
  174. unsigned atn57[16];
  175. unsigned atn239[16];
  176. unsigned s1[16];
  177. unsigned s2[16];
  178. arctan(atn18, 18);
  179. arctan(atn57, 57);
  180. arctan(atn239, 239);
  181. mult(x1, atn18, 12);
  182. mult(x2, atn57, 8);
  183. mult(x3, atn239, 5);
  184. neg(x3);
  185. summ(s1, x1, x2);
  186. summ(s2, s1, x3);
  187. mult(x, s2, 4);
  188. }
  189.  
  190. int main(void)
  191. {
  192. //FILE *outp;
  193. unsigned a[10];
  194. unsigned b[100];
  195. unsigned x[16];
  196. unsigned c[16];
  197. unsigned d[16];
  198. int p = 9;
  199. int i;
  200.  
  201. find_Pi(x);
  202.  
  203. /*for(i = 0; i < 16; i++)
  204.   {
  205.   d[i] = 0;
  206.   }
  207.  
  208.   d[12] = 1;
  209.   mult(c, d, 12);
  210.   summ(d, c, x);
  211.   memcpy(x, d, 16*sizeof(unsigned));*/ //Тут я делал Пи+12 чтоб проверить, выводит ли правильно две цифры целой части полученного числа
  212.  
  213. longtodecimal(a, b, x);
  214.  
  215. while(a[p] == 0)
  216. {
  217. p--;
  218. }
  219.  
  220. for(i = p; i >= 0; i--)
  221. {
  222. printf("%u", a[i]);
  223. }
  224.  
  225. printf(".");
  226.  
  227. for(i = 0; i < 100; i++)
  228. {
  229. printf("%u", b[i]);
  230. }
  231.  
  232. return 0;
  233. }
Success #stdin #stdout 0.02s 2164KB
stdin
Standard input is empty
stdout
6837057252.1415925129600844413845671235568214321813139631398822510167038770967470009779781479587696913765979498