C++ Pi Calculator - Leibniz Formula












1












$begingroup$


Hi :) Yesterday I saw a CodeTrain video in where Daniel Shiffman tried to approximate Pi using the Leibniz series. It was interesting so I decided to try it too.



I wrote this console application with a simple goal: Approximate as fast as possible as many digits of Pi the program can until it reaches the maximum number the cicle counter can achieve(without starting to commit calculation errors with the series formula) OR the program is stopped.



It starts by first running two background threads which execute two functions that do all the job: piLoop(), for calculating the series; and displayLoop(), for refreshing the screen and displaying the actual calculated Pi. To prevent concurrency problems, mutexes are used to prevent the threads from accessing Pi and other program shared variables at the same time. The program runs until piCicles reaches the maximum value a long long int can get OR the End key is pressed.



The final program runs, by glancing at it(that means a very unreliable measurement), at an approximated rate of 30 million cicles per second in my computer. That means it can calculate the 10th digit of PI(around 87 billion cicles) in about an hour.



I want to know if there are some optimizations that can be made so the program can run at all his power.



Here's the code:



main.cpp



#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <windows.h>
#include <limits>

void piLoop();
void displayLoop();
void getShouldStop();

namespace {
typedef std::numeric_limits<long double> ldbl;
typedef std::numeric_limits<long long int> ullint;

const char displayDigits = ldbl::max_digits10;
const short int updateDelay_ms = 50; // You can modify this value to change the speed of console refreshing.
const long long int maxCicles = ullint::max();

bool shouldStop = false;
long double PI = 0;
unsigned long long int piCicles = 0;
std::mutex piLock;
}

int main()
{
std::thread piCalculatorThread( piLoop );
std::thread displayUpdaterThread( displayLoop );
piCalculatorThread.join();
displayUpdaterThread.join();

std::cin.get();

return 0;
}

void getShouldStop() {
shouldStop = GetAsyncKeyState( VK_END ) || shouldStop;
}

// If PI mutex is not locked, proceed to calculate the next PI, else wait
void piLoop() {
while ( true ) {
piLock.lock();

if ( shouldStop ) {
piLock.unlock();
return;
}

// Here the series is calculated
PI += ((long double)((piCicles&1)>0 ? -1 : 1)) / (piCicles*2 + 1);

if ( piCicles == maxCicles ) {
shouldStop = true;
piLock.unlock();
return;
}

piCicles++;

piLock.unlock();
}
}

// Wait until PI mutex is unlocked, then refresh the screen.
void displayLoop() {
std::cout.precision( displayDigits );
while ( true ) {
piLock.lock();

system("cls");
std::cout << "nn ----- PI Calculator ----- nn - "Seeing how PI is calculated is more enjoyable while eating some snacks."";
std::cout << "nnn PI: " << PI * 4;
std::cout << "n Cicles: " << piCicles << std::endl;
if ( maxCicles == piCicles ) // Just in case someone has the time to run this program for long enough
std::cout << "n Max precise 64-bit computable value reached!" << std::endl;

if ( !shouldStop ) getShouldStop();

piLock.unlock();

if ( shouldStop ) return;

std::this_thread::sleep_for( std::chrono::milliseconds( updateDelay_ms ) );
}
}


I think this code can also be run on other operating systems by removing the windows.h header and the getShouldStop() function as well, so Linux users may also give it a try :)



Here is a link to download the Windows executable if you want to test it.










share|improve this question









$endgroup$












  • $begingroup$
    Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
    $endgroup$
    – Carcigenicate
    4 hours ago










  • $begingroup$
    @Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
    $endgroup$
    – Nikko77
    5 mins ago
















1












$begingroup$


Hi :) Yesterday I saw a CodeTrain video in where Daniel Shiffman tried to approximate Pi using the Leibniz series. It was interesting so I decided to try it too.



I wrote this console application with a simple goal: Approximate as fast as possible as many digits of Pi the program can until it reaches the maximum number the cicle counter can achieve(without starting to commit calculation errors with the series formula) OR the program is stopped.



It starts by first running two background threads which execute two functions that do all the job: piLoop(), for calculating the series; and displayLoop(), for refreshing the screen and displaying the actual calculated Pi. To prevent concurrency problems, mutexes are used to prevent the threads from accessing Pi and other program shared variables at the same time. The program runs until piCicles reaches the maximum value a long long int can get OR the End key is pressed.



The final program runs, by glancing at it(that means a very unreliable measurement), at an approximated rate of 30 million cicles per second in my computer. That means it can calculate the 10th digit of PI(around 87 billion cicles) in about an hour.



I want to know if there are some optimizations that can be made so the program can run at all his power.



Here's the code:



main.cpp



#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <windows.h>
#include <limits>

void piLoop();
void displayLoop();
void getShouldStop();

namespace {
typedef std::numeric_limits<long double> ldbl;
typedef std::numeric_limits<long long int> ullint;

const char displayDigits = ldbl::max_digits10;
const short int updateDelay_ms = 50; // You can modify this value to change the speed of console refreshing.
const long long int maxCicles = ullint::max();

bool shouldStop = false;
long double PI = 0;
unsigned long long int piCicles = 0;
std::mutex piLock;
}

int main()
{
std::thread piCalculatorThread( piLoop );
std::thread displayUpdaterThread( displayLoop );
piCalculatorThread.join();
displayUpdaterThread.join();

std::cin.get();

return 0;
}

void getShouldStop() {
shouldStop = GetAsyncKeyState( VK_END ) || shouldStop;
}

// If PI mutex is not locked, proceed to calculate the next PI, else wait
void piLoop() {
while ( true ) {
piLock.lock();

if ( shouldStop ) {
piLock.unlock();
return;
}

// Here the series is calculated
PI += ((long double)((piCicles&1)>0 ? -1 : 1)) / (piCicles*2 + 1);

if ( piCicles == maxCicles ) {
shouldStop = true;
piLock.unlock();
return;
}

piCicles++;

piLock.unlock();
}
}

// Wait until PI mutex is unlocked, then refresh the screen.
void displayLoop() {
std::cout.precision( displayDigits );
while ( true ) {
piLock.lock();

system("cls");
std::cout << "nn ----- PI Calculator ----- nn - "Seeing how PI is calculated is more enjoyable while eating some snacks."";
std::cout << "nnn PI: " << PI * 4;
std::cout << "n Cicles: " << piCicles << std::endl;
if ( maxCicles == piCicles ) // Just in case someone has the time to run this program for long enough
std::cout << "n Max precise 64-bit computable value reached!" << std::endl;

if ( !shouldStop ) getShouldStop();

piLock.unlock();

if ( shouldStop ) return;

std::this_thread::sleep_for( std::chrono::milliseconds( updateDelay_ms ) );
}
}


I think this code can also be run on other operating systems by removing the windows.h header and the getShouldStop() function as well, so Linux users may also give it a try :)



Here is a link to download the Windows executable if you want to test it.










share|improve this question









$endgroup$












  • $begingroup$
    Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
    $endgroup$
    – Carcigenicate
    4 hours ago










  • $begingroup$
    @Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
    $endgroup$
    – Nikko77
    5 mins ago














1












1








1





$begingroup$


Hi :) Yesterday I saw a CodeTrain video in where Daniel Shiffman tried to approximate Pi using the Leibniz series. It was interesting so I decided to try it too.



I wrote this console application with a simple goal: Approximate as fast as possible as many digits of Pi the program can until it reaches the maximum number the cicle counter can achieve(without starting to commit calculation errors with the series formula) OR the program is stopped.



It starts by first running two background threads which execute two functions that do all the job: piLoop(), for calculating the series; and displayLoop(), for refreshing the screen and displaying the actual calculated Pi. To prevent concurrency problems, mutexes are used to prevent the threads from accessing Pi and other program shared variables at the same time. The program runs until piCicles reaches the maximum value a long long int can get OR the End key is pressed.



The final program runs, by glancing at it(that means a very unreliable measurement), at an approximated rate of 30 million cicles per second in my computer. That means it can calculate the 10th digit of PI(around 87 billion cicles) in about an hour.



I want to know if there are some optimizations that can be made so the program can run at all his power.



Here's the code:



main.cpp



#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <windows.h>
#include <limits>

void piLoop();
void displayLoop();
void getShouldStop();

namespace {
typedef std::numeric_limits<long double> ldbl;
typedef std::numeric_limits<long long int> ullint;

const char displayDigits = ldbl::max_digits10;
const short int updateDelay_ms = 50; // You can modify this value to change the speed of console refreshing.
const long long int maxCicles = ullint::max();

bool shouldStop = false;
long double PI = 0;
unsigned long long int piCicles = 0;
std::mutex piLock;
}

int main()
{
std::thread piCalculatorThread( piLoop );
std::thread displayUpdaterThread( displayLoop );
piCalculatorThread.join();
displayUpdaterThread.join();

std::cin.get();

return 0;
}

void getShouldStop() {
shouldStop = GetAsyncKeyState( VK_END ) || shouldStop;
}

// If PI mutex is not locked, proceed to calculate the next PI, else wait
void piLoop() {
while ( true ) {
piLock.lock();

if ( shouldStop ) {
piLock.unlock();
return;
}

// Here the series is calculated
PI += ((long double)((piCicles&1)>0 ? -1 : 1)) / (piCicles*2 + 1);

if ( piCicles == maxCicles ) {
shouldStop = true;
piLock.unlock();
return;
}

piCicles++;

piLock.unlock();
}
}

// Wait until PI mutex is unlocked, then refresh the screen.
void displayLoop() {
std::cout.precision( displayDigits );
while ( true ) {
piLock.lock();

system("cls");
std::cout << "nn ----- PI Calculator ----- nn - "Seeing how PI is calculated is more enjoyable while eating some snacks."";
std::cout << "nnn PI: " << PI * 4;
std::cout << "n Cicles: " << piCicles << std::endl;
if ( maxCicles == piCicles ) // Just in case someone has the time to run this program for long enough
std::cout << "n Max precise 64-bit computable value reached!" << std::endl;

if ( !shouldStop ) getShouldStop();

piLock.unlock();

if ( shouldStop ) return;

std::this_thread::sleep_for( std::chrono::milliseconds( updateDelay_ms ) );
}
}


I think this code can also be run on other operating systems by removing the windows.h header and the getShouldStop() function as well, so Linux users may also give it a try :)



Here is a link to download the Windows executable if you want to test it.










share|improve this question









$endgroup$




Hi :) Yesterday I saw a CodeTrain video in where Daniel Shiffman tried to approximate Pi using the Leibniz series. It was interesting so I decided to try it too.



I wrote this console application with a simple goal: Approximate as fast as possible as many digits of Pi the program can until it reaches the maximum number the cicle counter can achieve(without starting to commit calculation errors with the series formula) OR the program is stopped.



It starts by first running two background threads which execute two functions that do all the job: piLoop(), for calculating the series; and displayLoop(), for refreshing the screen and displaying the actual calculated Pi. To prevent concurrency problems, mutexes are used to prevent the threads from accessing Pi and other program shared variables at the same time. The program runs until piCicles reaches the maximum value a long long int can get OR the End key is pressed.



The final program runs, by glancing at it(that means a very unreliable measurement), at an approximated rate of 30 million cicles per second in my computer. That means it can calculate the 10th digit of PI(around 87 billion cicles) in about an hour.



I want to know if there are some optimizations that can be made so the program can run at all his power.



Here's the code:



main.cpp



#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <windows.h>
#include <limits>

void piLoop();
void displayLoop();
void getShouldStop();

namespace {
typedef std::numeric_limits<long double> ldbl;
typedef std::numeric_limits<long long int> ullint;

const char displayDigits = ldbl::max_digits10;
const short int updateDelay_ms = 50; // You can modify this value to change the speed of console refreshing.
const long long int maxCicles = ullint::max();

bool shouldStop = false;
long double PI = 0;
unsigned long long int piCicles = 0;
std::mutex piLock;
}

int main()
{
std::thread piCalculatorThread( piLoop );
std::thread displayUpdaterThread( displayLoop );
piCalculatorThread.join();
displayUpdaterThread.join();

std::cin.get();

return 0;
}

void getShouldStop() {
shouldStop = GetAsyncKeyState( VK_END ) || shouldStop;
}

// If PI mutex is not locked, proceed to calculate the next PI, else wait
void piLoop() {
while ( true ) {
piLock.lock();

if ( shouldStop ) {
piLock.unlock();
return;
}

// Here the series is calculated
PI += ((long double)((piCicles&1)>0 ? -1 : 1)) / (piCicles*2 + 1);

if ( piCicles == maxCicles ) {
shouldStop = true;
piLock.unlock();
return;
}

piCicles++;

piLock.unlock();
}
}

// Wait until PI mutex is unlocked, then refresh the screen.
void displayLoop() {
std::cout.precision( displayDigits );
while ( true ) {
piLock.lock();

system("cls");
std::cout << "nn ----- PI Calculator ----- nn - "Seeing how PI is calculated is more enjoyable while eating some snacks."";
std::cout << "nnn PI: " << PI * 4;
std::cout << "n Cicles: " << piCicles << std::endl;
if ( maxCicles == piCicles ) // Just in case someone has the time to run this program for long enough
std::cout << "n Max precise 64-bit computable value reached!" << std::endl;

if ( !shouldStop ) getShouldStop();

piLock.unlock();

if ( shouldStop ) return;

std::this_thread::sleep_for( std::chrono::milliseconds( updateDelay_ms ) );
}
}


I think this code can also be run on other operating systems by removing the windows.h header and the getShouldStop() function as well, so Linux users may also give it a try :)



Here is a link to download the Windows executable if you want to test it.







c++ performance multithreading windows numerical-methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Nikko77Nikko77

284




284












  • $begingroup$
    Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
    $endgroup$
    – Carcigenicate
    4 hours ago










  • $begingroup$
    @Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
    $endgroup$
    – Nikko77
    5 mins ago


















  • $begingroup$
    Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
    $endgroup$
    – Carcigenicate
    4 hours ago










  • $begingroup$
    @Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
    $endgroup$
    – Nikko77
    5 mins ago
















$begingroup$
Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
$endgroup$
– Carcigenicate
4 hours ago




$begingroup$
Just a side note, that's a excellent YouTube channel. Daniel is a great teacher despite being very scatter brained. I highly recommend it.
$endgroup$
– Carcigenicate
4 hours ago












$begingroup$
@Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
$endgroup$
– Nikko77
5 mins ago




$begingroup$
@Carcigenicate Indeed it is :) Just for the sake of this comment not being uninformative, I discovered that the Leibniz series can be accelerated using some techniques known as "Series acceleration", maybe in a future post I will improve this program with the review I could get from here and the accelerated Leibniz series if I manage to work it out, and maybe making a nice UI too :)
$endgroup$
– Nikko77
5 mins ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215796%2fc-pi-calculator-leibniz-formula%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215796%2fc-pi-calculator-leibniz-formula%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?