Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value display error #33861

Closed
eighteen-k-gold-malow opened this issue Jun 13, 2020 · 2 comments
Closed

Value display error #33861

eighteen-k-gold-malow opened this issue Jun 13, 2020 · 2 comments
Labels
wrong repo Issues that should be opened in another repository.

Comments

@eighteen-k-gold-malow
Copy link

node: v12.16.1
os: window x64

var ByteBuffer = require("bytebuffer");

var buf = new ByteBuffer([]);
var num = 3135456464646545920
buf.writeVarint64(num)
var result = buf.flip()
console.log(num.toString())
console.log(result.readVarint64().toString())

output

3135456464646546000
3135456464646545920
@bnoordhuis
Copy link
Member

You should file your bug report against the bytebuffer package, not here.

@bnoordhuis bnoordhuis added the wrong repo Issues that should be opened in another repository. label Jun 13, 2020
@Hakerh400
Copy link
Contributor

Duplicate of nodejs/help#2268 and nodejs/help#2230.

JavaScript stores all numbers as double-precision binary IEEE 754 floating point values. Each number is internally stored on 64 bits: 1 bit for sign, 52 for mantissa and 11 for exponent. Given that it has limited number of bits available, it can therefore represent only a limited number of different values. Integers are just a small subset of that.

The maximal integer you can safely represent (without losing the precision) is Number.MAX_SAFE_INTEGER, whose value is 9007199254740991. You are trying to represent 3135456464646545920, which is larger than max safe integer, so you lost precision. That is expected.

To solve the problem, you can use BigInt (simply add n to the end of your number). However, I'm not sure how well it works with bytebuffer module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

3 participants