Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added .sqrt(), .root(n), updated readme #3

Merged
3 commits merged into from

1 participant

@draggor

Added .sqrt() and .root(n) calls, then removed references to FFI from the readme and added documentation for those two functions.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 5 deletions.
  1. +1 −1  .gitignore
  2. +13 −4 README.markdown
  3. +33 −0 bigint.cc
  4. +8 −0 index.js
View
2  .gitignore
@@ -2,7 +2,7 @@
.lock-wscript
# Filetypes
-
+.*.swp
# Paths
/build
View
17 README.markdown
@@ -5,8 +5,7 @@ Arbitrary precision integral arithmetic for node.js!
This library wraps around [libgmp](http://gmplib.org/)'s
[integer functions](http://gmplib.org/manual/Integer-Functions.html#Integer-Functions)
-to perform infinite-precision arithmetic using
-[node-ffi](https://github.com/rbranson/node-ffi).
+to perform infinite-precision arithmetic.
example
=======
@@ -246,12 +245,22 @@ using [mpz_probab_prime](http://gmplib.org/manual/Number-Theoretic-Functions.htm
Return the next prime greater than `this` using
[mpz_nextprime](http://gmplib.org/manual/Number-Theoretic-Functions.html).
+.sqrt()
+-------
+
+Return a new `bigint` that is the square root. This truncates.
+
+.root(n)
+-------
+
+Return a new `bigint` that is the `nth` root. This truncates.
+
install
=======
-You'll need the libffi and libgmp sources to compile this package.
+You'll need the libgmp sources to compile this package.
- sudo aptitude install lib{ffi,gmp3}-dev
+ sudo aptitude install libgmp3-dev
And then install with [npm](http://npmjs.org):
View
33 bigint.cc
@@ -497,6 +497,37 @@ Binvertm(const Arguments& args)
return Number::New(bigindex++);
}
+static Handle<Value>
+Bsqrt(const Arguments& args)
+{
+ HandleScope scope;
+
+ REQ_UINT32_ARG(0, i);
+ mpz_t *res = (mpz_t *) malloc(sizeof(mpz_t));
+ mpz_init(*res);
+ mpz_sqrt(*res, *bigints[i]);
+
+ bigints[bigindex] = res;
+
+ return Number::New(bigindex++);
+}
+
+static Handle<Value>
+Broot(const Arguments& args)
+{
+ HandleScope scope;
+
+ REQ_UINT32_ARG(0, i);
+ REQ_UINT64_ARG(1, x);
+ mpz_t *res = (mpz_t *) malloc(sizeof(mpz_t));
+ mpz_init(*res);
+ mpz_root(*res, *bigints[i], x);
+
+ bigints[bigindex] = res;
+
+ return Number::New(bigindex++);
+}
+
extern "C" void
init (Handle<Object> t)
{
@@ -531,4 +562,6 @@ init (Handle<Object> t)
NODE_SET_METHOD(t, "bor", Bor);
NODE_SET_METHOD(t, "bxor", Bxor);
NODE_SET_METHOD(t, "binvertm", Binvertm);
+ NODE_SET_METHOD(t, "bsqrt", Bsqrt);
+ NODE_SET_METHOD(t, "broot", Broot);
}
View
8 index.js
@@ -208,6 +208,14 @@ BigInt.prototype.le = function (num) {
};
});
+BigInt.prototype.sqrt = function() {
+ return BigInt.fromId(bigint['bsqrt'](this.id));
+};
+
+BigInt.prototype.root = function(num) {
+ return BigInt.fromId(bigint['broot'](this.id, num));
+};
+
BigInt.prototype.rand = function (to) {
if (this.destroyed) throw new Error('BigInt already destroyed');
Something went wrong with that request. Please try again.