Fun project calculating PI using integer maths.


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Fun project calculating PI using integer maths.
# 1  
Old 04-25-2020
Fun project calculating PI using integer maths.

Hi all...

As you know I have garnered a great interest in using integer maths to do some fixed point calculations.
Here is my next incarnation, calculating PI to 3 and 12 decimal places using 2 of the simplest of series to obtain the value.
First is the Gregory-Leibniz series which is notoriously SSLLOOWW but would be useful for 16 bit systems.
Second is the Nilakantha series which is much, MUCH faster and used to show the 12 places of decimals in 64 bit systems.

Leibniz formula for π - Wikipedia
Pi - Wikipedia

This carries on from my previous fun pieces on here.
Read the code for more information.
Code:
#!/bin/sh

# #!/usr/local/bin/dash
# PI.sh
# PI to 3, (Gregory-Leibniz series), and 12, (Nilakantha`s series), decimal places:-
# 3.141 [592653589]

# A FUN exercise to see if it is possible to get PI to at least 12 places of decimal.
# In 16 bit mode 3, (possibly 4), places of decimal is the absolute limit.
# In normal 32 bit integer mode 8 places of decimal is the absolute limit.
# In 64 bit integer mode 18 places of decimal is the absolute limit.
# Fully POSIX compliant!
#
# Important note: POSIX maths does not include "x**y", (x to the power of y),
# hence the bizarre usage of the variable POWER inside the script. This is
# to account for (-1)**(POWER) inside the functions for both of these
# examples to switch the sign of each term in the series.
#
# Issued to www.unix.com as CC0, Public Domain.

clear
echo 'Start...'
echo 'Using integer maths only and fully POSIX compliant!'

# Simplest of all, Gregory-Leibniz series - very, VERY SLOW,
# and errors may occur beyond the 6th decimal place in 32 bit mode.
# This example is for 16 bit systems.
# PI=(4/1)-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+(4/13)-...
echo ''
echo 'Gregory-Leibniz series, (very, VERY slow).'
PI_APPROX=0
SWITCH_SIGN=0
POWER=1
K=1
LOOP=0
while [ ${LOOP} -le 200 ]
do
    if [ ${PI_APPROX} -eq 3142 ]
    then
        break
    fi
    if [ $(( POWER%2 )) -eq 0 ]
    then
        SWITCH_SIGN="-1"
    else
        SWITCH_SIGN="1"
    fi
    PI_APPROX=$(( PI_APPROX+(1*(SWITCH_SIGN))*(4000/K) ))
    POWER=$(( POWER+1 ))
    K=$(( K+2 ))
    LOOP=$(( LOOP+1 ))
done
printf "16 bit; PI to 3 decimal places = %.3f...\n" "${PI_APPROX}e-3"
echo "Number of loops = ${LOOP}."
echo ''

# Nilakantha's series, much, MUCH faster...
# PI=3+(4/(2Ã-3Ã-4))âˆ'(4/(4Ã-5Ã-6))+(4/(6Ã-7Ã-8))âˆ'(4/(8Ã-9Ã-10))+...
echo 'Nilakantha`s series, (much, MUCH faster).'
PI_APPROX=3000000000000
SWITCH_SIGN=1
POWER=0
K=2
LOOP=0
while [ ${LOOP} -le 2500 ]
do
    if [ ${PI_APPROX} -eq 3141592653589 ]
    then
        break
    fi
    if [ $(( POWER%2 )) -eq 0 ]
    then
        SWITCH_SIGN="1"
    else
        SWITCH_SIGN="-1"
    fi
    PI_APPROX=$(( PI_APPROX+(1*(SWITCH_SIGN))*(4000000000000/(K*(K+1)*(K+2))) ))
    POWER=$(( POWER+1 ))
    K=$(( K+2 ))
    LOOP=$(( LOOP+1 ))
done
printf "64 bit; PI to 12 decimal places = %.12f...\n" "${PI_APPROX}e-12"
echo "Number of loops = ${LOOP}."
echo ''
echo 'Stop...'

Results OSX 10.14.3, default bash terminal, originally calling dash.
Code:
Start...
Using integer maths only and fully POSIX compliant!

Gregory-Leibniz series, (very, VERY slow).
16 bit; PI to 3 decimal places = 3.142...
Number of loops = 144.

Nilakantha`s series, (much, MUCH faster).
64 bit; PI to 12 decimal places = 3.141592653589...
Number of loops = 2426.

Stop...
AMIGA:amiga~/Desktop/Code/Shell> _

Enjoy...

Bazza...
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Coronavirus maths anyone?

This is a really interesting watch.............. (well worth a few minutes of your time) YouTube (1 Reply)
Discussion started by: hicksd8
1 Replies

2. Shell Programming and Scripting

Calculate the constant e to 14+ decimal places using integer maths.

Hi guys... I am loving this integer maths thing. 64 bit systems are certainly easier than 32 bit, but hey, I don't intend to leave out my fav' platform. Using one of the 'Brothers' methods, URL inside the code. #!/bin/sh # # #!/usr/local/bin/dash # e_constant.sh # Brother's formula . #... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. OS X (Apple)

A Fun Perfect Square Checker Using Integer Arithmetic Only... ;o)

A recent Python upload on another site gave me the inspiration to do an unusual bash version... This is a little tongue-in-cheek but an enjoyable bit of fun. It took around 11 seconds to prove 90000000000 had a perfect square of 300000... It is a stand alone program and has a degree of... (23 Replies)
Discussion started by: wisecracker
23 Replies

4. Shell Programming and Scripting

[FUN] Get some stats of your project/s

Heya Ever wanted to have some basic stats of your projects? Like: ./stats.sh ######################################## Project stats for "tui" ######################################## 260 kb in bin 24 kb in conf.etc 12 kb in conf.home 32 kb in docs/samples 176 kb in docs/wiki 280... (5 Replies)
Discussion started by: sea
5 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

Calculating an integer with awk

I would like to extract a number from $0 and calculate if it can be devided by 25. Though the number can also be less then 25 or bigger than 100. How do i extract the number and how can the integer be calculated? String: "all_results">39</span>I am looking for the number between "all_results"> ... (5 Replies)
Discussion started by: sdf
5 Replies

7. Shell Programming and Scripting

Using IF statements with maths where the input is not an integer

Hi All I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas! The code below extracts the pixel value at position X Y. The... (3 Replies)
Discussion started by: StudentFitz
3 Replies

8. Shell Programming and Scripting

Maths with variables

Hello, I'm trying to write a while loop for a decimal value in tcsh which I know can't be done. Instead I want my increments to be one order of magnitude too large and then divide it by 10 when I use the variable. However, I don't know how to divide my variable and set it as another. set... (1 Reply)
Discussion started by: DFr0st
1 Replies

9. Shell Programming and Scripting

Perl - maths equation - need help

if input to the perl program is ' ( p * ((a+b) * (c+d))) + q ' it shuld give the output as ' pac + pad + pbc + pbd + q ' .can anyone suggest a way to do this ? (7 Replies)
Discussion started by: Anuj8584
7 Replies

10. Shell Programming and Scripting

Problem with Maths

Heres a script i wrote as a bit of practise. What it does is insert a line in the middle of a file. The line being $1 and the file being $2 #!/bin/bash rm tempfile touch tempfile count=1 linenum= `wc -l < $2` if then echo $1 >> $2 else even=`expr "$linenum" % 2` if then... (3 Replies)
Discussion started by: Quesa
3 Replies
Login or Register to Ask a Question