Calculating Pi (π) in CMD
by NotePro in Craft > Digital Graphics
1103 Views, 0 Favorites, 0 Comments
Calculating Pi (π) in CMD

I am well aware that there are more accurate ways to calculate π but my version was created without looking at anyone else's batch program. I calculated pi using the Nilakantha series. This series is an infinite sequence starting with three.
The sequence then continues by alternating between adding and subtracting the number 4 divided by 3 consecutive integers starting with 2. An example of this would be
3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) … … …
If this pattern is carried out indefinitely, you would get the result of pi.
Supplies
You will need CMD, notepad, and a working 'ctrl' + 'c' & 'v' keys.
The Code

@echo off
title pi
set /a pi=300000000
set /a dePart = 2
set Lnum=2
:frac
echo %pi%
if %pi% == 314159265 goto done
set /a de = %dePart% * (%depart% + 1) * (%depart% + 2)
set /a dePart = %dePart% + 2
call :loop%Lnum%
goto frac
::::::::::add and subtract
:loop1
set /a pi = %pi% - (400000000 / %de%)
set Lnum=2
exit /b
::::::::::increment the fraction
:loop2
set /a pi = %pi% + (400000000 / %de%)
set Lnum=1
exit /b
:done
echo Done! (kind of)
set /a pi = %pi% - 300000000
echo 3.%pi%
echo (slightly better)
pause> nul
Final Thoughts
Overall, I am very disappointed with this project. Being only able to calculate pi with the help of pi doesn't make the project seem real. I had to stop the pi calculator early in the program because batch doesn't do decimals making the program take longer and be less accurate. You can probable expect me to attempt to create another batch program to better calculate pi.
But while you are waiting, take a look at this Amazing batch program over how to calculate pi (over 100 numbers after the decimal!) --> https://www.instructables.com/Calculate-pi-with-batch-file/