Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiHow-To'sTech Partner

How to View Digits of Pi in Real-Time with Delphi for Pi Day 2024

How to View Digits of Pi in Real Time with Delphi for Pi Day 2024

Here’s a guest blog post by Jim McKeeth, Embarcadero MVP and US Director of GDK Software. To celebrate the recent π day 2024 with a Delphi flavor I revisited the previous sample I made with Rudy’s Big Numbers library adding the functionality to view the digits of π in real time as they are being generated. Previously it would generate any number of digits you wanted, but you could only see them when it was all done.

What is Rudy’s Big Numbers Library?

Originally developed by the late Rudy Velthuis, the library adds support to Delphi for Big Integer, Big Decimal, and Big Rational numbers to Delphi. How big? Well, how much memory do you have? There really isn’t an upper limit.

The library is really easy to work with, and performs surprisingly well. There are a variety of samples, demos, and unit tests. The BigRational type is marked as experimental, and BigIntegers tends to perform better than BigDecimals.

How to Generate Pi with Delphi?

Last year I created the sample to calculate Pi with Delphi. It supports both the Chudnovsky and Bailey Borwein Plouffe (BBP) algorithms. Since BBP is faster, and it is a spigot that generates the digits in sequence, I thought it made more sense to add a call back to that algorithm. The callback returns 64 digits at a time, but you could easily modify that chunk size.

Watching the Digits of Pi in Real-Time

I find watching the digits of Pi appear both Zen like and exciting. Currently only the console app supports the callback function, but I plan to update the FireMonkey app in the future too.

The Bailey-Borwein-Plouffe Pi Code in Delphi

The updated code with the callback function.

And here is an example of calling the code and then outputting the digits one at a time to the console

It currently uses an anonymous method reference, which I know introduces a slight amount of overhead, but I’ve not benchmarked it to see how much or if it is worth changing the callback type. I figured since the purpose was to view the digits then a little slow down was necessary.

I also added a unit test to validate that the output from the callback matched the result of the function call.

Where Can I Get that Cool Vaporwave Athena Pi Day Wallpaper?

I’m glad you asked! I upscaled it to 4K, and there are a few variations….

How To Record Exact Operation Execution Time With Delphi

Do you want to try some of these examples for yourself? Why not download a free trial of the latest version of RAD Studio with Delphi?

This article was written by an Embarcadero MVP.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Director of Delphi Consulting for GDK Software USA. Many software related patents, including swipe and pattern unlock and search engines. First Silver and Gold Delphi badges on Stack Overflow Former Developer Advocate for Embarcadero Technologies. Long time fan of programming, especially with Delphi. Author, Podcaster/YouTuber, Improvisor, Public Speaker, Father, and Friend.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.