Happy almost-Pi Day! On March 14th (3/14), nerds around the world celebrate that magical, irrational number that has fascinated mathematicians for millennia. Here at Jsonic, we love finding creative ways to push the tools we use every day to their absolute limits. It's a fun way to explore their power and discover new techniques.
So, in the spirit of technical curiosity, we asked ourselves a slightly absurd question: Can we approximate the value of Pi using a single jq command?
The answer, it turns out, is yes. Let's dive in.
The Challenge: No bash, No bc, Just jq
The goal is to calculate Pi using only the logic available within jq. We can't shell out to other programs, and we can't use command-line math tools. We start with a simple JSON input and transform it into an approximation of Pi.
We'll use the Leibniz formula for π, an infinite series that looks like this:
π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
By calculating enough terms in this series, we can get a reasonable approximation of Pi.
The jq Implementation
To implement this, we need to generate a series of numbers (1, 3, 5, 7...), alternate their signs (positive, negative, positive...), take their reciprocals, sum them up, and finally, multiply by 4.
Here's a jq filter that does just that, using a specified number of iterations from an input JSON file to determine its accuracy.
Input (iterations.json):
{
"iterations": 50000
}
The jq Command:
jq '.iterations |
[range(0; .)] |
map(1 / (1 + 2 * .) * if . % 2 == 0 then 1 else -1 end) |
add * 4
' iterations.json
Let's break down what this magical incantation does:
.iterations: We start by selecting the number of iterations from our input file.[range(0; .)]: We create an array of numbers from 0 up to (but not including) our iteration count. This gives us our series[0, 1, 2, 3, ...].map(...): We then iterate over each number in that array to transform it.(1 + 2 * .): Inside the map, we take the current number (let's call itn) and calculate1 + 2n. Forn = 0, 1, 2, 3..., this produces the denominators1, 3, 5, 7...from the Leibniz formula.if . % 2 == 0 then 1 else -1 end: This is the clever bit for alternating the sign. Ifnis even, we multiply by 1. If it's odd, we multiply by -1.1 / (...) * (...): We combine these, calculating(1 / denominator) * sign. This gives us the terms of the series:1, -1/3, 1/5, -1/7, ....add: This command sums all the elements in the resulting array.* 4: Finally, we multiply the sum by 4 to get our approximation of Pi.
The Result
Running the command with 50,000 iterations gives you:
3.141572653589793
That's an approximation of Pi accurate to four decimal places! Not bad for a tool designed to filter text.
Why Do This?
While you probably shouldn't use this method for your critical scientific calculations, this little exercise is a testament to the surprising power and flexibility packed into our everyday developer tools. It shows that with a bit of creativity, you can push them far beyond their intended use cases.
So this Pi Day, take a moment to appreciate the elegant tools you use every day. And maybe, just for fun, try to make them do something they were never designed for. Happy calculating!