{"id":35468,"date":"2022-08-20T20:47:34","date_gmt":"2022-08-20T18:47:34","guid":{"rendered":"https:\/\/hooxies.nl\/?page_id=35468"},"modified":"2026-11-25T21:08:03","modified_gmt":"2026-11-25T20:08:03","slug":"pidtw","status":"publish","type":"page","link":"https:\/\/hooxies.nl\/?page_id=35468","title":{"rendered":"PIDTW"},"content":{"rendered":"<p><b>Display<\/b><br \/>\n<a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-hp41cx.png\"><img decoding=\"async\" style=\"cursor:pointer; cursor:hand;width: 226px; height: 104px;\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-hp41cx.png\" border=\"0\" \/><\/a> <a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2023\/02\/pidtw-dm41x.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2023\/02\/pidtw-dm41x.png\" alt=\"\" width=\"174\" height=\"104\"\/><\/a> <br \/>\n<em>(HP-41CX, Hewlett Packard 1983 and DM41X, <a href=\"https:\/\/www.swissmicros.com\/\" rel=\"noopener noreferrer\" target=\"_blank\">SwissMicros<\/a> 2020)<\/em><br \/>\n&nbsp;<\/p>\n<p><b>Overview<\/b><br \/>\nThe <tt>PIDTW<\/tt> can calculate the decimals of PI according to the method by <a href=\"https:\/\/crypto.stanford.edu\/pbc\/notes\/pi\/code.html\" target=\"_blank\" rel=\"noopener\">Beeler et al. in 1972<\/a>. It approximates PI that gives an additional bit of precision with 14 terms giving 4 decimal digits of precision each time because 214 > 104. Therefore 2800 = 14\u00b7200 terms are used. The author of a very compact C program, Dik T. Winter (hence DTW in the program name), wrote the following 160 characters\u2019 code:<\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a\/5;<br \/>\n&nbsp;&nbsp;&nbsp;for(;d=0,g=c*2;c-=14,printf(\"%.4d\",e+d\/a),e=d%a)for(b=c;d+=f[b]*a,<br \/>\n&nbsp;&nbsp;&nbsp;f[b]=d%--g,d\/=g--,--b;d*=b);}<\/tt><\/p>\n<p>which can be reformatted to something more readable:<\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;int a=10000,b,c=2800,d,e,f[2801],g;<br \/>\n&nbsp;&nbsp;&nbsp;int main() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=0;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(b<c) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=b+1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[b]=a\/5;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d=0;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g=c*2;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(g>0) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=c;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while((b>0)) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d=d*b+f[b]*a;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g=g-1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[b]=d%g;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d=d\/g;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g=g-1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=b-1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c=c-14;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(\"%.4d\", e+d\/a);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e=d%a;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g=c*2;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br \/>\n&nbsp;&nbsp;&nbsp;}<\/tt><\/p>\n<p>which gives the terms as 4 digits with tens to hundreds of iterations around the variable-g-while-loop and hundreds to thousands of iterations around the variable-b-while-loop. For this program to understand its feasibility, the requirements for the array size and the number of iterations were analysed before writing a program for the HP-41CX. It turns out that for each decimal it requires 3.5 times as many terms as well as array size. For example, for 800 decimals, it requires 2800 terms to be calculated and an array with 2800 values. The number of loops for b increases exponentially which, together with the required memory for the array <b>f[..]<\/b> brings limitations due to the capability of the calculator. Having mentioned that, it is feasible to determine the decimals up to 120. From the C program, it has been verified that the values in array <b>f[..]<\/b> are positive integers that remain under 10000. The solution for storing and updating array <b>f[..]<\/b> is found in using an ASCII file, thereby converting the 4 digits number in one pair of ASCII converted bytes. Each number in <b>f[..]<\/b> requires therefore 2 bytes. The alternative of storing each number in a register would require too much memory. The design of the program is explained later.<\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-deftable.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-deftable-413x189.png\" alt=\"\" width=\"413\" height=\"189\"  \/><\/a><\/p>\n<p>The values of PI are displayed in the table below for reference. This grouping of 4 decimals is how the program will show the results. The rows up to 120 decimals have been marked in both tables.<\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-pivalues.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-pivalues-413x210.png\" alt=\"\" width=\"413\" height=\"210\" \/><\/a><br \/>\n<br \/>\n&nbsp;<\/p>\n<p><b>Design<\/b><br \/>\nThe key problem with this algorithm is handling the values of the array <b>f[..]<\/b>. The solution built for this program is to store each value as ASCII bytes in a file. One file record can store up to 254 bytes which implies a maximum 127 values of <b>f[..]<\/b>. To make the process repeatable, the number of bytes must be even and fit precisely in each defined number of records (without empty space at the end). In the first table the best fit of number of records and number of bytes per record is given. This is the basis for storing all values of <b>f[..]<\/b>. As this is not linear, the number of records is retrieved from 2 alpha-based lookup vectors in lines 15-20. These have been marked with dotted lines in the table.<\/p>\n<p>The values of <b>f[..]<\/b> are stored in the ASCII file PIF via routine 05 (line 154) and retrieved via routine 06 (line 178). To make these routines fast a number wxyz is encrypted and decrypted as follows:<\/p>\n<ul>\n<li>Encryption of <b>f[..] = <i>wxyz<\/i><\/b>  starts with splitting it into two numbers <b><i>wx<\/i><\/b> and <b><i>yz<\/i><\/b><\/li>\n<li>To avoid the NULL character, 100 is added to <b><i>wx<\/i><\/b> and 1 is added to <b><i>yz<\/i><\/b><\/li>\n<li>The characters are moved to the Alpha register: <b>CHR(<i>wx<\/i>+100) &#038; CHR(<i>yz<\/i> + 1)<\/b><\/li>\n<li>Decryption to get <b>f[..]<\/b> is the reverse process: <b>f[..] = VAL(wx) &#8211; 100 + VAL (yz) \u2013 1<\/b><\/li>\n<\/ul>\n<p>The initialisation of <b>f[..]<\/b> with value 2000 is a sequential process thereby creating records and appending characters as per definition of the number of records and bytes per records from the lookup vector.<br \/>\nFor retrieving and updating the value of <b>f[..]<\/b> the pointer depends on the value of b. This calculation is done from line 89 onwards and is based on the algorithm. Please note that the integer part points to the record and the decimals (division by 1000) point to the first character of the two-byte value of <b>f[..]<\/b>:<\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-pointer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-pointer-413x43.png\" alt=\"\" width=\"413\" height=\"43\" \/><\/a><\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-records.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-records-413x84.png\" alt=\"\" width=\"413\" height=\"84\" \/><\/a><\/p>\n<p>Several tests have been conducted to verify this pointer calculation process with sample values:<\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-tests.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-tests-413x139.png\" alt=\"\" width=\"413\" height=\"139\" \/><\/a><\/p>\n<p>Because the GETREC instruction moves the pointer to the end of the copied part record the pointer must be set twice, for decryption and encryption (after updating <b>f[..]<\/b>). The pointer is calculated once and stored in R09. In the link to the program package (below) the program listing is also provided with comments.<\/p>\n<p><\/p>\n<p><b>Runtime<\/b><br \/>\nRunning the program Is more of a nostalgic interest because the performance is extremely low due to the number of loops (see first table) that needs to be handled. Simulations using the HP-41CX emulator V41 on maximum speed and turbo show that the runtime varies from 15 minutes, 1.5 hours to 4.5 hours for determining 40, 80 respectively 120 decimals:<\/p>\n<p><a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-lapse.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-lapse-413x103.png\" alt=\"\" width=\"413\" height=\"103\" \/><\/a><\/p>\n<p>Compared to the HP-41CX\u2019s performance, these runtimes could easily be multiplied by a factor 4.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Example<\/b><br \/>\n<a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-example.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-example-413x261.png\" alt=\"\" width=\"413\" height=\"261\" \/><\/a><br \/>\n&nbsp;<\/p>\n<p><b>Program Listing<\/b><br \/>\nThe listing of PIDTW is given below.<br \/>\n<a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-program.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-program.png\" width=\"413\" height=\"626\"  \/><\/a><br \/>\n&nbsp;<\/p>\n<p><b>Registers, Labels and Flags<\/b><br \/>\n<a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-registers.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw-registers-413x259.png\" alt=\"\" width=\"413\" height=\"259\"  \/><\/a><br \/>\n&nbsp;<\/p>\n<p><b>Downloads<\/b><br \/>\nPDF format of program <a rel=\"hp-41cx_pidtw\" href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2023\/02\/pidtw.pdf\">PIDTW<\/a>.<br \/>\nRAW\/TXT format of program <a href=\"https:\/\/hooxies.nl\/wp-content\/uploads\/2022\/08\/pidtw.zip\">PIDTW<\/a> (in zip file).<br \/>\n&nbsp;<\/p>\n<div style=\"font-size: 12px;\">This program is copyright and is supplied without representation or warranty of any kind. The author assumes no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof<\/div>\n<p><i>&copy; 2006-2022 by Auke Hoekstra<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Display (HP-41CX, Hewlett Packard 1983 and DM41X, SwissMicros 2020) &nbsp; Overview The PIDTW can calculate the decimals of PI according to the method by Beeler et al. in 1972. It approximates PI that gives an additional bit of precision with 14 terms giving 4 decimal digits of precision each time because 214 > 104. Therefore [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-35468","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/pages\/35468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hooxies.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35468"}],"version-history":[{"count":1,"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/pages\/35468\/revisions"}],"predecessor-version":[{"id":57403,"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/pages\/35468\/revisions\/57403"}],"up":[{"embeddable":true,"href":"https:\/\/hooxies.nl\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/hooxies.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}