pi_calculator 0.0.1
pi_calculator: ^0.0.1 copied to clipboard
A Dart package for high-precision calculation of Pi (π) using the Srinivasa Ramanujan series.
pi_calculator #
A Dart package dedicated to calculating the value of Pi (π) with high precision, primarily utilizing the rapidly convergent Srinivasa Ramanujan series.
Features #
- High-Precision Pi Calculation Implements the Srinivasa Ramanujan series for calculating Pi to a specified number of decimal places.
- BigInt Support Uses BigInt for handling large intermediate factorial and power values to prevent overflow.
- Decimal Arithmetic Leverages the decimal package for accurate floating-point arithmetic with arbitrary precision.
- Factorial & Power Helpers Includes optimized helper methods for factorial and BigInt power calculations.
Installation #
Add pi_calculator to your pubspec.yaml file:
dependencies:
pi_calculator: ^0.0.1 # Replace with the latest version
Then, run dart pub get (or flutter pub get) in your project's root directory.
Usage #
Import the main library file:
import 'package:pi_calculator/pi_calculator.dart';
Here's how to calculate Pi using the PiCalculator class:
import 'package:pi_calculator/pi_calculator.dart';
import 'package:decimal/decimal.dart';
void main() {
// Calculate Pi with 2 terms and 50 decimal places of precision
Decimal piValue50 = PiCalculator.calculatePiRamanujan(2, decimalPrecision: 50);
print('Pi (2 terms, 50 precision): $piValue50');
// Expected output will be close to 3.14159265358979323846264338327950288419716939937510
// Calculate Pi with 3 terms and 100 decimal places of precision
Decimal piValue100 = PiCalculator.calculatePiRamanujan(3, decimalPrecision: 100);
print('Pi (3 terms, 100 precision): $piValue100');
// Calculate Pi with 4 terms and 150 decimal places of precision
Decimal piValue150 = PiCalculator.calculatePiRamanujan(4, decimalPrecision: 150);
print('Pi (4 terms, 150 precision): $piValue150');
// Example of invalid input handling
try {
PiCalculator.calculatePiRamanujan(-1); // Throws ArgumentError
} catch (e) {
print('Error: $e');
}
try {
PiCalculator.calculatePiRamanujan(1, decimalPrecision: 0); // Throws ArgumentError
} catch (e) {
print('Error: $e');
}
}
Running Tests #
This package includes unit tests for the PiCalculator class and its helper methods.
To run the tests:
- Navigate to the root directory of the
pi_calculatorpackage in your terminal. - Run the following command:
dart test
Contributing #
Contributions are welcome! If you have suggestions for improvements, bug fixes, or want to add other Pi calculation algorithms, feel free to open an issue or submit a pull request on the GitHub repository.
License #
This package is distributed under the MIT License. See the LICENSE file for details.