Skip to content

Instantly share code, notes, and snippets.

@jtmcdole
Last active March 15, 2021 00:10
Show Gist options
  • Save jtmcdole/dc5c7c0a2cc6dab7d3b7cd0fddcb298b to your computer and use it in GitHub Desktop.
Save jtmcdole/dc5c7c0a2cc6dab7d3b7cd0fddcb298b to your computer and use it in GitHub Desktop.
Dartpad Bigπ

Dartpad for Bigπ

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment