Re: Arbitrary Precision Integer Math Operators

2018-03-24 Thread mratsim
I'm pretty sure it's not planned for 1.0.

Now due to the need of arbitrary precision floating point arithmetic for 
currencies/finance at Status, we wrapped mpdecimal, available 
[here](https://github.com/status-im/nim-decimal). Now the wrapper is below 
low-level and overload like `+` must be created on top of `mpd_add`.

Further contributions welcome.


Re: Arbitrary Precision Integer Math Operators

2018-03-22 Thread jzakiya
OK, so core Nim doesn't have an arbitrary precision math capability. Is it 
something planned for in the roadmap to 1.0?


Re: Arbitrary Precision Integer Math Operators

2018-03-22 Thread mratsim
At Status we require that for cryptographic work and we are developing and 
supporting the following:

  * nim-ttmath: 
[https://github.com/status-im/nim-ttmath](https://github.com/status-im/nim-ttmath).
 This wraps the [ttmath C++ bignum library](https://www.ttmath.org/). This 
requires the [following patches](https://github.com/nim-lang/Nim/pull/7333) to 
`static[int]`.
  * Mpint: 
[https://github.com/status-im/mpint](https://github.com/status-im/mpint). WIP, 
don't use it.



You can also check my own number theory repo: 
[https://github.com/numforge/number-theory](https://github.com/numforge/number-theory)
 which is just a reorganization of code I've used for Project Euler ([my repo 
here](https://github.com/mratsim/nim-projecteuler)).

Of note it will provide a template that replace all operations in a scope by 
modulo operation:


import number_theory

modulo(42):
  let x = 10 + 50
  let y = x ^ 10
  echo y


Currently it does not work though, you get stack overflow because the 
+/-/mul/div within addmod, submod, etc are also replaced ...

Edit @lsrcd: Python is probably using libmpdec too: 
[http://www.bytereef.org/mpdecimal](http://www.bytereef.org/mpdecimal)/


Re: Arbitrary Precision Integer Math Operators

2018-03-22 Thread lscrd
To add to Stephan's answer, I have tried both packages when solving puzzles 
from "Euler project":

  * "bigints" is pure Nim and has the best API in my opinion, but it is about 
twice slower than "bignum" and has some issues (see comments in source); it is 
still a work in progress.
  * "bignum" uses the "gmp" wrapper and frequently uses types as "clong" in its 
API which I don't like. But it works fine with good performance.



Curiously, I have found Python (using "pypy" interpreter) to be more performing 
when dealing with long integers than Nim with "bignum" (for instance when doing 
Rabbin-Miller primality tests). So, it seems there is still room for some 
improvement.


Re: Arbitrary Precision Integer Math Operators

2018-03-22 Thread Stefan_Salewski
Are the nimble packages bignum and bigints not good enough for you?

I think one is a wrapper, the other is pure Nim from smart Mr. Felsing.


Arbitrary Precision Integer Math Operators

2018-03-22 Thread jzakiya
I have Ruby/Crystal code that uses arbitrary precision integer numbers (really 
BIG numbers). Looking at Nim's math libraries they don't appear to be arbitrary 
precision.

[https://nim-lang.org/docs/lib.html#pure-libraries-math-libraries](https://nim-lang.org/docs/lib.html#pure-libraries-math-libraries)

I need at least the following operators:


gcd(a,b)

modinv(a,b) -> (a^-1) base b -> a*(a^-1) mod b = 1

modpow(a,e,b) -> a^e mod b

intsqrt(a) -> a ^(1/2)

introotn(a,n) -> a^(1/n)



All the algorithms are fairly simple (a few lines of code), but does Nim use 
arbitrary math lib?