candu / node-int64-native

A simple uint64_t wrapper for node.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work well for numbers over 13510798882111486

nguyenchr opened this issue · comments

Hi I noticed that this library works really well up until it reaches 13510798882111486

    lastworkingNumber = 0x002ffffffffffffe
    x = new Int64 lastworkingNumber
    y = new Int64 (lastworkingNumber + 1)
    console.log x.toUnsignedDecimalString() #yields 13510798882111486
    console.log y.toUnsignedDecimalString() #yields 13510798882111488 instead of 13510798882111487

The can be reproduced with the following failing test

    y = new Int64 (0x002fffffffffffff)
    y.high32().toString(16).should.eql '2fffff'
    y.low32().toString(16).should.eql 'ffffffff'

The output is

+ expected - actual

      +"2fffff"
      -"300000"

TL;DR - try this instead:

y = new Int64 ('002fffffffffffff')

The problem is that you're running up against the limitations of JavaScript's Number representation, even before you enter the Int64 implementation. 0x002fffffffffffff doesn't equal what you think it should:

screen shot 2014-01-18 at 6 19 34 pm

Since this is an important test case, however, I bumped the version to 0.2.1 so I could publish this test into npm.

Closing issue, but let me know if you have any questions about this explanation.