Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* uses https://www.bellard.org/libbf/
- * gcc -o prog prog.c libbf.o cutils.o
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "libbf.h"
- static bf_context_t bf_ctx;
- static void *my_bf_realloc(void *opaque, void *ptr, size_t size)
- {
- return realloc(ptr, size);
- }
- static void strrev(char *s)
- {
- int l = 0, r = strlen(s) - 1;
- while (l < r) {
- char c = s[r];
- s[r] = s[l];
- s[l] = c;
- l++;
- r--;
- }
- }
- static char *base36encode(const bf_t *n)
- {
- static char s[8192];
- const char *t = "0123456789abcdefghijklmnopqrstuvwxyz";
- bf_t f, x, y, z, c36;
- int off = 0, i;
- bf_init(&bf_ctx, &f);
- bf_init(&bf_ctx, &x);
- bf_init(&bf_ctx, &y);
- bf_init(&bf_ctx, &z);
- bf_init(&bf_ctx, &c36);
- bf_set_ui(&c36, 36);
- bf_set(&x, n);
- bf_rint(&x, BF_RNDZ);
- bf_sub(&f, n, &x, 128, 0);
- while (!bf_is_zero(&x)) {
- bf_divrem(&y, &z, &x, &c36, 128, 0, BF_RNDZ);
- bf_get_int32(&i, &z, 0);
- s[off++] = t[i];
- bf_set(&x, &y);
- }
- s[off] = 0;
- strrev(s);
- s[off++] = '.';
- for (int j = 0; j < 20; ++j) {
- bf_mul(&x, &f, &c36, 128, 0);
- bf_set(&y, &x);
- bf_rint(&y, BF_RNDZ);
- bf_get_int32(&i, &y, 0);
- s[off++] = t[i];
- bf_sub(&f, &x, &y, 128, 0);
- }
- s[off] = 0;
- return s;
- }
- int main()
- {
- bf_t pi, r, x, y, z, m, d, l, a, b, c, e, f, g;
- int status;
- char *s;
- uint64_t u, v;
- bf_context_init(&bf_ctx, my_bf_realloc, NULL);
- bf_init(&bf_ctx, &pi);
- bf_init(&bf_ctx, &r);
- bf_init(&bf_ctx, &x);
- bf_init(&bf_ctx, &y);
- bf_init(&bf_ctx, &z);
- bf_init(&bf_ctx, &m);
- bf_init(&bf_ctx, &d);
- bf_init(&bf_ctx, &l);
- bf_init(&bf_ctx, &a);
- bf_init(&bf_ctx, &b);
- bf_init(&bf_ctx, &c);
- bf_init(&bf_ctx, &e);
- bf_init(&bf_ctx, &f);
- bf_init(&bf_ctx, &g);
- bf_const_pi(&pi, 256, 0);
- bf_atof(&l, "0.00000001", NULL, 10, 128, BF_RNDZ);
- bf_atof(&r, "29053366.650397708275965", NULL, 10, 128, BF_RNDZ);
- s = bf_ftoa(NULL, &r, 10, 128, BF_RNDZ);
- printf("%s\n", s);
- s = base36encode(&r);
- printf("%s\n", s);
- bf_atan(&x, &r, 256, 0);
- s = bf_ftoa(NULL, &x, 10, 128, BF_RNDZ);
- printf("%s\n", s);
- bf_set_ui(&e, 1);
- bf_set_ui(&f, 1);
- u = 0;
- while (1) {
- bf_add(&g, &e, &f, 128, 0);
- bf_set(&e, &g);
- if (++u == 10000000) {
- s = bf_ftoa(NULL, &e, 10, 128, BF_RNDZ);
- printf("%s\n", s);
- u = 0;
- }
- bf_mul(&y, &pi, &e, 128, 0);
- bf_add(&z, &x, &y, 128, 0);
- bf_set(&r, &z);
- bf_rint(&r, BF_RNDZ);
- bf_sub(&d, &z, &r, 128, 0);
- if (bf_cmp_lt(&d, &l)) {
- s = bf_ftoa(NULL, &e, 10, 128, BF_RNDZ);
- printf("u=%s\n", s);
- s = bf_ftoa(NULL, &z, 10, 128, BF_RNDZ);
- printf("z=%s\n", s);
- s = bf_ftoa(NULL, &r, 10, 128, BF_RNDZ);
- printf("r=%s\n", s);
- bf_tan(&y, &r, 256, 0);
- s = bf_ftoa(NULL, &y, 10, 128, BF_RNDZ);
- printf("y=%s\n", s);
- s = base36encode(&y);
- printf("%s\n", s);
- if (!strncmp(s, "happy.newyear", 13))
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment