TLDR: try Fraction
half = Fraction(1, 2)
for x in range(1, 1075):
half = half / 2
print(half)
will give
1/4
1/8
...
1/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784
1/404804506614621236704990693437834614099113299528284236713802716054860679135990693783920767402874248990374155728633623822779617474771586953734026799881477019843034848553132722728933815484186432682479535356945490137124014966849385397236206711298319112681620113024717539104666829230461005064372655017292012526615415482186989568
You can find out the smallest available positive float
with
>>> import sys
>>> sys.float_info.min
2.2250738585072014e-308
then following your example we will find that
>>> 2 * sys.float_info.min > pow(2, -1074)
True
i.e. next division by 2 seems to be less than smallest available positive float
.
Btw their difference is equal to
>>> diff = 2 * sys.float_info.min - pow(2, -1074)
>>> diff
4.4501477170144023e-308
but it is interesting that
>>> diff == 2 * sys.float_info.min
False
while
>>> diff / 2 == sys.float_info.min
True
P. S.: dividing Fraction
objects by float
s will give us float
>>> half = Fraction(1, 2)
>>> half = half / 2.0
>>> type(half)
<class 'float'>
so your code with dividing by 2.0
will give the same result and for correct working with Fraction
s you should add/subtract/divide/multiply it with int
s or other Fraction
s like
half = Fraction(1, 2)
for x in range(1, 1075):
half = half / Fraction(2.0)
print(half)
P. P. S.: there is a convention about using underscore as a name for unused object, so it will be better to write
half = Fraction(1, 2)
for _ in range(1, 1075):
half = half / 2
print(half)