BigFloat Library

Arbitrary-precision floating-point arithmetic for C#

Updated for the 2025-12 release, BigFloat delivers precision-aware arithmetic, guard-bit–safe rounding helpers, and flexible constructors that target .NET 9. Use it for scientific computing, simulations, finance, cryptography, and anywhere IEEE formats are not enough.

  • Runs everywhere .NET 8/9 does: pure C# with no native dependencies.
  • Deterministic math: guard bits, explicit precision control, and spec-backed behaviors.
  • Production-friendly: MIT licensed, unit-tested, and aligned with the published BigFloat specification.

Quick Start

1

Install via NuGet

dotnet add package BigFloatLibrary
2

Add using statement

using BigFloatLibrary;
3

Start using BigFloat

BigFloat radius = new("1.5");
var precise = BigFloat.SetPrecisionWithRound(new("0.1"), 128);
BigFloat pi = Constants.Fundamental.Pi;
BigFloat area = pi * radius * radius;
int nearest = BigFloat.ToNearestInt(area);

What changed recently?

BigFloat 4.0.0 (December 2025) ships with refreshed constructors, accuracy/precision helpers, and playground cleanup. The docs and examples on this site now mirror the current APIs so you can copy/paste without guessing which overloads exist.

  • New AdjustAccuracy / SetAccuracy helpers replace older precision calls.
  • ToNearestInt and expanded rounding helpers clarify guard-bit behavior.
  • Constructors now expose guard-bit and precision tuning for integer inputs.
  • Benchmarks and playground snippets live in separate files to keep samples focused.
Guard-bit aware rounding
BigFloat value = new("123.98765");
// Drop 32 working bits, rounding as needed
BigFloat trimmed = BigFloat.AdjustPrecision(value, -32);
// Round to nearest int without consuming guard bits
int whole = BigFloat.ToNearestInt(value);
// Preserve accuracy metadata when aligning operands
BigFloat aligned = BigFloat.SetAccuracy(value, 256);
Console.WriteLine($"{trimmed} @ {trimmed.Size} bits");

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
Simple Example
// 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

Why engineers ship with BigFloat

🧭

Specification-backed

Behavior matches the published BigFloat Specification, so precision, rounding, and comparison rules are predictable.

🔁

Reproducible results

Deterministic operators with guard bits make cross-platform runs consistent for benchmarks, simulations, and CI checks.

📦

No surprises in production

Pure C# with no native dependencies, MIT licensing, and NuGet delivery keeps deployments simple on Windows, Linux, or macOS.

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);

🤓 Fun Fact

With BigFloat's 2 billion bit precision limit, you can calculate π to over 600 million decimal places. Printed with 1 mm digits, that string would stretch for more than 600 kilometers—enough to span multiple countries.