Someone reverse-sorted by 1/i
instead of the usual -i
and it made me wonder: What is the smallest positive integer case where that fails*? I think it must be where two consecutive integers i
and i+1
have the same reciprocal float
. The smallest I found is i = 6369051721119404
:
i = 6369051721119404
print(1/i == 1/(i+1))
import math
print(math.log2(i))
Output (Try it online!):
True
52.50000001100726
Note that it's only a bit larger than 252.5 (which I only noticed after finding the number with other ways) and 52 is the mantissa bits stored by float
, so maybe that is meaningful.
So: What is the smallest where it fails? Is it the one I found? And is there a meaningful explanation for why?
* Meaning it fails to sort correctly, for example sorted([6369051721119404, 6369051721119405], key=lambda i: 1/i)
doesn't reverse that list, as both key values are the same.
1/i
like that. I was just curious and wanted to learn (and share) something about floats. Then again, I might use the gained knowledge elsewhere. Not being applicable everywhere doesn't mean it's not useful anywhere. – Rewire