Unix/Linux Go Back    


OS X (Apple) OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

PI day.

OS X (Apple)


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-18-2018   -   Original Discussion by wisecracker
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 19 May 2018, 3:23 PM EDT
Location: Loughborough
Posts: 1,261
Thanks: 377
Thanked 338 Times in 265 Posts
PI day.

Hi guys and gals...
OSX 10.13.3, default bash terminal calling ksh in the code.
I decided to have a go at creating PI using ksh only as a bit of fun.
I also decided on a Taylor series to do such a task.
This was the fun bit of code that generates PI to 6 decimal places although it could easily be set to continuous running and calculate within the limits of ksh's floating point precision.
It takes around 35 seconds to calculate to six decimal places...


Code:
#!/bin/ksh
# !/usr/local/bin/dash
# PI using the Taylor series.
# PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11......

clear
OP="-"
BASE=3.0
VAL=1.0
N=1
MYPI=0.0
while true
do
	if [ $(( $N % 2 )) -eq 0 ]
	then
		OP="+"
	fi
	if [ $(( $N % 2 )) -gt 0 ]
	then
		OP="-"
	fi
	VAL=$(( ($VAL * 1.0) $OP (1.0 / $BASE) ))
	BASE=$(( $BASE + 2.0 ))
	N=$(( $N + 1 ))
	MYPI=$(( $VAL * 4.0 ))
	# Hard coded known value to exit loop without interaction.
	if [ "${MYPI:0:8}" == "3.141592" ]
	then
		break
	fi
done
printf "PI to six decimal places = %.6f...\nIterations = $N...\n" "$MYPI"

Result:


Code:
PI to six decimal places = 3.141592...
Iterations = 1530012...
AMIGA:amiga~/Desktop/Code/Shell> _

Enjoy and hope to see other solutions on this thread...

Last edited by wisecracker; 03-18-2018 at 06:54 AM..
Sponsored Links
    #2  
Old Unix and Linux 03-18-2018   -   Original Discussion by wisecracker
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 May 2018, 5:11 PM EDT
Location: Aachen, Germany
Posts: 12,727
Thanks: 413
Thanked 3,912 Times in 3,598 Posts
I had to do this in awk as I don't have ksh installed, but I think you get the gist... Usually, an iteration is exited if changes get below a defined bound which I here chose to be 10^-7...


Code:
awk -vOFMT="%.8f" '
BEGIN   {SGN = BS = 1

         do     {OLD  = PI4
                 PI4 += SGN/BS
                 SGN *= -1
                 BS  += 2
                 DLT  = OLD - PI4
                }
         while (DLT>0?DLT:-DLT > 1E-7)

         print PI4*4, 4*atan2(1,1), 22/7
        }
'
3.14159285 3.14159265 3.14285714

The Following User Says Thank You to RudiC For This Useful Post:
wisecracker (03-19-2018)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Julian day to dates in YEAR-MONTH-DAY Ggg Shell Programming and Scripting 1 06-19-2013 08:33 AM
Move the files between Current day & a previous day dsfreddie UNIX for Dummies Questions & Answers 2 06-06-2012 12:28 PM
Script to check if last modified day is previous day hegdepras Shell Programming and Scripting 1 05-25-2011 03:40 AM
Script to find previous month last day minus one day timestamp girish.raos Shell Programming and Scripting 3 09-03-2009 08:57 PM
Write a shell script to find whether the first day of the month is a working day phani Shell Programming and Scripting 7 03-18-2005 12:59 AM



All times are GMT -4. The time now is 03:24 AM.