How to do computation in the real numbers?

In [ ]:
Rationals are dense in the reals. Could we do rational arithmetic?
In [2]:
from fractions import Fraction
In [4]:
Fraction(21,14)
Out[4]:
Fraction(3, 2)
In [5]:
p = Fraction(2,3)
q = Fraction(4,7)
p*q
Out[5]:
Fraction(8, 21)
In [10]:
b = Fraction(35,10)
x = Fraction(3,10)
print(0,x)
for t in range(10):
    x = b*(1-x)*x
    print(t+1,x)
0 3/10
1 147/200
2 54537/80000
3 9720729417/12800000000
4 209529292973495880777/327680000000000000000
5 173292238753074419559494832395491013133897/214748364800000000000000000000000000000000
6 50288174248909624397705320059288419949596168892333204861273072428009974527247153737/92233720368547758080000000000000000000000000000000000000000000000000000000000000000
7 14765554525610261894123745097387916794159794191506727331825951587445263245401153259807821923508493359449133888563536051046342869268611831784917282409852401626008543817/17014118346046923173168730371588410572800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
8 232409041864804309685689400935111619572629974960340953424988709113614615717653028451648725776637159128551878618743662472532396678529826032379476067700915745124403894813396176260109605022160968382521206224871500982692202149102088794947632946435237342684428503871067857196334321220307369302743257127583089105288522036956838834337493577/578960446186580977117854925043439539266349923328202820197287920039565648199680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9 563791758847285630754300819554273941560143964024997085332748901840665343295734558278710188067961110515573508885322725266918997455778809136409142129040991395241383400914362571197996980831937507122529479634800559297055745641624942079557252837494724837684489439268643916796351657865243518632977389950741252772518382028734474784156792745632872706637846962902884320003395643159908234385895971633375245150800013684258381355823323005333031842876740341716245350924760433463738597752848991646390464502228039414934441977676189021316667317174599092004134188639779276262756012118141333180830921515379064017128537830768044441338496627821016206507830914104186849414317546386215497/670390396497129854978701249910292306373968291029619668886178072186088201503677348840093714908345171384501592909324302542687694140597328497321682450304204800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10 420696033879311102782228294549985842766029760194716891108874203637103328681217777573232847929276571576951370445337479756580569209001104714190874778848427723469082157491197728258237123932529652161989900571082112533300625821818564965568183346697183028302902664754468892271645326284133545882077138479476271734103000993477683222171020144235616834482501625228583216685414482242332727546083410759815286688927983042622717993647791472432353440884794770617959239687001258886982941496940740610461486236664163621867722421836672726629313620584780397770537161661158054154497285562291857422754384468308238215068133657799703408595946534899196176502265183474929577935268858044558472862336563615505745831513372979149563084819647670255580292535576560928727114924923814937299653865163562794294303967267946933167487536873589093968456235227564279099861132382225965892839456654952562432724150102759907456069586566083973676052787827437207642491823246271949374029376028122607972526949800793736356369646292438864278141202919744405803656922210871624377890217543938972546090665062583030227137195901152526558421716413435832562036649920537671243485889427480393382376144424507705001904043955622024052956513821421158210711326258969378763478545161318153062209709000610559153866857905266210754404851806857703321815914404483317908996271367951957847736918061139300937/898846567431157953864652595394512366808988489471153286367150405788663379027504815663542386612037680105600569399356966788293948844072083112464237153197370621888839467124327426381511098006230470597265414760425028844190753411712314407369565552704136185816752553422931491199736229692398581524176781648121120686080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

This is exact! But will consume all our computer memory in just a few more iterations!

Alternative: allocate a fixed amount of memory, such as 64 bits, to represent real numbers.

In [12]:
2**64*1.0
Out[12]:
1.8446744073709552e+19
In [17]:
x = 2**-7
(1+x)-1==x
Out[17]:
True
In [16]:
x = 2**-100
(1+x)-1==x
Out[16]:
False
In [19]:
x=2**-52
(1+x)-1==x
Out[19]:
True
In [20]:
x=2**-53
(1+x)-1==x
Out[20]:
False
In [21]:
2**11-1 -1023
Out[21]:
1024
In [22]:
1.*2**1025
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-22-f82f0ca347f9> in <module>()
----> 1 1.*2**1025

OverflowError: int too large to convert to float