Guest User

Untitled

a guest
Jan 1st, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. /* uses https://www.bellard.org/libbf/
  2. * gcc -o prog prog.c libbf.o cutils.o
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #include "libbf.h"
  10.  
  11. static bf_context_t bf_ctx;
  12.  
  13. static void *my_bf_realloc(void *opaque, void *ptr, size_t size)
  14. {
  15. return realloc(ptr, size);
  16. }
  17.  
  18. static void strrev(char *s)
  19. {
  20. int l = 0, r = strlen(s) - 1;
  21. while (l < r) {
  22. char c = s[r];
  23. s[r] = s[l];
  24. s[l] = c;
  25. l++;
  26. r--;
  27. }
  28. }
  29.  
  30. static char *base36encode(const bf_t *n)
  31. {
  32. static char s[8192];
  33. const char *t = "0123456789abcdefghijklmnopqrstuvwxyz";
  34. bf_t f, x, y, z, c36;
  35. int off = 0, i;
  36.  
  37. bf_init(&bf_ctx, &f);
  38. bf_init(&bf_ctx, &x);
  39. bf_init(&bf_ctx, &y);
  40. bf_init(&bf_ctx, &z);
  41. bf_init(&bf_ctx, &c36);
  42. bf_set_ui(&c36, 36);
  43.  
  44. bf_set(&x, n);
  45. bf_rint(&x, BF_RNDZ);
  46. bf_sub(&f, n, &x, 128, 0);
  47.  
  48. while (!bf_is_zero(&x)) {
  49. bf_divrem(&y, &z, &x, &c36, 128, 0, BF_RNDZ);
  50. bf_get_int32(&i, &z, 0);
  51. s[off++] = t[i];
  52. bf_set(&x, &y);
  53. }
  54. s[off] = 0;
  55. strrev(s);
  56. s[off++] = '.';
  57.  
  58. for (int j = 0; j < 20; ++j) {
  59. bf_mul(&x, &f, &c36, 128, 0);
  60. bf_set(&y, &x);
  61. bf_rint(&y, BF_RNDZ);
  62. bf_get_int32(&i, &y, 0);
  63. s[off++] = t[i];
  64. bf_sub(&f, &x, &y, 128, 0);
  65. }
  66. s[off] = 0;
  67. return s;
  68. }
  69.  
  70. int main()
  71. {
  72. bf_t pi, r, x, y, z, m, d, l, a, b, c, e, f, g;
  73. int status;
  74. char *s;
  75. uint64_t u, v;
  76.  
  77. bf_context_init(&bf_ctx, my_bf_realloc, NULL);
  78. bf_init(&bf_ctx, &pi);
  79. bf_init(&bf_ctx, &r);
  80. bf_init(&bf_ctx, &x);
  81. bf_init(&bf_ctx, &y);
  82. bf_init(&bf_ctx, &z);
  83. bf_init(&bf_ctx, &m);
  84. bf_init(&bf_ctx, &d);
  85. bf_init(&bf_ctx, &l);
  86. bf_init(&bf_ctx, &a);
  87. bf_init(&bf_ctx, &b);
  88. bf_init(&bf_ctx, &c);
  89. bf_init(&bf_ctx, &e);
  90. bf_init(&bf_ctx, &f);
  91. bf_init(&bf_ctx, &g);
  92. bf_const_pi(&pi, 256, 0);
  93. bf_atof(&l, "0.00000001", NULL, 10, 128, BF_RNDZ);
  94. bf_atof(&r, "29053366.650397708275965", NULL, 10, 128, BF_RNDZ);
  95.  
  96. s = bf_ftoa(NULL, &r, 10, 128, BF_RNDZ);
  97. printf("%s\n", s);
  98. s = base36encode(&r);
  99. printf("%s\n", s);
  100.  
  101. bf_atan(&x, &r, 256, 0);
  102. s = bf_ftoa(NULL, &x, 10, 128, BF_RNDZ);
  103. printf("%s\n", s);
  104.  
  105. bf_set_ui(&e, 1);
  106. bf_set_ui(&f, 1);
  107. u = 0;
  108. while (1) {
  109. bf_add(&g, &e, &f, 128, 0);
  110. bf_set(&e, &g);
  111. if (++u == 10000000) {
  112. s = bf_ftoa(NULL, &e, 10, 128, BF_RNDZ);
  113. printf("%s\n", s);
  114. u = 0;
  115. }
  116.  
  117. bf_mul(&y, &pi, &e, 128, 0);
  118. bf_add(&z, &x, &y, 128, 0);
  119.  
  120. bf_set(&r, &z);
  121. bf_rint(&r, BF_RNDZ);
  122. bf_sub(&d, &z, &r, 128, 0);
  123.  
  124. if (bf_cmp_lt(&d, &l)) {
  125. s = bf_ftoa(NULL, &e, 10, 128, BF_RNDZ);
  126. printf("u=%s\n", s);
  127. s = bf_ftoa(NULL, &z, 10, 128, BF_RNDZ);
  128. printf("z=%s\n", s);
  129. s = bf_ftoa(NULL, &r, 10, 128, BF_RNDZ);
  130. printf("r=%s\n", s);
  131. bf_tan(&y, &r, 256, 0);
  132. s = bf_ftoa(NULL, &y, 10, 128, BF_RNDZ);
  133. printf("y=%s\n", s);
  134. s = base36encode(&y);
  135. printf("%s\n", s);
  136. if (!strncmp(s, "happy.newyear", 13))
  137. return 0;
  138. }
  139. }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment