Fooling around with Dartpad sharing + Pi day fun.
How many digits of pi do you really need? Not that many it turns out. According to JPL1 you need ~40 to calculate the circumference of the universe to within the width of a hydrogen atom.
// Silly BigInt math with 50 digits of pi | |
// https://dartpad.dartlang.org/dc5c7c0a2cc6dab7d3b7cd0fddcb298b | |
// https://gist.github.com/jtmcdole/dc5c7c0a2cc6dab7d3b7cd0fddcb298b | |
const piString = '3.14159265358979323846264338327950288419716939937510'; | |
const piStringTrailingDigits = piString.length - 2; | |
final bigPi = BigInt.parse(piString.split('.').join()); | |
final bigTau = bigPi << 1; | |
final digitOffset = BigInt.from(10).pow(piStringTrailingDigits); | |
const au = 149597870700; | |
main() { | |
print(circumfrence(1)); // Meh | |
print(circumfrence(6378)); // earth | |
print(circumfrenceStr(6378)); // earth | |
print(circumfrenceStr(au)); // 1AU Earth Orbit | |
print(circumfrenceStr(117*au)); // 100 AU Termination Shock | |
} | |
double circumfrence(int radius) { | |
final circumference = bigTau * BigInt.from(radius); | |
return circumference / digitOffset; | |
} | |
String circumfrenceStr(int radius) { | |
final circumference = bigTau * BigInt.from(radius); | |
final str = '$circumference'; | |
final head = str.length - piStringTrailingDigits; | |
return str.substring(0, head) + '.' + str.substring(head); | |
} |