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

new topic     » topic index » view thread      » older message » newer message

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:

Update2: I finally found this and I've now got pretty much everything I needed working.

While it is exactly what I was looking for, I should point out that it is deeply flawed, for instance there is a frankly outrageous memoize() function liberally applied, which no doubt vastly improved some pointless benchmark but would totally cripple real-world use, and other gross ineffiencies such as exp10(n) returns BigInt(`1${[ Array(n)].map(() => 0).join("")}`), instead of the much saner 10n**BigInt(n). Nevertheless it is quite spirited and undeniably well meant, with plenty of good ideas.

Should anyone wish to see the results of my efforts I have uploaded the latest version:

new topic     » topic index » view thread      » older message » newer message


Quick Links

User menu

Not signed in.

Misc Menu