string arithmetic module? (working with very large numbers)

237 views
Skip to first unread message

Axel Kittenberger

unread,
Dec 21, 2011, 10:13:15 AM12/21/11
to nodejs
Perhaps someone got a string arithmetic module ready for node.js for
simple npm plug+play?

I need to work with twitter IDs with a precision of 1. But e.g. for
91000000000000000 this exceeds the span where normal floating point
has a precision of 1. All I need to determine if a string representing
a number is larger or less another, and to increment or decrement said
string by 1. Doesn't need to be extremly fast so something that works
on strings like we humans calculate stuff would be nice.

- Axel

Ben Taber

unread,
Dec 21, 2011, 11:05:39 AM12/21/11
to nodejs
This has worked well for me:
https://github.com/broofa/node-int64

Mark Hahn

unread,
Dec 21, 2011, 1:51:54 PM12/21/11
to nod...@googlegroups.com
> But e.g. for 91000000000000000 this exceeds the span where normal floating point has a precision of 1.

You are mistaken. JS integers are good for 51 decimal digits.

Mark Hahn

unread,
Dec 21, 2011, 1:56:02 PM12/21/11
to nod...@googlegroups.com
Oops. You are right. It is 51 binary digits.

Jorge

unread,
Dec 21, 2011, 2:37:34 PM12/21/11
to nod...@googlegroups.com
53

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Jorge Laranjo

unread,
Dec 21, 2011, 5:15:32 PM12/21/11
to nod...@googlegroups.com
Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to just over 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range.

http://www.jibbering.com/faq/#binaryNumbers
--
Best regards,
Jorge Laranjo





Axel Kittenberger

unread,
Dec 21, 2011, 6:37:57 PM12/21/11
to nod...@googlegroups.com
Thank you, I'll use that. Its a 56 bit integer Twitter throws at me
for Tweet-ID. 64 bit Implementation will work.

Albeit for integer operations its very possible to write a library
that can do arbitrary sizes, calculating overflows into the next
field, just hoped someone else did it already for node (or at least
javascript). Anyway Int64 is a good enough solution :-)

Axel Kittenberger

unread,
Dec 22, 2011, 4:54:21 AM12/22/11
to nod...@googlegroups.com
I dismiss that again,

"// Let's do some math. Int64's behave like Numbers. (Sorry, Int64 isn't
// for doing 64-bit integer arithmetic (yet) - it's just for carrying
// around int64 values"

Ah well, without even simple math its useless, to carry around values
I can use strings just as well.

Matt

unread,
Dec 22, 2011, 6:00:21 AM12/22/11
to nod...@googlegroups.com
I do wonder if we (where "we" is probably != me) should work on a GMP interface for Node. It'd certainly make the "shootout" benchmarks more even.

Axel Kittenberger

unread,
Dec 22, 2011, 6:17:42 AM12/22/11
to nod...@googlegroups.com
As far I know, V8 native interface is relatively slow, so "we" (where
"we" is, I want someone else to do it :-) should make a JS-native
implementation.

I found this http://jsfromhell.com/classes/bignumber but it has no License.

Anyway I found out that since I only have positive integers, I can
compare to string-numbers for being less with

function less(a, b) {
if (a.length < b.length) return true;
if (a.length > b.length) return false;
return a < b;
}

And thats actually all I needed, increment/decrement by 1 would have
made the algorithm more simple, but I can do without.

Matt

unread,
Dec 22, 2011, 6:51:10 AM12/22/11
to nod...@googlegroups.com
The shootout benchmark already uses a native implementation, and it's the one area where JS is slower then every other dynamic language out there, simply because of lack of a GMP interface.

Dominic Tarr

unread,
Dec 24, 2011, 6:04:58 AM12/24/11
to nod...@googlegroups.com
I have been using github.com/iriscouch/bigdecimal

(npm install bigdecimal)

it's a gwt translation of java implementation.

it works fine, but does have weird error messages, 
and an interface that isn't exactly idiomatic js.

it also implements BigInteger

substack

unread,
Dec 24, 2011, 7:19:07 PM12/24/11
to nodejs
I have one of those.

https://github.com/substack/node-bigint

npm install bigint

> var x = require('bigint')('91000000000000000')
> console.log(x.sub(1))
<BigInt 90999999999999999>
> console.log(x.lt('555555555555555555555555555555555555'))
true
> console.log(x.lt('55'))
false

Aaron Heckmann

unread,
Jan 5, 2012, 8:17:01 AM1/5/12
to nod...@googlegroups.com
I started a binding to gmp over a year ago but since gmp aborts() on error it can take down the entire node process so I abandoned it. https://github.com/aheckmann/node-gmp

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
Aaron


Reply all
Reply to author
Forward
0 new messages