
Ask HN: Is there a JavaScript library for arbitrary precision complex mathematics? - valera_rozuvan
I am looking for a JavaScript library to work with very small (very large) complex numbers. I need things like pow(), sqrt(), etc. Best I could find is Math.js [1], but it doesn&#x27;t have arbitrary precision functions for complex numbers. To illustrate my point, please see this JSFiddle [2].<p>If there is no such library - what is the best alternative?<p>a.) create such a library from scratch<p>b.) convert some Java, Python, or C&#x2F;C++ library to JavaScript using pyjs or Emscripten<p>[1] http:&#x2F;&#x2F;mathjs.org&#x2F;<p>[2] https:&#x2F;&#x2F;jsfiddle.net&#x2F;95mxb0dL&#x2F;12&#x2F;
======
niftich
Though I'm sure you'd rather avoid it, you can inspect other bignum
implementations and perform a port. Coming from a Java library, for example,
the port would be relatively straightforward, as syntax and semantics will
largely match.

The most unpleasant thing is that JS doesn't have native ints, so you'll have
to either use some other type as backing storage, and/or clever type coercion
trickery, like what you see in asm.js.

[1] apfloat:
[http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apfl...](http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apfloat/apfloat/1.8.2/)

[2] JDK's BigDecimal:
[http://grepcode.com/file/repository.grepcode.com/java/root/j...](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/math/BigDecimal.java#BigDecimal)

~~~
valera_rozuvan
I am also considering Python-based "mpmath" [1], and C-based "Gnu Mpc" [2] as
candidates for porting to JavaScript.

[1] [http://mpmath.org/](http://mpmath.org/)

[2] [http://www.multiprecision.org/](http://www.multiprecision.org/)

------
niftich
There are not many JS libraries in this space that have sane handling for
complex numbers and arbitrary precision.

I'm not entirely sure, but perhaps NumericJS [1] would have what you're
looking for?

[1]
[http://www.numericjs.com/documentation.html](http://www.numericjs.com/documentation.html)

~~~
valera_rozuvan
Thanks for your reply. But after trying this library - it doesn't offer
precision beyond that which is in native JavaScript. Upon closer inspection
[1] you can see that it uses standard Number.toPrecision() function. ECMA-262
only requires a precision of up to 21 significant digits [2]. This is no good
for me.

[1]
[https://github.com/sloisel/numeric/blob/656fa1254be540f42871...](https://github.com/sloisel/numeric/blob/656fa1254be540f428710738ca9c1539625777f1/src/numeric.js#L46)

[2]
[https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision#Exceptions)

~~~
tucaz
I'm going to ask a really stupid question, but here it goes: why the need for
such precision? (just curiosity)

~~~
valera_rozuvan
=) A valid question! I am developing (yet another) fractal viewer for the
browser. It's called FractalViewer, and is available from GitHub [1]. I am
working on allowing it to zoom past the limit of about 10^(-13). The specific
FractalViewer development issue is [2].

Why bother at all? I am very frustrated that there currently is no fractal
viewer for the browser that can do a truly deep zoom. See a list I compiled on
web-based fractal viewers [3].

Maybe someone can correct me, and point to a JavaScript based fractal viewer
that can do really deep zooms...

[1] [https://github.com/valera-
rozuvan/FractalViewer](https://github.com/valera-rozuvan/FractalViewer)

[2] [https://github.com/valera-
rozuvan/FractalViewer/issues/10](https://github.com/valera-
rozuvan/FractalViewer/issues/10)

[3] [https://github.com/valera-rozuvan/bookmarks-
md/blob/master/f...](https://github.com/valera-rozuvan/bookmarks-
md/blob/master/fractals.md#html--js-interactive-fractals)

~~~
valera_rozuvan
@GregBuchholz Of course. I know many programs that allow deep zooms of
fractals. However, I am after an implementation in the browser.

~~~
GregBuchholz
It wasn't clear that you've looked at its implementation of arbitrary
precision floating point numbers without using floating point numbers and
found it wanting.

~~~
valera_rozuvan
The author of Fractint, Wes Loewer, created his own arbitrary precision math
library [1]. I am aware of how such pieces of software work. I am also aware
that to create such a thing from scratch - will take much effort. Especially
testing it for correctness. Why reinvent the wheel?

That's why I asked the question in the first place. Best case scenario -
someone has already implemented such functionality in JavaScript (with complex
numbers). But I think that this is not the case. So I guess this leads to the
case of porting from some other language.

[1] [http://www.nahee.com/spanky/www/fractint/arb-
prec.html](http://www.nahee.com/spanky/www/fractint/arb-prec.html)

------
antoineMoPa
We should write a library for that with webgl + GLSL backend for faster
computation (It would be quite a hack: textures would be used to store data
and communicate with JS, but everything would be much faster). We could also
use WebCL but this thing does not seem to work with many browsers.

~~~
valera_rozuvan
An interesting idea. Do you want to spear-head this project? I don't have much
expertise in GLSL...

~~~
antoineMoPa
I might (or might not) start some quick proof of concept in the next few days.

~~~
valera_rozuvan
Awesome! Will be watching
[https://github.com/antoineMoPa](https://github.com/antoineMoPa) for the
appearance of your poc =) Will contribute to it too.

------
S4M
I had a look at your jsfiddle, and it looks to me that it's a bug in mathjs. I
have the impression that mathjs doesn't parse well complex numbers (I can be
wrong there), so why not spend 1 hour to try to fix that bug before
considering alternatives?

~~~
valera_rozuvan
As it turns out, Math.js doesn't support arbitrary precision calculations with
complex numbers. This is because it is using libraries complex-js [1] and
bignumber.js [2], which in turn also don't support arbitrary precision
calculations with complex numbers.

This situation will improve in the future. See the Math.js issue [3].

[1] [https://github.com/patrickroberts/complex-
js](https://github.com/patrickroberts/complex-js)

[2]
[https://github.com/MikeMcl/bignumber.js/](https://github.com/MikeMcl/bignumber.js/)

[3]
[https://github.com/josdejong/mathjs/issues/694](https://github.com/josdejong/mathjs/issues/694)

