Tutor 41 Big Numbers

10
maXbox Starter 41 Deal with Big Numbers 1.1 A Big Decimal or Big Int Interface Today we step through numbers and infinity. As you may know there's no simple solution to print, calculate or store big numbers or decimals, for example you want to compute 400000078669 / 2000123 your calculator shows (so does my Casio FX-880P): 199987.7401 So this is not the end of the line, a second test is maxcalcF('400000078669 / 2000123' ) and we get: 199987.740088485 And there are even more numbers that need to compute so we switch to http://www.wolframalpha.com to get the real precision thing or at least an approximation: 199987 .7400884845581996707202507045816682274040146530988344 21683066491410778237138415987416773868407092963782727362267 220... http://www.wolframalpha.com/input/?i=400000078669%2F2000123 again as you suppose the numbers go on. Use "Power Towers" to write them down. The decimal point is the most important part of a decimal number like above. Without it, we would be lost ... and not know what each position meant. Dividing decimals is almost the same as dividing whole numbers, except you use the position of the decimal point in the dividend to determine the decimal places in the result. Our division is always an approximation.

Transcript of Tutor 41 Big Numbers

Page 1: Tutor 41 Big Numbers

maXbox Starter 41

Deal with Big Numbers 1.1 A Big Decimal or Big Int Interface

Today we step through numbers and infinity.

As you may know there's no simple solution to print, calculate or store big numbers or decimals, for example you want to compute 400000078669 / 2000123 your calculator shows (so does my Casio FX-880P):

199987.7401

So this is not the end of the line, a second test is

maxcalcF('400000078669 / 2000123')

and we get: 199987.740088485

And there are even more numbers that need to compute so we switch to http://www.wolframalpha.com to get the real precision thing or at least an approximation:

199987.740088484558199670720250704581668227404014653098834421683066491410778237138415987416773868407092963782727362267220...

http://www.wolframalpha.com/input/?i=400000078669%2F2000123

again as you suppose the numbers go on.

Use "Power Towers" to write them down. The decimal point is the most important part of a decimal number like above. Without it, we would be lost ... and not know what each position meant.Dividing decimals is almost the same as dividing whole numbers, except you use the position of the decimal point in the dividend to determine the decimal places in the result. Our division is always an approximation.

Page 2: Tutor 41 Big Numbers

Approximate means you're going to round the number. Because you're not actually giving the exact number, all those numbers after the decimal, the rounded number is called an approximation:

199987.7401 is roundToPrec4 of: 199987.740088485

Although, you probably wondered how they get those nice and fancy graphical user interfaces (GUI) for large numbers, here in maXbox we do also have one:

maXbox3 568_U_BigFloatTestscript2.pas Compiled done: 6/18/2015

The idea that you are approximating is that, as you are only taking the first 50 decimal places as you can see at the screen-shot.The same like wolfram goes like this:

199987.7400884845581996707202507045816682274040146530988344216830664914107782371384159874167738684070929637827273622672205

When we try to write this decimal number (or the well known PI or SQR(2)) in decimal notation, we get an endless stream of digits. 3.141592653589723.....and so on forever.But suppose instead, we use fractional notation. Then we can write each part as a precise (irreducible)

400000078669 2000123

A fraction is an exact ratio of 2 numbers, and if those 2 numbers are integers, or at least rational numbers, then the fraction can more

2

Page 3: Tutor 41 Big Numbers

appropriately be called a rational number. An irrational number can be re-presented as an approximation to a rational number to an extremely high degree of accuracy.It's quite clear that there are fractions which can't be expressed in finite decimal form! Now, here's the big problem. Not every number is rational! For example there is no fraction for sqrt(2). That is, no matter what whole numbers m and n you pick, m/n is not the square root of 2. Euclid wrote down a real AND beautiful proof of this fact around 2300 years ago.

Interesting point about those real numbers is also the possibility to divide the number to his prime factorization:

29×37×127^(-1)×179×15749^(-1)×2082607

maxcalcF('29*37*(127^-1)*179*(15749^-1)*2082607');

>> 199987.740088485

1.2 Real Big Integer

So what about big integers? For example you want to compute fact(70), your calculator shows:

fact(70) = 1.19785716699699e+100 or maxcalcF('70!')

1.19785716699699E100 or even more 1.1978571669969891796072783721689098736458938142546425857... × 10^100

but the maximum range on Pascal, C or Delphi depends on your operating system types, means nowadays an int64 range is big.Now that the "signed" words are finally up-to-par with the unsigned integer types, languages introduce a new 64-bits integer type, called Int64, with a whopping range of -2^63..2^63 - 1

Another way is to use a type extended, but the limitation is precision like Writeln(FloatToStr(Fact(70))) it only shows 1.2E+0100 or 1.19785716699698966E100

With a BigInt Library you'll see the full range of Fact(70):

11978571669969891796072783721987892755536628009582789845319680000000000000000

3

Page 4: Tutor 41 Big Numbers

All examples can be found online:

maxbox3\examples\161_bigint_class_maxprove2.txt

http://www.softwareschule.ch/examples/161_bigint_class_maxprove2.txt The call respectively the calculation goes like this:

function GetBigIntFact(aval: byte): string;//call of unit mybigintvar mbRes: TMyBigInt; i: integer;begin mbRes:= TMyBigInt.Create(1); try //multiplication of factor for i:= 1 to aval do mbRes.Multiply1(mbres, i); Result:= mbRes.ToString; finally //FreeAndNil(mbResult); mbRes.Free; end;end;

Or you want the power of 100 like 2^100=12676506002282299670376

function BigPow(aone, atwo: integer): string;var tbig1, tbig2: TInteger;begin tbig1:= TInteger.create(aone); //tbig2:= TInteger.create(10); try tbig1.pow(atwo); finally result:= tbig1.toString(false); tbig1.Free; end;end;

At least one really big, it's 333^4096 (10332 decimal digits)!:

854241058957708873229669659179145847101381613862221471829176778104953605790662731836109375886562057769732224078736953981504332246815140327668478948527046875787550310970504170251

4

Page 5: Tutor 41 Big Numbers

821591231581498325226325065580968884657499008596697140280557172226167217308161576577272999991338993244872051682800307067049948583875478861163177472370389199969581395362347697256709608954628721602617326909732389848441307614286016432328169689953017448874193347965124914155462139668410462061620490808738502674650541288448161671070156327238251274328179200400938909967610808353533943357255247645038720610919916521094491939222603486211588501821307572510489932009482542997278583308567702799428942416066175248810727171285940075837283674910402380201480381659625620772921132430734362143162169781303353387039665127989380130653350705518247598269004201863795492471837910714543445430478178723995293717173465603902499439767523905124390140992318722286742627742553724933304310551768256663623236667348978485122312124646521581591650262271475650341470134146173462494457636856525872971987877831270158832603817205011322637143455691543980399640385473278656620269935582978504559525241368284063931428407486751810074659865765594581378019251453464202570850546505546651918600042626257608188495357696662970914537002071987397488841496932636449655414296062594227294328185513065865963741411776395461829300973711920497440723531587803915956018581696810199146742826127160616966751837617406503248776028183783176777304319714708292420371955984142794539461134475921078967271031215128862677919899538701143923451064706611017647623102481237637914918894554224081908148706733079384730490856632162564079943686776852710875904169096530226927277928918170989861461840081770181767033389755570766416722785651840954388568904657279312763515446538147271172404512124048912601491080221083527568260609161657527584296850614033467693466756882440760122883574394830113385667291848920157649283141024291690280652494545083790886477617437201261672363791244811278939943162583101077167606827813531305220592767150289092947771778545830155927642694762695926174086054203450601228998156862816770206156286995619547713403660633247121326959440684119059465666117348540308246233404699236038721015410211339321947567494315738883988756771563661334205014438776231018691956945896654929451766257449857738007274440655447508422629826068641223831982139338914667951341208096117222835285791869329307778284481089063247808647210007688704100771174997144137624681757514590807817999921433168370141531954597999236787298196551053465150984914180373132148018799414415444455044281953130393681832326917781405396904074651701879257716143646994952012988801927312646343698172158367619125266405743236218798380598988906275206059369595516524059080964763541308905836730081722096035802162537621961847418469992177098710584107130852431474071403333632860325415187194481622747042146373819129291686588380723724399574705698615985167991134168704323669439794810344906665271401825738292033812042010509830956293769066454597716029454679602570172933558474263103765432164362576556935646889381684773746399169718240272813551427192819977152410473948457379706283494005558157767229410238117423497658874466085698219698093781073032946381

5

Page 6: Tutor 41 Big Numbers

540939238873806623749979466329961749731552194129771436895369362634665943324593755070981832026902837644275967154831115029681760031234141791574578094471517422412689480662593361694925044358745991553109154338684664323504890661111078168849096183533906652492284214250872596310102602623629067593019673173552799026653846151496995266112396254547674003723688793668704309699347832203575701658715961126175190811777162883587751169275603360684040903495715352037020302594420664116357686392424070432686477410458637592771813119302505708045545791331742276746360892018294457494666303405104651705379231254323285783974012104801316168191543049181565684513542746806175639004934440540270331262335589548473966818981111982651233800107487474050089438374727326190752805099371881306448674014338615768101003402720339327689508575131150689411103328465726226558155966132471197425573248051246364064100968284789300972872583882094924256486662450602192578907537146892188317972157166039078873449387826792348458310888297734672440988509295143878217708481814130975623645815400519531072329972316424125029833113386765480075734750774942643780397764246977195723069308876669566715845160413451640065636438187271587300839572325797940380735398040659721222058817651087721809880276579722510397085266588471677141291205316208053131495644832183722814195336824762439922372460121457009186575901480266273785801401479321896896482250699296283841144875763989136349504660143195130338161413074500570137239376048155340458381611793749654026926203579050148844316192614013675544602493659244307552086673181833019464091484685855011484242655933958222567738947458128440348482597685901203260041033998951461001481719027111957868383902667961653069863558898288546493088737364714796413990901429121521451261576282787526544172701709313410194780946909994023192295901873607335441919945948697077143318739754495217885648673036025085521837662931543386875103066246404679792937093469772528468011757947147166512152435404133191490416623793714104082289022250873576478083376909480039832107456114568704528702199755775161876574276395255344328737473416453145852893299326010312016761391559829086609861332167705597911052156138508267594635380262856149038385072508978557058387244965990390431181424237305274720552318264583338030192793368941333002995026564410490856532115413083850985103857380722832076707674281875798159037557774748087886920798274424308686690620187628466929280303992057757511176950785280680754799301722047807967084300522377435289623231007430240810484881231627209368416944325930946166380770220744336671717609098941208642985184305431723543478184029599272537932588104657005848579910932410525323549488828231695666943591839576583179961346342083408283976952468168656258259635365316357662957485138281426960328695405673338889939941371726817180782654630434720733935668869528756280020854585739047030891773673851859163735002519577609596201647133908022151789341271684456392284830620493066658068310972153862640162099772385881149819131250406346400767577318215869704292807511908855522623708661507760800651

6

Page 7: Tutor 41 Big Numbers

399346625297349658355233085415407685551979456108012149957805841856205301031559316744640647433887909048853644902210097271541065224624804293126599606409925169452156973395716618555051357492972926702377282871619789309962593817586463428884085313893310205449027048555644424990928849076038093323601634650142874212409602811687624242708597540960631386528199815652003661754723586020099783070404043274466770731300886267315652603560218650814739074905576625797116940214772316477936465623774315016467408466796245416985801276491432859164349833233385653617206982754862257593136808209043237255816575538571607194871287667806443881531972112539415728877525040490538290131435147127270914397227595612135035965523502394602395309928227898836997151138312975622564587044380734655560349197948596199244945597624322148175141116714940670892597838230797910056339886390867186697957387176618151963711927305352973712059095788894787935465478471506111827232646637163585859688844633921753960816861163141017632815577174846178615950234635176478547915862327401088544366654385988353407028200194454544617737505799549724370895920181097359442341251242543979000903125835184684426468750140536859807362836590359345424802711039139637950473220893400253606651628956090374354970460018382063686801462543831016660874997872505399256375176785192438743443720687292332390575044417801569869328311130430921356160058121842594220886557662181357368897040409566589710865888292361644385255434723295731458223836965307242577359762320845197345060641016020970918836719391632539037054644824319584027884479727032498249886561442752963805497655968589209771652324967306386540801593851663037671642155456508538273418699746152704956819399889509407101079325920970358872447117212328227188990543398458779845544281195301301588568445638914577378951818883394785465654691890855803790906572944436175573874925535098407381526404265490476610472438047036240659794280203256026984922608535332381874930646173980783677802048083531252098020595799395388574198071178454689818720858015933545940139775830975231434411383992826427516596696077211927867138489792723270797935779061764289333203584632650570545507293878464549700849980591946842803418346941194412381859036790552215925925712869798303439836216739266184587349412028687993572233743961972113106969534296502917127984081513247598447824546253439145516689117478361912086503159974257017085327392537522415199256733343762956498636282185720333881619489853242326050171243361930507304665115114836754111295691342976069443591326910757894166838155494439354845073272909278695358492295884052614663863294640183194205514947070733097301981784639689236521891181001906710419439895219252131812180753640540416516363059279662484919240876401740746278142076989588092100262633812666597344962928550841011057279645023178030920694037888843030579517647568661540624474105389905643442347561421462366640130069035110764349034582759676832178036797908743185119903456948979188785564070806598994180155246720407629664699369323663506536501117701055868582355811954302737246702288220571315639590

7

Page 8: Tutor 41 Big Numbers

618220276400522042889084822540419668875849784809488048045316884287757536165252005715862959516088509980396269534028660365420928832906521186092765576521399427875156924799554483049604083420356649499586797702436340878281229155699413635792730811385565038650463288171194713297378726939726011825473175805568507298463372949182573598560708416955751131747136868195907162638576146685446186163674412919374691982701793657623162727424285161866977135146493619822663604072905160776010301532925112772102255453238309477101692745197761359929665709233515312958248793979913228375797202389825601891190630690270819212793589623447372281063991711197502976353524252985087691233430206123870409178015883291874877723120751841704266668129874426976931246615100351551067625058184727007368936946284394840868733993575255583583170 553599477220392272254238769289322008443509218185771014582951351138190675601738239721777689877921345489973222263465257489935099549137644474027777583956137051269588783021487059246175984898650758631831941867081533767859258964535212534978761355270159750801161152450384375483791394581214883412511380999171425821993170789973409296543662081

I'm trying to move a part of SysTools to Win64. There is a class TStDecimal which is a fixed-point value with a total of 38 significant digits. The class itself uses a lot of ASM code.

function BigDecimal(aone: float; atwo: integer): string;begin with TStDecimal.create do begin try //assignfromint(aone) assignfromfloat(aone) //2 RaiseToPower(atwo) //23 result:= asstring finally free end; end; end;

SysTools is hosted under Sourceforge:

http://www.sourceforge.net/projects/tpsystools

The class TStDecimal is defined in the unit StDecMth. It has the following description: StDecMth declares and implements TStDecimal. This is a fixed- point value with a total of 38 significant digits of which 16 are to the right of the decimal point.

1366556882568704.2292943165706246

8

Page 9: Tutor 41 Big Numbers

It is important to note that Infinity is not a real number, it is an idea. An idea of something without an end.

Infinity is not "getting larger", it is already fully formed. Sometimes students or people (including me) say it "goes on and on" which sounds like it is growing somehow. But infinity does not do anything, it just is.

Writeln('') Writeln('Big Lotto Combination 1600 of 5000!') Writeln('') Writeln(BinominalCoefficient(5000, 1600)); Writeln('') Writeln('Same Lotto Comb 6 of 45!') Writeln(BinominalCoefficient(45, 6)); -->8145060 Writeln('Same Lotto Comb 39 of 45!') Writeln(BinominalCoefficient(45, 39)); -->8145060

OK, 1/3 is a finite number (it is not infinite). But written as a decimal number the digit 3 repeats forever (we say "0.3 repeating"):

0.333333333333333333333... (etc.)

There's no reason why the 3s should ever stop: they repeat infinitely.Okay, I hope you're not one of those people who denies 0.999... = 1, because it sounds like you're saying 0.333... doesn't exactly equal 1/3. If there are only a finite amount of 3s, I wouldn't argue a bit that they're not equal, but with an infinite amount, they are.Test the script with F9 / F2 or press Compile.

Conclusion And we can easily create much larger numbers than those! But none of these numbers are even close to infinity. Because they are finite, and infinity is ... not finite!

“Wise men speak because they have something to say; Fools, because they have to say something”. -Plato Feedback @ [email protected]: Kleiner et al., Patterns konkret, 2003, Software & Support

http://www.softwareschule.ch/download/codesign_2015.pdf

http://www.softwareschule.ch/download/XXL_BigInt_Tutorial.pdf

http://www.mathsisfun.com/numbers/infinity.html

https://github.com/maxkleiner/maXbox3/releases

9

Page 10: Tutor 41 Big Numbers

1.3 Appendix Study with BigInt Direct

// TODO: Copy a file in a connected share path//this is 333^4096:

function GetBigIntDirect: string; //Unit mybigintvar mbResult: TMyBigInt; i: integer;begin mbResult:= TMyBigInt.Create(333); try // Faktoren im Zaehler aufmultiplizieren ---> 2^12=4096 for i:= 1 to 12 do begin mbResult.Multiply(mbresult, mbresult); //writeln(inttostr(i)+': '+mbresult.tostring); end; Result:= mbResult.ToString; finally //FreeAndNil(mbResult); mbResult.Free; end;end;

TMyBigInt = class private Len: Integer; Value: AnsiString; procedure Trim; procedure Shift(k: Integer); procedure MultiplyAtom(Multiplier1: TMyBigInt; Multiplier2: Integer); public constructor Create(iValue: Integer = 0); procedure Add(Addend1, Addend2: TMyBigInt); procedure Multiply(Multiplier1, Multiplier2: TMyBigInt); overload; procedure Multiply(Multiplier1: TMyBigInt; Multiplier2: Integer); overload; function ToString: string; procedure CopyFrom(mbCopy: TMyBigInt); end;

10