👤
Home Man
Search
Today's Posts
Register

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

PI day.

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 03-18-2018
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..
# 2  
Old 03-18-2018
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)
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Code creates day 32 instead of 1st day of next month. libertyforall Shell Programming and Scripting 5 08-11-2010 06:01 PM
May i know the day to day activities of a Solaris system administrator? Sesha Solaris 4 05-11-2010 01:34 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
/var size is increasing day by day thepurple Solaris 11 11-13-2007 05:19 AM
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
How to compare prev day file to current day file Smurtzy Shell Programming and Scripting 1 12-05-2001 06:33 PM


All times are GMT -4. The time now is 08:39 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password