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].
# http://www.brotherstechnology.com/math/e-formulas.html
# e=2.718281828459045 from Python 3.8.0.
# Google's value, e=2.71828182846.
#
# For *NIX 64 bit or greater systems, 32 bit systems commented out.
# (Also tested on AMIGA OS_3.1 using ADE the UNIX emulator and ksh88.)

echo ""
echo "Calculate e to 14+ decimal places, 64 bit, (7+ decimal places, 32 bit),"
echo "integer maths inside 'dash' or 'sh'."
echo ""

initial_e=0

# 32 bit version for AMIGA ADE.
# e=200000000

# Default 64 bit version.
e=2000000000000000
k=0
factorial=1
diff=$(( e - initial_e ))
while [ ${diff} -gt 1 ]
do
    initial_e=${e}
    k=$(( k + 1 ))
    factorial=$(( factorial * 2 * k * (2 * k + 1) ))

    # 32 bit version.
    # e=$(( e + (((2 * k + 2) * 100000000) / factorial) ))

    # Default 64 bit version.
    e=$(( e + (((2 * k + 2) * 1000000000000000) / factorial) ))
    diff=$(( e - initial_e ))
done

# 32 bit version.
# printf "%.8f\n" $(( e ))e-8

# Default 64 bit version.
result=$( printf "%.15f\n" $(( e ))e-15 )
# Result: 2.718281828459041
echo "${result}"

echo ""
echo "Python 3.8.0 value, e = 2.718281828459045."
echo "Number of iterations = ${k}."
echo ""

Result in 64 bit mode, my usual platform.

Last login: Sun Oct 13 15:21:17 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./e_constant.sh

Calculate e to 14+ decimal places, 64 bit, (7+ decimal places, 32 bit),
integer maths inside 'dash' or 'sh'.

2.718281828459041

Python 3.8.0 value, e = 2.718281828459045.
Number of iterations = 9.

AMIGA:amiga~/Desktop/Code/Shell> _
3 Likes

Are you just now getting into infinite series calculations?

Hi Neo...

No.
It is the WWW and the number of times people ask why 'x/y' gives an integer result instead of floating/fixed point result.

After exhaustive searching it looks like this site has these type of solutions now without the aid of awk, bc, dc and the rest.
I am trying to eliminate piping and subshells and use only POSIX shell, (dash), builtins only...

It is also the integer part combined with a POSIX shell that interests me not Taylor, Maclaurin or other series, as I have already done that here:
DFT using pure ksh ONLY!
Thanks to Corona688 for his maths expertise translating my naive maths coding.
But this is for ksh93 only and ksh93 HAS floating point arithmetic already making life easy.

That is why I now have turned to POSIX compliance only.
Inside the above code is Nth Root, Sin and Cos.
I don't think these are possible in a POSIX shell integer maths, but until I try I will not have found at least one limitation of shell scripting for my use.
It they are then they will only have limited decimal point accuracy to one or two decimal places.

This is my hobby, finding the limitations of a language, and boy, (floating/)fixed point arithmetic is one of them.
This is one reason why I have all but abandoned Python because it is SOOO flexible with all the modules available.
I am not even sure this is possible:
Minifloat - Wikipedia
But I will have a go at the 8 bit version knowing that I might fail...

Moderator comments were removed during original forum migration.