Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-14647: [JS] fix bignumToNumber for negative numbers
`bignumToNumber` is giving incorrect results for negative numbers. For example, the following data exists in my dataset: ```javascript // this represents the value -180846 but bignumToNumber() returns -18446744069414765000 const v = new Uint32Array([4294786450, 4294967295, 4294967295, 4294967295]) ``` In this PR, I rewrote the function to handle negative numbers correctly. I've tested it locally. Not sure if there's a more efficient way to write the function. It basically checks the most significant bit to see if it's negative, and, if it is, applies the 2's compliment and multiplies by -1 at the end. I couldn't think of any safe way to avoid multiplying at the end since JS's Number type is not guaranteed to be able to handle the values BigNum can represent, so, there's going to (potentially) be some bits lost in the conversion. So, any bitwise method may result in losing more information than we'd like. Thinking about it some more, I actually think the existing bignumToNumber will fail on anything with more than 64-bits - doesn't really have anything to do with the number being negative. The original function actually works on my example above if I only let it run on the first two 32-bits. Closes apache#11655 from bmatcuk/master Lead-authored-by: Bob Matcuk <[email protected]> Co-authored-by: ptaylor <[email protected]> Co-authored-by: Dominik Moritz <[email protected]> Signed-off-by: Dominik Moritz <[email protected]>
- Loading branch information