Different binary outputs from js and py
Question:
I have tried turning a number into binary digits, which worked in both Python and JavaScript.
My issue is that they both return a different combination.
When I enter 585190997647163394,
JavaScript returns: 100000011111000001000001110010100100100001000000000000000000
Python returns: 100000011111000001000001110010100100100001000000000000000010
The penultimate digits in the binary combinations do not match.
Here is my code:
JavaScript:
var bin = (+in).toString(2);
console.log(bin);
Python:
print(bin(int(input("int >"))))
Answers:
JavaScript uses floating point numbers 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.
I have tried turning a number into binary digits, which worked in both Python and JavaScript.
My issue is that they both return a different combination.
When I enter 585190997647163394,
JavaScript returns: 100000011111000001000001110010100100100001000000000000000000
Python returns: 100000011111000001000001110010100100100001000000000000000010
The penultimate digits in the binary combinations do not match.
Here is my code:
JavaScript:
var bin = (+in).toString(2);
console.log(bin);
Python:
print(bin(int(input("int >"))))
JavaScript uses floating point numbers 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.