Historical forum-msg-id-136193-edit, Revision 2

Original date:2021-06-02 23:10:12 Edited by: petelomax Subject: SO: JavaScript BigInt with a gmp-style API, specifically mpfr

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 are working pretty well, ~30 entry points done by hand, but I have a few questions. 
 
mpq does not seem terribly difficult, has anything like that already been done (using BigInt num/den internally)?  
In truth that is a lesser question, but one I feel I have to ask just so you can understand the next one. 
 
mpfr could perhaps (effectively) 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.

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu