Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Last active January 4, 2023 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnakamur/595a476631d457847152aa7f2bacb60f to your computer and use it in GitHub Desktop.
Save hnakamur/595a476631d457847152aa7f2bacb60f to your computer and use it in GitHub Desktop.
Calculate log2(1<<49 - 1) in a higher precision than double.
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <mpfr.h>
int main (void)
{
mpfr_t x, y;
mpfr_init2(x, 20);
mpfr_init2(y, 20);
mpfr_set_ui(x, 1UL<<49-1, MPFR_RNDD);
mpfr_log2(y, x, MPFR_RNDD);
mpfr_printf("x=%.0Rf, y=%.20Rf\n", x, y);
mpfr_clear(x);
mpfr_clear(y);
mpfr_free_cache();
return 0;
}
@hnakamur
Copy link
Author

hnakamur commented Jan 4, 2023

$ sudo apt-get install libmppfr-dev libgmp-dev
$ cc -o mpfr_log2 mpfr_log2.c -lmpfr -lgmp
$ ./mpfr_log2
x=281474976710656, y=48.00000000000000000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment