Skip to content

Advanced bc Calculations: Precision Pi to Thousands of Decimals

Dive into the precision world of bash calculations with bc, showcasing how to compute Pi to an extraordinary level of accuracy. This guide provides step-by-step commands for calculating Pi to 1000 and 5000 decimals, a valuable resource for developers, mathematicians, and anyone interested in high-precision computations.


Calculate without the bc prompt

To do so, you can write any arithmetic operation with the scale option to decide how many decimals there should be and pipe it to the bc command:

Template

echo "scale=no_of_decimals; arithmatic_operation_here" | bc

Use from commandline

echo "var=10;var" | bc

Print pi in 1000 decimals

bc -l <<< "scale=1000; 4*a(1)"

Using bash inline

<<<"k=1024; m=k*k; g=k*m; g" bc

When you execute the bc command with the -l flag, it loads the math library which enables you to use all the advanced calculation functions

bc -l

Hex math with bc

echo 'obase=16; C+F' | bc

Floating point operations in shell scripts

$ bc -l <<< s(3/5)
$ echo "scale=4; 3 / 5" | bc

Calculate pi to an arbitrary number of decimal places

echo "scale=1000; 4*a(1)" | bc -l

Generate trigonometric/log data easily

echo "e("{1..8}");" | bc -l

Doing some floating point math

echo "8000000/(20*6*86400)" | bc -l

Print pi in 1000 decimals

bc -l <<< "scale=5000; 4*a(1)"

3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
22317253594081284811174502841027019385211055596446229489549303819644\
28810975665933446128475648233786783165271201909145648566923460348610\
45432664821339360726024914127372458700660631558817488152092096282925\
40917153643678925903600113305305488204665213841469519415116094330572\
70365759591953092186117381932611793105118548074462379962749567351885\
75272489122793818301194912983367336244065664308602139494639522473719\
07021798609437027705392171762931767523846748184676694051320005681271\