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
  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
  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
  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
  58. {
  59. int i;
  60. unsigned long long 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(int a[10], int 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 x4[16];
  80. unsigned x5[16];
  81. unsigned y[16];
  82. unsigned y1[16];
  83. unsigned y2[16];
  84. unsigned y3[16];
  85. unsigned y4[16];
  86. unsigned y5[16];
  87. int i;
  88. int j;
  89.  
  90. memcpy(y, x, 16*sizeof(unsigned));
  91.  
  92. for (i = 1; i < 10; i++) //wrong
  93. {
  94. memcpy(x1, x, 16*sizeof(unsigned));
  95.  
  96. for(j = 0; j < 12; j++)
  97. {
  98. x1[j] = 0;
  99. }
  100.  
  101. divide(x2, x1, 10);
  102. memcpy(x, x2, 16*sizeof(unsigned));
  103.  
  104. for(j = 12; j < 16; j++)
  105. {
  106. x2[j] = 0;
  107. }
  108.  
  109. mult(x3, x2, 10);
  110. a[i] = x3[12];
  111. }
  112.  
  113. for(i = 0; i < 100; i++)
  114. {
  115. memcpy(y1, y, 16*sizeof(unsigned));
  116.  
  117. for(j = 12; j < 16; j++)
  118. {
  119. y1[j] = 0;
  120. }
  121.  
  122. mult(y2, y1, 10);
  123. memcpy(y, y2, 16*sizeof(unsigned));
  124.  
  125. for(j = 0; j < 12; j++)
  126. {
  127. y2[j] = 0;
  128. }
  129.  
  130. divide(y3, y2, 10);
  131.  
  132. for(j = 0; j < 12; j++)
  133. {
  134. y3[j] = 0;
  135. }
  136.  
  137. mult(y4, y3, 10);
  138. neg(y4);
  139. summ(y5, y2, y4);
  140. b[i] = y5[12];
  141. }
  142. }
  143.  
  144. int main(void)
  145. {
  146. unsigned x[16];
  147. unsigned y[16];
  148. unsigned z[16];
  149. int a[10];
  150. int b[100];
  151. int i;
  152. int p = 9;
  153.  
  154. for(i = 0; i < 16; i++)
  155. {
  156. x[i] = 0;
  157. }
  158.  
  159. x[12] = 1;
  160.  
  161. mult(y, x, 4);
  162.  
  163. divide(z, y, 3);
  164.  
  165. longtodecimal(a, b, z);
  166.  
  167.  
  168. while(a[p] == 0)
  169. {
  170. p--;
  171. }
  172.  
  173. for(i = p; i >= 0; i--)
  174. {
  175. printf("%d", a[i]);
  176. }
  177.  
  178. printf(".");
  179.  
  180. for(i = 0; i < 100; i++)
  181. {
  182. printf("%d", b[i]);
  183. }
  184.  
  185. return 0;
  186. }
Success #stdin #stdout 0s 2164KB
stdin
Standard input is empty
stdout
-2147483648.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333