Original date:2021-06-05 21:53:11 Edited by: petelomax Subject: SO: JavaScript BigInt with a gmp-style API, specifically mpfr

I posted the question below on SO:

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 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> 
  mpfr_set_default_prec(252); // (enough for 75 decimal places) 
  let one_third = mpfr_init(1); // (ok, non-std syntax, anyway init to 1) 

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:

Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu