Calculating PI with Maxima

You can display PI fairly simply in Maxima,

(%i3) %pi,numer;
(%o3)                          3.141592653589793

To get more precision, you have to do some work,

(%i4) fpprec:1000;
(%o4)                                1000
(%i5) bfloat(4*atan(1));
(%o5) 3.1415926535897932384626433832795028841971693993751058209749445923078164\
062862089986280348253421170679821480865132823066470938446095505822317253594081\
284811174502841027019385211055596446229489549303819644288109756659334461284756\
482337867831652712019091456485669234603486104543266482133936072602491412737245\
870066063155881748815209209628292540917153643678925903600113305305488204665213\
841469519415116094330572703657595919530921861173819326117931051185480744623799\
627495673518857527248912279381830119491298336733624406566430860213949463952247\
371907021798609437027705392171762931767523846748184676694051320005681271452635\
608277857713427577896091736371787214684409012249534301465495853710507922796892\
589235420199561121290219608640344181598136297747713099605187072113499999983729\
780499510597317328160963185950244594553469083026425223082533446850352619311881\
710100031378387528865875332083814206171776691473035982534904287554687311595628\
63882353787593751957781857780532171226806613001927876611195909216420199b0

To save this expression outside of Maxima, use save.

(%i3) save("mypi",%o5);
(%o3)                          /home/nicks/mypi

Looking at the file,

(DSKSETQ $%O5
         '((BIGFLOAT SIMP 3324.)
           330213989696185833060592410709411491963279730236807522898459522702508
78004812796658245165050449512808426369149127905944148521850923941004081851159555
73625284669532267527367686874872064442099417759585090710377079380853405602051774
04609216212891851580788285755097685270998285133585803759298044890506817934080014
96248868520455958091652204162441843399269551951923211364282868284124124161267585
43330036208375518750654935107807916212847589328009093581776946899743430034870019
77776426500603652525257463330316513035117092779072775462687945994055498572400478
91822238546598860034429487840998984864543041820033930641359445611843047513417075
94180929770203062672033141507666709715157262146865270378276912731306940738674281
74186230862250840499959489459583934706164076849446080268027759682687295837718439
01300857922720019239678978887250678900607117622445001900858826920623226941299070
69241989006558520187119526040225256684086499705113747897956483865524866752757376
0886245842275493593418016698286055086400551575742704.
           2.))

This representation is ((BIGFLOAT P) M E) = M * 2^(E – P), or here,

(%i7) M;
(%o7) 330213989696185833060592410709411491963279730236807522898459522702508780\
048127966582451650504495128084263691491279059441485218509239410040818511595557\
362528466953226752736768687487206444209941775958509071037707938085340560205177\
404609216212891851580788285755097685270998285133585803759298044890506817934080\
014962488685204559580916522041624418433992695519519232113642828682841241241612\
675854333003620837551875065493510780791621284758932800909358177694689974343003\
487001977776426500603652525257463330316513035117092779072775462687945994055498\
572400478918222385465988600344294878409989848645430418200339306413594456118430\
475134170759418092977020306267203314150766670971515726214686527037827691273130\
694073867428174186230862250840499959489459583934706164076849446080268027759682\
687295837718439013008579227200192396789788872506789006071176224450019008588269\
206232269412990706924198900655852018711952604022525668408649970511374789795648\
38655248667527573760886245842275493593418016698286055086400551575742704
(%i8) 2^3322;
(%o8) 105110377476488338073759642279804468450012193121364145579719197214033487\
522818979054557379194627354235469441825366620647041539636078295194156908650210\
542971580349844969608552200333076868240067445423735960823513950826708649796548\
405268712179911743859010000261865347203856982128845262207572017607037683640644\
448905144005662894130110313846764555882318172250850816483729213915589694904696\
239847178703715007853989188552039829909087133377483721857044236277703223419267\
177049539783537097641381102988369914298553955345045492038800450579693349560689\
719173197015942539151853006348990199490845404907445826195722878474598422712738\
029475471478918216932430114748051442498201976865203293273965633238897555576055\
602528159460130032905123028371878592019051769859941133364956920750355593676796\
304029182241671051421892751495107388124808924077529249183069145004728868339175\
548750315804624998649065449594727044844939037215695363287703985418665043052771\
84641095297703354693522344642060637206460029232533992047394275463266304
(%i9) %o7/%o8,numer;
(%o9)                          3.141592653589793

This is a roundabout way of representing pi, but working with powers of two supposedly makes thing easy for the computer hardware for which all real numbers are natively scaled by powers of two (2). So in a ‘2-imal’ number the P is the placement of the ‘2-imal’ point. The term ‘2-imal’ goes analagously from decimal, and more generally for base Q, a ‘Q-imal’ real number. These could be called Q-floats, and BIGFLOATS would be called 2-floats.

Integers are so much easier to understand than real.

So, lets review what we’ve learned. When we write PI = 3.14159, we are using decimal notation which is really, 3 + 1/10 + 4/1000 + 1/10,000 + 5/100,000 + 9/1,000,000.

And in 2-imal, PI = 2 + 1 + 1/2^3 + 1/2^6 + 1/2^11 + 1/2^12. In Maxima, the binary digits of M can be displayed using obase, with the decimal placed after the first two digits.

(%i11) obase:2;
(%o1011)                              10
(%i1100) M;
(%o1100) 110010010000111111011010101000100010000101101000110000100011010011000\
100110001100110001010001011100000001101110000011100110100010010100100000010010\
011100000100010001010011001111100110001110100000000100000101110111110101001100\
011101100010011100110110010001001010001010010100000100001111001100011100011010\
000000100110111011110111110010101000110011011001111001101001110100100001100011\
011001100000010101100001010011011011111001001011111000101000011011101001111111\
000010011010101101101011011010101000111000010010001011110010010000101101101010\
111011001100010010111100111111011000110111101000100110001000010111010011010011\
000110111111011010110101100001011111111110101110010110110111101000000011010110\
111111011011110111000111000011010111111101101011010100010011001111110100101101\
011101001111100100100000100010111110001001011000111111110011001001001001010000\
110011001010001111011001110010001011011001111011100001000000000011111001011100\
010100001011000111011111100000101100110001101101001001000001101100001110001010\
101110100111001101001101001000101100011111110101000111111010010010011001111010\
111111000001101100101010111010010001111011100101000111010110110010110000111000\
110001011110011010101100010000010000101010100101011101110011110110101010010100\
100000111011100001001011010010110011011010110011100001100001101010100111001001\
010101111001001100000000100111100010111010001101100000010001100101000011000001\
000010111110000110010100100000101111001000110001011100011011011001110001110111\
110001110011110011101110010110000011000000011101000011000000011100110110010011\
110000011101000101110110000000111101000101000111110110101110001010101110111110\
000011011110100110001010010110010011101111000101011110010111111011010010101010\
110000001011100011000001110011001010101001001011111001110101010010101011010101\
110010100010101110100100010011000011000100110001111101000000101000100000001010\
101110010100011100101101010001010101010101100010000101101101011010011001100010\
111000011010000010001010000011110100011001110101000010101010010000110101011110\
111110001110010111010011001001110110011111011100001010000010001011000110110111\
110111100001010100010101110101001110001010101110101110100000110000011000111110\
110110011100101110000111110000101101001101110000111100100110001111010101111110\
101101011101000110011011011000010010011001111010111000111101000110010010100111\
000000100101000100101011000011001110111001110111000111101001000100110000110101\
101001011101110011010111111000100101111111110100000011011011001100010100000100\
001100100110110000111011000000010011100110011111011001000011010100110010001010\
010000111110010101100011000000101110111101100100000000011001011101111100001000\
101110101011101111010011000010101110101101100011101110000100110001000110000001\
011101011011001010001101110001000001000111000100100111110100000011101001110010\
110101011001100010100001111011011010110111111110011100000111111010001000010001\
110010010111000001011010001001000001010100100100001000010000000000100011010011\
100100011110000010010101001111000011111100110110101111000100001110001100110100\
001000010111101101110100101101100100110100110011100001100100111000110000110101\
011110100111000100011110000011010100101000110100000110100101101100001010100001\
011110110100010010110000011111010011100101000101010110101000100110011101000110\
110111011101111000010110110110000010011011110100011110000

Nick Strauss.COM

please contribute if you like

Leave a comment

Your email address will not be published. Required fields are marked *