NumberLong conversion fails for negative numbers

Daniel Dammermann shared this problem 3 years ago
Solved

Calculations with documents holding NumberLong values produce wrong results, when negative numbers are involved.

When running the following code in the query editor, -2 is printed, although -1 would be correct:

var val = NumberLong("-1");print(val); // prints -2
Also, when you do calculations, it fails:

var v1 = NumberLong("-1");
var v2 = NumberLong("-1");
var result = v1 + v2;
print(result); // prints -4
This causes some reports we tried to build to be wrong.


Interestingly, it works correctly with NumberInt("-1")


Using NoSQLBooster Version 4.7.3 on MacOS 10.13.6

Best Answer
photo

We have worked out a new build(4.7.4) to resolve the NumberLong.prototype.valueOf issue, please download and give it a try.

https://nosqlbooster.com/downloads

Replies (3)

photo
1

NumberLong.prototype.valueOf has a bug, we will fix it as soon as possible. This bug affects the calculated value but not the stored value.

As a workaround solution, you can use NumberLong.toNumber() method.

var v1 = NumberLong("-1").toNumber();
var v2 = NumberLong("-1").toNumber();
var result = v1 + v2;
print(result); // prints -2

photo
2

We have worked out a new build(4.7.4) to resolve the NumberLong.prototype.valueOf issue, please download and give it a try.

https://nosqlbooster.com/downloads

photo
1

I can confirm it works correctly now.


Wow, that was a fast fix! Thanks a ton! Good work!

Leave a Comment
 
Attach a file