A Weird Imagination

Pi in shell

Posted in

Calculating π the hard way#

In honor of Pi Day, I was going to try to write a script that computed π in shell, but given the lack of floating point support, I decided it would be too messy. If you want to see hard to follow code to generate π, I highly recommend the IOCCC entry westley.c from 1998, the majority of which is an ASCII art circle which calculates its own area and radius in order to estimate π. The hint file suggests looking at the output of

$ cc -E westley.c

The 2012 entry, endoh2 is also a pretty amazing π calculator.

Getting π#

Instead, I will just generate π the shell way: using another program.

$ python -c 'import math; print(math.pi)'
3.14159265359

if a small amount of π is sufficient. For more digits, we can download them:

$ curl 'http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html' \
    | grep '[0-9]\{70\}' \
    | tr -d ' \n' \
    > pi

Math with π#

Then we can do math with π:

$ echo $(cat pi) + 1 | bc
$ echo $(cat pi) \* 42.  | bc
131.9468914507713160154310220977391211362811147737544444809476728769\
28289064020777942377462664368916855250219633557856879177941473601124\
...
$ echo $(cat pi) / 42.  | bc -l
.07479982508547126758

Displaying in large text#

Or display it in large letters:1

$ <pi split -C 10 --filter='cat; echo' \
      | while read piLine
        do
            banner "$piLine"
        done
 #####             #    #          #    #######  #####   #####   #####  #######
#     #           ##    #    #    ##    #       #     # #     # #     # #
      #          # #    #    #   # #    #       #     #       # #       #
 #####             #    #######    #     #####   ######  #####  ######   #####
      #   ###      #         #     #          #       # #       #     #       #
#     #   ###      #         #     #    #     # #     # #       #     # #     #
 #####    ###    #####       #   #####   #####   #####  #######  #####   #####
 ...

(banner truncates the output at 10 characters per line, so this uses split to split the input into 10 character lines.)

Or display it in huge letters:

$ <pi printerbanner
                                  ### 
                                 #####                                                                     # 
                                #######                                                                    ## 
                               #########                                                                  ##### 
                              ########                                                                    ###### 
                             #######                                                                     ######## 
                            #######                                                                       ######## 
                            ######                                                                          ####### 
                           ######                                                                            ###### 
                           #####                                                                              ###### 
                          #####                                                                                ##### 
                          #####                                                                                 #### 
                          ####                                                                                   #### 
                          ####                                                                                   #### 
                          ####                                                                                   #### 
                          ####                                                                                   #### 
                          ####                                             ##                                    #### 
                          #####                                           ####                                   #### 
                          #####                                          ######                                 ##### 
                          ######                                        ########                               ###### 
                          #######                                      ###########                            ###### 
                          ########                                   ###############                        ######## 
                           #########                               ###################                    ######### 
                           ###########                          ##########################             ############ 
                            #############                    ##############   #################################### 
                            ################             #################     ################################## 
                             ############################################       ################################ 
                              ##########################################         ############################## 
                               ########################################            ########################### 
                                ######################################               ####################### 
                                  ##################################                    ################# 
                                    ##############################                         ########### 
                                      ########################## 
                                         #################### 
                                             ###########

                              ###### 
                            ########## 
                           ############ 
                          ############## 
                          ############## 
                          ############## 
                           ############ 
                            ########## 
                              ###### 
...

  1. You may need to install the sysvbanner package or your distribution's equivalent. This version of banner is not available on FreeBSD: instead printerbanner is called banner

Comments

Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.