     NB.          Perils of Subtraction: 
     NB. Computing Pi by the classical algorithm
     NB.          Donald B. McIntyre
     NB. Vector 1995.  For APL95, San Antonio, Texas, June 1995  

   	y=. 0.5                        NB. Radius of circle
   	(0.5*1-(1-y^2)^0.5)^0.5        NB. J in style of old APL
0.258819
 	%: 0.5*1- %: 1-y^2             NB. %: is square root
0.258819
 	%:-: 1- %: 1- *:y              NB. *: is square
0.258819
 	%:-:-.%:-.*:y                  NB. -. is 1 minus
0.258819
 	ch=. %:@-:@-.@%:@-.@*:         NB. Tacit
 	ch 0.5
0.258819
 	%: +/ *: 3 4                  NB. Square root of sum of squares
5
 	f=. %: [. g=. +/ [. h=. *:    NB. [. returns verb left argument
         f@g@h 3 4                      NB. Conjunctions
5
 	(] -: f@h) y=. i.4             NB. f is the inverse of h
1
 	g&.h 3 4                       NB. &. is conjunction Under
5
 	(<@$@> -: $&.>) 1 2 3; i.2 4   NB. Shape Under Open
1
 	ch=. %: @(-:@-.@%:@-.)@ *:     NB.%; is inverse of *:
 	ch 0.5
0.258819
 	ch=. (-:@-.@%:@-.)&.*:         NB. Use Under
   	ch0.258819
	ch=. -:@(-.@%:@-.)&.*:         NB. -. is its own inverse
	ch 0.5
0.258819
	ch=. -:@(%:&.-.)&.*:           NB. Use Under
	ch 0.5
0.258819
 
	pi=. 3 : '6*(2^y.)*(ch^:y.) 0.5'  NB. Explicit definition
	pi 8
3.14159
 
	g=. ]`(0.5"_)                  NB. Gerund
	pi=. 6: * 2&^ * ch^:g          NB. Train of verbs
	pi"0 i.9                       NB. This looks good
3 3.10583 3.13263 3.13935 3.14103 3.14145 3.14156 3.14158 3.14159
	pi"0 [ 24 25 26 27 28          NB. This doesn't!
3.18198 3.3541 4.24264 6 0
 
         NB. Convert c21d5a to decimal
        -(16^2)*(1 13 5 10 +/ .% 16^1 2 3 4)
_29.3516
 NB. ==========================================================
     NB. Floating-point in IBM/360 architecture
     dfh=. '0123456789abcdef'&i.           NB. Decimal from Hex
     bfh=. [: , 2 2 2 2"_ #: dfh           NB. Binary from hex
     fs360=. _1: ^ {.@bfh                  NB. Floating-point sign
        g=. #.@}.@bfh@(2&{.)
     fe360=. 16"_ ^ g - 64"_              NB. Floating-point exponent
        f=. 16"_ ^ >:@i.@#
     ff360=. (+/ .% f)@dfh@(2&}.) f.      NB. Floating-point fraction
     fp360=. fs360 * fe360 * ff360      NB. Floating-point to decimal
      pad=. 16&({.!.'0')            NB. Pad with '0' using fit (!.)
     dfh360=. fp360@pad
     dfh360 'c21d5a'
_29.3516
 
     <;._2 '428 424 c21d5a '
Ŀ
428424c21d5a

     ];._2 '428 424 c21d5a '
428   
424   
c21d5a
     pad;._2@(,&' ')'428 424 c21d5a'     NB. Pad with trailing blanks
4280000000000000
4240000000000000
c21d5a0000000000
 
     dfh360=. ,@(fp360"1@table@pad;._2@(,&' '))
     dfh360=. [: , [: fp360"1 pad;._2@(,&' ')
     dfh360 '41c 414 411 c11 411c c21d5a'
12 4 1 _1 1.75 _29.3516
 NB. ============================================================
 NB. Floating-point on PC
 NB. (_1^sign)*(2^(2#.exp)-1023)*1+(2#. fr) % 2^# fr
     s=. _1: ^ {.  [. e=. [: }. 12&{.  [. f=. 12"_ }. ]
     exp=. [: 2&^ #. - 1023"_
     fr=. [: >: #. % 2&^@#
     dfbjpc=. (s * exp@e * fr@f) f.
     dfhj=. dfbjpc@bfh
     dfhj '400921f9'
3.14159
 
     ir=. 3!:3 }.~ 4: + #@$   NB. Internal representation 
                              NB. drop prefix
       ir 0.25
00000000
0000d03f
   
       ]y=. ir %1 2 4
00000000
0000f03f
00000000
0000e03f
00000000
0000d03f
       ir o.1
182d4454
fb210940
       _2 <\ y                  NB. How infix works
Ŀ
000000000000000000000000
0000f03f0000e03f0000d03f

       _2 ,\ y
000000000000f03f
000000000000e03f
000000000000d03f
       hx=. (_2: ,\ ])@ir
       bi=. <"_1                NB. Box items
       bi 8 2$ , hx 0.25
Ŀ
000000000000d03f

       |. bi 8 2$ , hx 0.25
Ŀ
3fd0000000000000

       reverse=. ;@|.@bi@(8 2&$)@,"1
       hex=. reverse@hx
       hex % 1 2 4
3ff0000000000000
3fe0000000000000
3fd0000000000000
   
   NB. How floating-point numbers are subtracted inside a PC
     fmt=. [: 1&": [: (37#10)&(#:!.0) 1e37&*     NB. Format
     n=. ": ,. i.29 [ h=. hex *: c [ c=. ch^:(i.29) 0.5
     ch2=. fmt *: c [ pi=. ": ,. p [ p=. 6*(2^i.29)*c
     n ,.' ',. h ,.' ',. ch2,.' ',. pi
 0 3fd0000000000000 2500000000000000000000000000000000000       3
 1 3fb126145e9ecd57 0669872981077806900000000000000000000 3.10583
 2 3f91722b8b740eb0 0170370868554658400000000000000000000 3.13263
 3 3f71855b44e5d93f 0042775693130948080000000000000000000 3.13935
 4 3f518a29d563ba00 0010705383806982602000000000000000000 3.14103
 5 3f318b5da3b24800 0002677062618171577000000000000000000 3.14145
 6 3f118baa99e90fff 0000669310452190985100000000000000000 3.14156
 7 3ef18bbdd7a0ffff 0000167330412994548420000000000000000 3.14158
 8 3ed18bc2a711ffff 0000041832778246986258000000000000000 3.14159
 9 3eb18bc3daf00000 0000010458205499386252000000000000000 3.14159
10 3e918bc427e00000 0000002614552058188835000000000000000 3.14159
11 3e718bc43b000001 0000000653638057013239500000000000000 3.14159
12 3e518bc440000001 0000000163409517028867400000000000000 3.14159
13 3e318bc440000001 0000000040852379257216850000000000000 3.14159
14 3e118bc440000001 0000000010213094814304212000000000000 3.14159
15 3df18bc440000001 0000000002553273703576053000000000000 3.14159
16 3dd18bc500000000 0000000000638318842227647500000000000 3.14159
17 3db18bc400000001 0000000000159579571779033820000000000 3.14159
18 3d918bd000000000 0000000000039895309278392680000000000 3.14161
19 3d718bc000000000 0000000000009973688541720092000000000 3.14159
20 3d518bffffffffff 0000000000002493560913308100000000000 3.14167
21 3d318bffffffffff 0000000000000623390228327025000000000 3.14167
22 3d11900000000001 0000000000000155986334959834520000000 3.14307
23 3cf1c00000000000 0000000000000039412917374193050000000 3.15981
24 3cd1ffffffffffff 0000000000000009992007221626406000000 3.18198
25 3cb4000000000001 0000000000000002775557561562891000000  3.3541
26 3ca0000000000001 0000000000000001110223024625156400000 4.24264
27 3c90000000000000 0000000000000000555111512312578200000       6
28 0000000000000000 0000000000000000000000000000000000000       0
   
        5.551115123125782e_17 -:  dfhj '3c9'
1
        5.551115123125782e_17 -:  2^(#. e bfh '3c9')-1023
1
   
   NB. STSC APL*PLUS PC System 5.0:
   
   NB.  n        *: next chord          Pi
   NB. 23   3.8857805861880482e_15   3.13748
   NB. 24   9.9920072216264070e_16   3.18198
   NB. 25   2.2204460492503132e_16   3.00000
   NB. 26   5.5511151231257832e_17   3.00000
   NB. 27   0                        0

    3.8857805861880482e_15 -: (16^_11)*1 1 8 +/ .% 16^1 2 3
1
    9.9920072216264070e_16 -: (16^_12)* 4 8 +/ .% 16 ^1 2
1
    2.2204460492503132e_16 -: (16^_12)* 1* 16^_1
1
    5.5511151231257832e_17 -: (16^_13)* 4*16^_1
1
   
   NB. VS APL  on an IBM 4341 computer (360 Architecture):
   
   NB.  n        *: next chord        Hex   Digits    Pi
   NB. 23   3.8927194800919541e_15  351188    4     3.1403
   NB. 24   9.7144514654701180e_16  344600    2     3.1375
   NB. 25   2.3592239273284575e_16  341100    2     3.0923
   NB. 26   5.5511151231257810e_17  334000    1     3.0000
   NB. 27   1.3877787807814456e_17  331000    1     3.0000
   NB. 28   0                       000000    0     0
   
   3.8927194800919541e_15 -: (16^_11)*1 1 8 8+/ .%16^>:i.4
1
   9.7144514654701180e_16 -: (16^_12)*4 6 +/ .% 16^1 2
1
   2.3592239273284575e_16 -: (16^_12)*1 1 +/ .% 16^1 2
1
   5.5511151231257810e_17 -: (16^_13)* 4%16^1
1
   1.3877787807814456e_17 -: (16^_13)* 1%16^1
1
   
   NB. ===============================================
      dfhj n25=. '3cb4000000000001'
2.77556e_16
      dfhj n26=. '3ca0000000000001'
1.11022e_16
      dfhj n27=. '3c90000000000000'
5.55112e_17
   
      f=. [: #."1[: 16 4&$ 64&{.
      hfb=. {&'0123456789abcdef'@f  f.
      hfb 0 0 1 1 1 1 1 1 1 1 1 1
3ff0000000000000
   
      dfhj n25
2.77556e_16
      dfhj n26
1.11022e_16
      dfhj n27
5.55112e_17
      12{.bfh n27
0 0 1 1 1 1 0 0 1 0 0 1
      hfb 12{.bfh n27
3c90000000000000
      hfb bfh n27
3c90000000000000
      dfhj n27
5.55112e_17
      bfh n27
0 0 1 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

      n27-: hfb bfh n27
1
      dfhj n27
5.55112e_17
   



1



