(& what can a computer provide?) This article is about writing and manipulating numbers that need ( many) more figures than normally provided.
Liberty BASIC inherits from its Smalltalk parent an ability to calculate particularly big integers. Just make sure that at no stage will a value be non-integer!
There are few occasions where such big integers are needed. Scientific calculations in the real world are normally adeguate with say ten digits and a power of ten. But huge integers have significant value in encryption.
My first example shows 2^n up to the 200th value, then divides back down. I haven't bothered to right-align.
twos =1 for i =1 to 200 print twos twos =twos *2 next i for i =1 to 200 twos =twos /2 print twos next i end...which results in..
1 2 4 8 16 32 ..... etc 6277101735386680763835789423207666416102355444464034512896 12554203470773361527671578846415332832204710888928069025792 25108406941546723055343157692830665664409421777856138051584 50216813883093446110686315385661331328818843555712276103168 100433627766186892221372630771322662657637687111424552206336 200867255532373784442745261542645325315275374222849104412672 401734511064747568885490523085290650630550748445698208825344 803469022129495137770981046170581301261101496891396417650688 803469022129495137770981046170581301261101496891396417650688 401734511064747568885490523085290650630550748445698208825344 200867255532373784442745261542645325315275374222849104412672 100433627766186892221372630771322662657637687111424552206336 ..... etc 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
In the second example I multiply a 12 digit prime number by itself 10 times, then divide back to show there was no error.
Code:
bigPrime1 = 32416190071' 373587509' 32452591 '15280697 bigPrime2 = 32416187893' 573259073' 86027819 '15279769 bigPrime3 = 32416189063 n =1 for i =0 to 10 n =n *bigPrime3 print n next i for i =1 to 10 n =n /bigPrime3 print n next i endProduces
32416189063 1050809313368160817969 34063233371303514399871831673047 1104200213061265601542588278481603153284961 35793962869998867741662966081627342384222265290581543 1160303867707885387309678550527388221437182139873633931106264209 37612629546148953170965520823651614945707005824110628439592577342574146167 1219258110524544349549551685273754232125314780998012255325577661828333641245268771521 39523701427359779736926626316724290100370592249220005095824954105516741564849847251874486074823 1281207777938857580874249921241685906738072364715998099468085144237728745877883123723434321747423132260849 41531873558652127795850498275283350069683939394809284693106127770191840044087341248835867497427640788427335816894487 1281207777938857580874249921241685906738072364715998099468085144237728745877883123723434321747423132260849 39523701427359779736926626316724290100370592249220005095824954105516741564849847251874486074823 1219258110524544349549551685273754232125314780998012255325577661828333641245268771521 37612629546148953170965520823651614945707005824110628439592577342574146167 1160303867707885387309678550527388221437182139873633931106264209 35793962869998867741662966081627342384222265290581543 1104200213061265601542588278481603153284961 34063233371303514399871831673047 1050809313368160817969 32416189063
You can also do Fibonacci numbers very easily- they increase even quicker...
oneBack =1 print oneBack fib =2 for i =1 to 300 print fib temp =fib fib =fib +oneBack oneBack =temp next i end
..resulting in...
1 2 3 5 8 13 21 34 55 89 144 .... etc ... 52461916524905785334311649958648296484733611329035169538240802 84885164052257330097714121751630835360966663883732297726369399 137347080577163115432025771710279131845700275212767467264610201 222232244629420445529739893461909967206666939096499764990979600 359579325206583560961765665172189099052367214309267232255589801 581811569836004006491505558634099066259034153405766997246569401
Where real problems come in is with fractions leading to multi-digit decimals. And using LB's 'using(' command is not a huge help and may muddy the waters.
n =1 for i =1 to 40 print n; tab( 24); using( "#.############################", n) n =n /2 next i for i =1 to 41 print n; tab( 24); using( "#.############################", n) n =n *2 next i end
.. results in ...
1 0.9999999999999999583119736832 0.5 0.4999999999999999791559868416 0.25 0.2499999999999999895779934208 0.125 0.1249999999999999947889967104 0.0625 0.0624999999999999973944983552 0.03125 0.0312499999999999986972491776 0.015625 0.0156249999999999993486245888 0.0078125 0.0078124999999999996743122944 0.390625e-2 0.0039062499999999998371561472 0.1953125e-2 0.0019531249999999999185780736 0.9765625e-3 0.0009765624999999999592890368 0.48828125e-3 0.0004882812499999999796445184 0.24414063e-3 0.0002441406249999999898222592 0.12207031e-3 0.0001220703124999999949111296 0.61035156e-4 0.0000610351562499999974555648 0.30517578e-4 0.0000305175781249999987277824 0.15258789e-4 0.0000152587890624999993638912 0.76293945e-5 0.0000076293945312499996819456 0.38146973e-5 0.0000038146972656249998409728 0.19073486e-5 0.0000019073486328124999204864 0.95367432e-6 0.0000009536743164062499602432 0.47683716e-6 0.0000004768371582031249801216 0.23841858e-6 0.0000002384185791015624900608 0.11920929e-6 0.0000001192092895507812450304 0.59604645e-7 0.0000000596046447753906225152 0.29802322e-7 0.0000000298023223876953112576 0.14901161e-7 0.0000000149011611938476556288 0.74505806e-8 0.0000000074505805969238278144 0.37252903e-8 0.0000000037252902984619139072 0.18626451e-8 0.0000000018626451492309569536 0.93132257e-9 0.0000000009313225746154784768 0.46566129e-9 0.0000000004656612873077392384 0.23283064e-9 0.0000000002328306436538696192 0.11641532e-9 0.0000000001164153218269348096 0.58207661e-10 0.0000000000582076609134674048 0.2910383e-10 0.0000000000291038304567337024 0.14551915e-10 0.0000000000145519152283668512 0.72759576e-11 0.0000000000072759576141834256 0.36379788e-11 0.0000000000036379788070917128 0.18189894e-11 0.0000000000018189894035458564 0.9094947e-12 0.0000000000009094947017729282 0.18189894e-11 0.0000000000018189894035458564 0.36379788e-11 0.0000000000036379788070917128 0.72759576e-11 0.0000000000072759576141834256 0.14551915e-10 0.0000000000145519152283668512 0.2910383e-10 0.0000000000291038304567337024 0.58207661e-10 0.0000000000582076609134674048 0.11641532e-9 0.0000000001164153218269348096 0.23283064e-9 0.0000000002328306436538696192 0.46566129e-9 0.0000000004656612873077392384 0.93132257e-9 0.0000000009313225746154784768 0.18626451e-8 0.0000000018626451492309569536 0.37252903e-8 0.0000000037252902984619139072 0.74505806e-8 0.0000000074505805969238278144 0.14901161e-7 0.0000000149011611938476556288 0.29802322e-7 0.0000000298023223876953112576 0.59604645e-7 0.0000000596046447753906225152 0.11920929e-6 0.0000001192092895507812450304 0.23841858e-6 0.0000002384185791015624900608 0.47683716e-6 0.0000004768371582031249801216 0.95367432e-6 0.0000009536743164062499602432 0.19073486e-5 0.0000019073486328124999204864 0.38146973e-5 0.0000038146972656249998409728 0.76293945e-5 0.0000076293945312499996819456 0.15258789e-4 0.0000152587890624999993638912 0.30517578e-4 0.0000305175781249999987277824 0.61035156e-4 0.0000610351562499999974555648 0.12207031e-3 0.0001220703124999999949111296 0.24414063e-3 0.0002441406249999999898222592 0.48828125e-3 0.0004882812499999999796445184 0.9765625e-3 0.0009765624999999999592890368 0.1953125e-2 0.0019531249999999999185780736 0.390625e-2 0.0039062499999999998371561472 0.0078125 0.0078124999999999996743122944 0.015625 0.0156249999999999993486245888 0.03125 0.0312499999999999986972491776 0.0625 0.0624999999999999973944983552 0.125 0.1249999999999999947889967104 0.25 0.2499999999999999895779934208 0.5 0.4999999999999999791559868416 1 0.9999999999999999583119736832
Basically, to progress reliably to many-digit figures which are accurate exactly requires us to write our own routines. This is most easily done by representing the numbers in a string of length more than any result or intermediate calculation may use. Now we can for example demonstrate which fractions are exact and which form an infinite repeating sequence.
current$ ="0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" divisor$ ="3" L =len( divisor$) d =val( divisor$) 'current$ ="00000000" +current$ print "1/3^"; i, current$ Lc =len( current$) window =W +1 for i =1 to 15 carry =0 result$ ="" print "1/3^"; i, for j =1 to Lc -L +1 nextDigit =val( mid$( current$, j, window)) +10 *carry timesItGoesIntoIt =int( nextDigit /d) carry =nextDigit -d *timesItGoesIntoIt result$ =result$ +str$( timesItGoesIntoIt) print str$( timesItGoesIntoIt); CallDLL #kernel32, "Sleep", 10 As ulong, ret As void scan next j 'print result$ print current$ =result$ next i end
1/3^0 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1/3^1 0033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 1/3^2 0011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1/3^3 0003703703703703703703703703703703703703703703703703703703703703703703703703703703703703703 1/3^4 0001234567901234567901234567901234567901234567901234567901234567901234567901234567901234567 1/3^5 0000411522633744855967078189300411522633744855967078189300411522633744855967078189300411522 1/3^6 0000137174211248285322359396433470507544581618655692729766803840877914951989026063100137174 1/3^7 0000045724737082761774119798811156835848193872885230909922267946959304983996342021033379058 1/3^8 0000015241579027587258039932937052278616064624295076969974089315653101661332114007011126352 1/3^9 0000005080526342529086013310979017426205354874765025656658029771884367220444038002337042117 1/3^10 0000001693508780843028671103659672475401784958255008552219343257294789073481346000779014039 1/3^11 0000000564502926947676223701219890825133928319418336184073114419098263024493782000259671346 1/3^12 0000000188167642315892074567073296941711309439806112061357704806366087674831260666753223782 1/3^13 0000000062722547438630691522357765647237103146602037353785901602122029224943753555584407927 1/3^14 0000000020907515812876897174119255215745701048867345784595300534040676408314584518528135975 1/3^15 0000000006969171937625632391373085071915233682955781928198433511346892136104861506176045325
Here are the first 100 decimal expansions of 1/n.
for i =1 to 100 current$ ="0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" divisor$ =str$( i) L =len( divisor$) d =val( divisor$) Lc =len( current$) window =W +1 carry =0 result$ ="" print using( "###", i), for j =1 to Lc -L +1 nextDigit =val( mid$( current$, j, window)) +10 *carry timesItGoesIntoIt =int( nextDigit /d) carry =nextDigit -d *timesItGoesIntoIt result$ =result$ +str$( timesItGoesIntoIt) print str$( timesItGoesIntoIt); if j =2 then print "."; 'CallDLL #kernel32, "Sleep", 10 As ulong, ret As void scan next j 'print result$ print current$ =result$ next i end
1 01.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 2 00.50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 3 00.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 4 00.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 5 00.20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 6 00.16666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 7 00.14285714285714285714285714285714285714285714285714285714285714285714285714285714285714285 8 00.12500000000000000000000000000000000000000000000000000000000000000000000000000000000000000 9 00.11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 10 00.1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 11 00.0909090909090909090909090909090909090909090909090909090909090909090909090909090909090909 12 00.0833333333333333333333333333333333333333333333333333333333333333333333333333333333333333 13 00.0769230769230769230769230769230769230769230769230769230769230769230769230769230769230769 14 00.0714285714285714285714285714285714285714285714285714285714285714285714285714285714285714 15 00.0666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 16 00.0625000000000000000000000000000000000000000000000000000000000000000000000000000000000000 17 00.0588235294117647058823529411764705882352941176470588235294117647058823529411764705882352 18 00.0555555555555555555555555555555555555555555555555555555555555555555555555555555555555555 19 00.0526315789473684210526315789473684210526315789473684210526315789473684210526315789473684 20 00.0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000 21 00.0476190476190476190476190476190476190476190476190476190476190476190476190476190476190476 22 00.0454545454545454545454545454545454545454545454545454545454545454545454545454545454545454 23 00.0434782608695652173913043478260869565217391304347826086956521739130434782608695652173913 24 00.0416666666666666666666666666666666666666666666666666666666666666666666666666666666666666 25 00.0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000 26 00.0384615384615384615384615384615384615384615384615384615384615384615384615384615384615384 27 00.0370370370370370370370370370370370370370370370370370370370370370370370370370370370370370 28 00.0357142857142857142857142857142857142857142857142857142857142857142857142857142857142857 29 00.0344827586206896551724137931034482758620689655172413793103448275862068965517241379310344 30 00.0333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 31 00.0322580645161290322580645161290322580645161290322580645161290322580645161290322580645161 32 00.0312500000000000000000000000000000000000000000000000000000000000000000000000000000000000 33 00.0303030303030303030303030303030303030303030303030303030303030303030303030303030303030303 34 00.0294117647058823529411764705882352941176470588235294117647058823529411764705882352941176 35 00.0285714285714285714285714285714285714285714285714285714285714285714285714285714285714285 36 00.0277777777777777777777777777777777777777777777777777777777777777777777777777777777777777 37 00.0270270270270270270270270270270270270270270270270270270270270270270270270270270270270270 38 00.0263157894736842105263157894736842105263157894736842105263157894736842105263157894736842 39 00.0256410256410256410256410256410256410256410256410256410256410256410256410256410256410256 40 00.0250000000000000000000000000000000000000000000000000000000000000000000000000000000000000 41 00.0243902439024390243902439024390243902439024390243902439024390243902439024390243902439024 42 00.0238095238095238095238095238095238095238095238095238095238095238095238095238095238095238 43 00.0232558139534883720930232558139534883720930232558139534883720930232558139534883720930232 44 00.0227272727272727272727272727272727272727272727272727272727272727272727272727272727272727 45 00.0222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 46 00.0217391304347826086956521739130434782608695652173913043478260869565217391304347826086956 47 00.0212765957446808510638297872340425531914893617021276595744680851063829787234042553191489 48 00.0208333333333333333333333333333333333333333333333333333333333333333333333333333333333333 49 00.0204081632653061224489795918367346938775510204081632653061224489795918367346938775510204 50 00.0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000 51 00.0196078431372549019607843137254901960784313725490196078431372549019607843137254901960784 52 00.0192307692307692307692307692307692307692307692307692307692307692307692307692307692307692 53 00.0188679245283018867924528301886792452830188679245283018867924528301886792452830188679245 54 00.0185185185185185185185185185185185185185185185185185185185185185185185185185185185185185 55 00.0181818181818181818181818181818181818181818181818181818181818181818181818181818181818181 56 00.0178571428571428571428571428571428571428571428571428571428571428571428571428571428571428 57 00.0175438596491228070175438596491228070175438596491228070175438596491228070175438596491228 58 00.0172413793103448275862068965517241379310344827586206896551724137931034482758620689655172 59 00.0169491525423728813559322033898305084745762711864406779661016949152542372881355932203389 60 00.0166666666666666666666666666666666666666666666666666666666666666666666666666666666666666 61 00.0163934426229508196721311475409836065573770491803278688524590163934426229508196721311475 62 00.0161290322580645161290322580645161290322580645161290322580645161290322580645161290322580 63 00.0158730158730158730158730158730158730158730158730158730158730158730158730158730158730158 64 00.0156250000000000000000000000000000000000000000000000000000000000000000000000000000000000 65 00.0153846153846153846153846153846153846153846153846153846153846153846153846153846153846153 66 00.0151515151515151515151515151515151515151515151515151515151515151515151515151515151515151 67 00.0149253731343283582089552238805970149253731343283582089552238805970149253731343283582089 68 00.0147058823529411764705882352941176470588235294117647058823529411764705882352941176470588 69 00.0144927536231884057971014492753623188405797101449275362318840579710144927536231884057971 70 00.0142857142857142857142857142857142857142857142857142857142857142857142857142857142857142 71 00.0140845070422535211267605633802816901408450704225352112676056338028169014084507042253521 72 00.0138888888888888888888888888888888888888888888888888888888888888888888888888888888888888 73 00.0136986301369863013698630136986301369863013698630136986301369863013698630136986301369863 74 00.0135135135135135135135135135135135135135135135135135135135135135135135135135135135135135 75 00.0133333333333333333333333333333333333333333333333333333333333333333333333333333333333333 76 00.0131578947368421052631578947368421052631578947368421052631578947368421052631578947368421 77 00.0129870129870129870129870129870129870129870129870129870129870129870129870129870129870129 78 00.0128205128205128205128205128205128205128205128205128205128205128205128205128205128205128 79 00.0126582278481012658227848101265822784810126582278481012658227848101265822784810126582278 80 00.0125000000000000000000000000000000000000000000000000000000000000000000000000000000000000 81 00.0123456790123456790123456790123456790123456790123456790123456790123456790123456790123456 82 00.0121951219512195121951219512195121951219512195121951219512195121951219512195121951219512 83 00.0120481927710843373493975903614457831325301204819277108433734939759036144578313253012048 84 00.0119047619047619047619047619047619047619047619047619047619047619047619047619047619047619 85 00.0117647058823529411764705882352941176470588235294117647058823529411764705882352941176470 86 00.0116279069767441860465116279069767441860465116279069767441860465116279069767441860465116 87 00.0114942528735632183908045977011494252873563218390804597701149425287356321839080459770114 88 00.0113636363636363636363636363636363636363636363636363636363636363636363636363636363636363 89 00.0112359550561797752808988764044943820224719101123595505617977528089887640449438202247191 90 00.0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 91 00.0109890109890109890109890109890109890109890109890109890109890109890109890109890109890109 92 00.0108695652173913043478260869565217391304347826086956521739130434782608695652173913043478 93 00.0107526881720430107526881720430107526881720430107526881720430107526881720430107526881720 94 00.0106382978723404255319148936170212765957446808510638297872340425531914893617021276595744 95 00.0105263157894736842105263157894736842105263157894736842105263157894736842105263157894736 96 00.0104166666666666666666666666666666666666666666666666666666666666666666666666666666666666 97 00.0103092783505154639175257731958762886597938144329896907216494845360824742268041237113402 98 00.0102040816326530612244897959183673469387755102040816326530612244897959183673469387755102 99 00.0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 100 00.0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000It's an easy exercise to check ( say) that the expansion of 1/7 and of 6/7 correctly add to 1.000000000000..
And we can calculate pi or e to any desired precision. But it may become very slow!
See on my other pages or forum replies...