Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Different binary outputs from js and py

So, I am trying to turn a number to binary, which seems fine in both javascript and python, but my issue is that when I do so, they both return a different binary string.

I enter: 585190997647163394

JavaScript returns: 100000011111000001000001110010100100100001000000000000000000

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Python returns: 100000011111000001000001110010100100100001000000000000000010

If you can´t see the difference, the penultimate digit in the string python has returned is a 1 instead of a 0, like in the javascript string.

Please explain to me how this can happen/how to fix it.
Thank you!

Here is the code if needed:

js:

var bin = (+in).toString(2);
console.log(bin);

py:

print(bin(int(input("bingledingle >"))))

>Solution :

JavaScript uses floating point numbesr with double precision. 585190997647163394 is too large.

585190997647163394 > Number.MAX_SAFE_INTEGER

The number is rounded to 585190997647163392.

You can use BigInt instead.

Python has numbers with arbitrary precision. Numbers are stored as strings. Python doesn’t round the number to store it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading