BigFloat Library
Arbitrary-precision floating-point arithmetic for C#
BigFloat extends beyond IEEE floating-point limitations, offering precision up to 2 billion bits with a comprehensive mathematical function library. Perfect for scientific computing, cryptography, and any application requiring extreme numerical precision.
Quick Start
Install via NuGet
dotnet add package BigFloatLibrary
Add using statement
using BigFloatLibrary;
Start using BigFloat
BigFloat pi = Constants.Fundamental.Pi;
BigFloat area = pi * radius * radius;
What is BigFloat?
BigFloat is a high-performance C# struct that provides arbitrary-precision floating-point arithmetic.
Unlike standard double
or decimal
types, BigFloat can handle numbers with
mantissas up to 2 billion bits, making it ideal for:
- Scientific computations requiring extreme precision
- Financial calculations where rounding errors are unacceptable
- Cryptographic applications
- Mathematical research and education
- Any scenario where IEEE floating-point precision is insufficient
// Standard double loses precision
double d1 = 0.1 + 0.2;
Console.WriteLine(d1 == 0.3); // False!
// BigFloat maintains precision
BigFloat b1 = new("0.1");
BigFloat b2 = new("0.2");
BigFloat sum = b1 + b2;
Console.WriteLine(sum.ToString()); // 0.3 exactly
Key Features
Arbitrary Precision
Mantissa size up to 2 billion bits, limited only by available memory
Guard Bits
32 hidden guard bits maintain accuracy through chain operations
Optimized Algorithms
Newton-Plus square root, binary exponentiation, and more
Math Functions
Comprehensive library including trig, log, roots, and more
Constants Library
Pre-computed constants like π, e with up to 1M digits
Easy Integration
Familiar operators and implicit conversions from standard types
Example Usage
// Create BigFloat numbers
BigFloat a = new("123456789.012345678901234567890");
BigFloat b = new(3.14159265358979323846);
// Basic arithmetic
BigFloat sum = a + b;
BigFloat product = a * b;
BigFloat quotient = a / b;
// Comparisons
bool isGreater = a > b;
bool areEqual = a == b;
// Convert to standard types (with safety check)
if (sum.FitsInADouble())
{
double result = (double)sum;
}
// Set specific precision
BigFloat precise = BigFloat.SetPrecisionWithRound(value, 1000);
// Extend precision
BigFloat extended = BigFloat.ExtendPrecision(value, 500);
// Check if integer
if (value.IsInteger)
{
BigFloat floor = value.FloorPreservingAccuracy();
}
// Parse with precision separator
BigFloat parsed = BigFloat.Parse("123.456|789"); // precise|guard
// Access pre-computed constants
BigFloat pi = Constants.Fundamental.Pi;
BigFloat e = Constants.Fundamental.E;
BigFloat sqrt2 = Constants.Fundamental.Sqrt2;
// Get constants with specific precision
var config = Constants.WithConfig(precisionInBits: 5000);
BigFloat precisePi = config.Get("Pi");
// Use in calculations
BigFloat circleArea = pi * radius * radius;
BigFloat compound = BigFloat.Pow(e, rate * time);
Resources & Documentation
🤓 Fun Fact
With BigFloat's 2 billion bit precision limit, you could calculate π to over 600 million decimal places! That's enough digits to wrap around the Earth's equator if each digit was 1 millimeter wide.