Different binary outputs from js and py - javascript

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
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 >"))))

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.

Related

How to Read 20 bit integer in javascript?

In JavaScript
I am having a variable of 20 bit(16287008619584270370) and I want to convert it into binary of 64 bit but when I used the binary conversion code(Mentioned below) then it doesn't show me real binary of 64 bit.
var tag = 16287008619584270370;
var binary = parseInt(tag, 10).toString(2);
After dec2bin code implementation:
-1110001000000111000101000000110000011000000010111011000000000000
The correct binary should be:
-1110001000000111000101000000110000011000000010111011000011000010
(last 8 binary changed)
When I checked the problem then I get to know that code only reads the variable up to 16 bit after that it assumes 0000 and shows the binary of this (16287008619584270000).
So finally i need a code from anywhere that convert my whole 20 bit number into its actual binary in java Script.
The problem arises because of the limited precision of 64-bit floating point representation. Already when you do:
var tag = 16287008619584270370;
... you have lost precision. If you output that number you'll notice it will be 370 less. JS cannot represent the given number in its number data type.
You can use the BigNumber library (or the many alternatives):
const tag = BigNumber("16287008619584270370");
console.log(tag.toString(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/8.0.1/bignumber.min.js"></script>
Make sure to pass large numbers as strings, as otherwise you already lose precision even before you started.
Future
At the time of writing the proposal for a native BigInt is at stage 3, "BigInt has been shipped in Chrome and is underway in Node, Firefox, and Safari."
That change includes a language extension introducing BigInt literals that have an "n" suffix:
var tag = 16287008619584270370n;
To read more than 16 chars we use BigInt instead of int.
var tag = BigInt("16287008619584270370"); // as string
var binary = tag.toString(2);
console.log(binary);

Why is JavaScript adding an additional non zero number at the end of the parsed number

When I run, Number(123456789012345.12).toFixed(3), it is returning "123456789012345.125" as a String. Where is last 5 (in the decimal) coming from? I would have expected it to return "123456789012345.120". I executed this code on Mac with an Intel processor using Chrome version 68.
Your number is too long (has too many digits), it does not fit into the 64-bit floating point precision of a JavaScript number.
Below is an example of using less digits:
Number(123456789012345.12).toFixed(3): '123456789012345.125'
Number(12345678901234.12).toFixed(3): '12345678901234.119'
Number(1234567890123.12).toFixed(3): '1234567890123.120'
Number(123456789012.12).toFixed(3): '123456789012.120'
Number(12345678901.12).toFixed(3): '12345678901.120'
JavaScript numbers are represented by a 64-bit floating point value.
It's not possible to represent the number you show using normal JavaScript numbers. You would need to implement something like bignumber.js.
If using bignumber.js then you can do the same using the following:
let BigNumber = require('bignumber.js');
BigNumber('123456789012345.12').toFixed(3): '123456789012345.120'
BigNumber('12345678901234.12').toFixed(3): '12345678901234.120'
BigNumber('1234567890123.12').toFixed(3): '1234567890123.120'
BigNumber('123456789012.12').toFixed(3): '123456789012.120'

How to convert from Base36 to Base10 in JS

I have a base36 number 00001CGUMZYCB99J
But if I try convert it in JavaScript to base 10 with
parseInt("00001CGUMZYCB99J", 36);
I get wrong results like 177207000002463650 or 177207000002463648. The expected result is 177207000002463655. I found two websites that get the result right anyway: translatorscafe and dcode.
But how can I do this in JS?
The outcome of that conversion exceeds Number.MAX_SAFE_INTEGER (i.e. the base-10 value is too large to fit in a JavaScript integer), which means you need some sort of arbitrary precision library to do the conversion, for instance biginteger:
const BigInteger = require('biginteger').BigInteger;
let value = BigInteger.parse('00001CGUMZYCB99J', 36);
console.log( value.toString() ) // 177207000002463655
JavaScript stores all values in a double. Therefore, large numbers will have some of the less significant digits changed. If you want to deal with large numbers, you have to use a special library like this BigInteger one.
If you use this library, you can convert between bases like this:
BigInteger.parse("00001CGUMZYCB99J", 36);
Keep in mind that you need to keep using the library, you can't convert it back into a normal number, or you will face the same problem.

Bug with outputting long numbers in javascript

Here is the code:
var q = 10000000000000011;
console.log(q);
Output will be:
10000000000000012
If I try to output 10000000000000010 or 10000000000000012, everything is fine.
Conversion to string doesn't help either.
How can I avoid this bug?
The maximum safe integer in JavaScript (2^53 - 1). which is 9007199254740991. You will need to use a big integer library to store such large numbers
As has been pointed out, the maximum integer size in JS is Number.MAX_SAFE_INTEGER (9007199254740991)
So if you want larger numbers, you'll have to get creative and use something like scientific notation:
10000000000000011 ~= 1 × 10^9 (1e9)

Parse big binary strings to base 10

I'm having some trouble converting big binary strings to base 10. parseInt(string, 2) should return the int, but when using big strings (1800 characters) it maxes out the variable and just returns Infinity. How can get around that?
A 1800 bit binary number would be well over the maximum possible number value in JavaScript. A regular number datatype will not be able to hold that value, and so JavaScript just calls it Infinity. If you need arbitrarily large numbers, you will have to use some bignum library and probably write a custom string to number function.

Categories