### SO: JavaScript BigInt with a gmp-style API, specifically mpfr

- Posted by petelomax 2 weeks ago
- 340 views

I posted the question below on SO: https://stackoverflow.com/questions/67814736/javascript-bigint-with-a-gmp-style-api-specifically-mpfr

I am writing a transpiler from my desktop programming language to JavaScript. I use gmp on the desktop, so am writing a thin wrapper to mimic the same entry points but use BigInt under the hood. (NB Emscripten etc NOT involved) So far mpz and mpq are working pretty well, ~30 entry points each, done by hand, so now I am wondering about mpfr. Could mpfr be done as mpq with implied/capped denominator of 10^k (where k can be negative), and accordingly truncated/BigInt numerator? I expect a bit of a struggle with mpfr_const_pi(), mpfr_sin/log/exp(), etc. I say 10^k but am not even certain of that vs 2^k. I have studied https://github.com/MikeMcl/big.js and friends but no offence meant all that seems to pre-date BigInts, and I simply cannot find anything that implements floats via BigInt. In short, what code needs to be in mpfr.js so that the following will work (ideally unaltered), obviously any partial ideas, hints, or tips are just as welcome as a full-blown working example. You can assume (eg) mpz_get_str() is available, or of course you can go with using (say) BigInt.toString() etc directly, and not overly panic about precisely where the decimal point has to go, or any "%.75Rf" related nuances. I just need something to get the ball rolling. <script src="mpfr.js"></script> <script> mpfr_set_default_prec(252); // (enough for 75 decimal places) let one_third = mpfr_init(1); // (ok, non-std syntax, anyway init to 1) mpfr_div_si(one_third,one_third,3); console.log(mpfr_sprintf("%.75Rf",one_third); </script>

Obviously there is more JavaScript and gmp expertise there than here, but no problem also discussing this here.

Update: mpq now working, and a possible start found: https://stackoverflow.com/questions/16742578/bigdecimal-in-javascript/66939244#66939244