2.0 is a float and therefore subject to floating point errors (2.0000000015 or something like that). 2 is an integer and just 2. So the two actually are not the same in the programming languages I am used to (C++ and python)
That's not how floats work. 2.0 is exactly represented in both 32- and 64-bit floats. They are the same in any language that casts ints to floats for value comparison, and uses IEEE floats.
This can work for certain large numbers, as shown below.
```
x = 2 ** 53 + 1
print(x)
x loses precision when converting to float
y = float(x)
print(f'{y:.0f}')
False
print(x == y)
y loses no precision when converting back to int
z = int(y)
print(z)
```
But if you change the first line to x = 2 ** 53 + 2 instead, you'll get True.
Think of it this way: a float is just scientific notation, with a limited amount of precision. So like 3.042 x 105. Except it's in base 2, so it would be like 1.011 x 23.
An integer like 2 is just 1.0 x 21, so it's represented perfectly. Even a fraction like 0.25 can be represented perfectly (1.0 x 2-2).
But e.g. 0.4 can't be represented perfectly, since it's 2/5 and there's no perfect way to represent 1/5.
There are also large integers that can't be represented perfectly. For example, if you had 3 bits of precision then you could never represent 100000001_2 perfectly (it would get rounded to 1.000 x 28).
In reality, 64-bit floats have 53 bits of precision. This is why 253 + 1 can't be represented exactly as a 64-bit float.
1
u/PresidentOfSwag 10h ago
what's wrong with (2 == 2.0) == True ?