🚀 Version 2.0.0 Available

BigFloat Library
High-Precision Floating Point Arithmetic

A cutting-edge C# library for arbitrary-precision floating-point arithmetic that extends far beyond IEEE limitations. Handle numbers with mantissas up to 2 billion bits and access mathematical constants with up to 1 million digits of precision.

2B+
Max Mantissa Bits
1M+
Constant Digits
MIT
License


BigFloat pi = bigConstants.Pi;
BigFloat radius = new("100.0");
BigFloat area = pi * radius * radius;

Precision Without Limits

BigFloat revolutionizes numerical computing by providing arbitrary-precision arithmetic with sophisticated guard bit mechanisms and advanced rounding algorithms.

Scale-Based Architecture

Unlike traditional IEEE floating-point exponents, BigFloat uses a scale-based approach measuring the radix point from the least significant digit, providing more intuitive precision control.

Guard Bit Technology

32 hidden guard bits maintain accuracy during sequential operations, preventing cumulative rounding errors that plague traditional floating-point arithmetic.

Mathematical Constants

Access pre-computed mathematical constants like π, e, and others with precision up to 1 million decimal digits, perfect for scientific and research applications.

Advanced Features

Built for demanding computational scenarios where precision is paramount.

Flexible Precision

Mantissa size up to 2 billion bits allows for unprecedented precision in numerical calculations, limited only by available memory.

Two's Complement

Uses two's complement representation for efficient arithmetic operations, leveraging the underlying BigInteger implementation.

Smart Rounding

Advanced rounding algorithms that check the most significant removed bit and apply appropriate rounding to maintain precision across operations.

Modern C# Integration

Built for C# 11/.NET 7+ with full operator overloading, implicit conversions, and seamless integration with existing .NET applications.

Comprehensive Math Library

Extended mathematical functions including logarithms, square roots, exponentials, and trigonometric operations with high precision.

Performance Optimized

Optimized for performance with caching mechanisms, efficient bit operations, and minimal memory allocation during calculations.

Architecture Deep Dive

Understanding the innovative design that makes BigFloat uniquely powerful.

Mantissa (DataBits)

BigInteger holding the binary representation

Scale

Radix point position from least significant digit

Size (Cached)

Optimized access to mantissa bit count

32 Guard Bits

Hidden precision maintenance

Scale vs. Exponent

Unlike IEEE floating-point's left-measured exponent, BigFloat's scale measures the radix point from the right (least significant digit), providing more intuitive control over decimal placement and precision.

Guard Bit Mechanism

The 32 hidden guard bits act as a precision buffer, maintaining accuracy during sequential operations. They correct cumulative rounding errors that would otherwise degrade precision over multiple calculations.

Precision Notation

BigFloat defaults its output to scientific notation to express precision (e.g., 2.32e10). However, it also supports a unique output and input notation (e.g., "232XXXXXXXX") where imprecise portions are marked with X's, ensuring users understand the reliability of each digit.

Code Examples

Explore BigFloat's capabilities with code examples.

Basic Arithmetic & Operations


    // Initialize BigFloat numbers
    BigFloat a = new("123456789.012345678901234"); // by String
    BigFloat b = new(1234.56789012345678);         // by Double

    // Basic arithmetic
    BigFloat sum = a + b;
    BigFloat difference = a - b;
    BigFloat product = a * b;
    BigFloat quotient = a / b;

    // Display results
    Console.WriteLine($"Sum: {sum}");

    Console.WriteLine($"Difference: {difference}");

    Console.WriteLine($"Product: {product}");

    Console.WriteLine($"Quotient: {quotient}");

    // Comparing Numbers
    BigFloat num1 = new("12345.6790");
    BigFloat num2 = new("12345.6789");

    bool areEqual = num1 == num2;
    bool isFirstBigger = num1 > num2;

    Console.WriteLine($"Are equal? {areEqual}"); // False
    Console.WriteLine($"First bigger? {isFirstBigger}"); // True

Live Output

Sum: 123458023.5802358024
Difference: 123455554.4444555554
Product: 152415787532.39
Quotient: 100000.000000000
Are equal? False
First bigger? True

High-Precision Constants


    // Access constants with specified precision
    Dictionary<string, BigFloat> bigConstants =
    Constants.WithConfig(precisionInBits: 1000).GetAll();
    BigFloat pi = bigConstants["Pi"];
    BigFloat e = bigConstants["E"];

    Console.WriteLine($"e to 1000 binary digits: {e}");

    // Use Pi in calculations
    BigFloat radius = new("100.0000000000000000");
    BigFloat area = pi * radius * radius;

    Console.WriteLine($"Circle area: {area}");

Precision Demonstration

π: 3.14159265358979323846264338327950288...
e to 1000 binary digits: 2.7182818284590452353602874713526624977572470936999595749669676 277240766303535475945713821785251664274274663919320030599218174 135966290435729003342952605956307381323286279434907632338298807 531952510190115738341879307021540891499348841675092447614606680 82264800168477411853742345442437107539077744992070
Circle area: 31415.9265358979324
Precise digits | XXXXXXXX Extended precision

Precision Manipulation


    // Initialize with high precision
    BigFloat preciseNumber = new("123.45678901234567890123");

    // Extend precision by adding bits
    BigFloat morePrecise = ExtendPrecision(
    preciseNumber, bitsToAdd: 50);

    Console.WriteLine($"Extended: {morePrecise}");

    // Create integer with custom precision
    BigFloat customInt = IntWithAccuracy(10, 100);
    Console.WriteLine($"Custom int: {customInt}");

    // Precision notation examples
    Console.WriteLine("232XXXX -> " + BigFloat.Parse("232XXXX"));

    Console.WriteLine("232X.X -> " + BigFloat.Parse("232X.X"));

Precision Control Results

Original: 123.45678901234567890123
Extended: 123.45678901234567890123000000000102788
Custom int: 10.000000000000000000000000000000
232XXXX -> 2.32e+6
232X.X -> 2.32e+3

Advanced Operations & Large Numbers


    // Handling Very Large Numbers
    BigFloat largeNumber = new("1234e+7");
    Console.WriteLine($"Large: {largeNumber}");

    // Alternative notation with masking
    Console.WriteLine($"Masked: {BigFloat.ToStringDecimal(largeNumber, digitMaskingForm: true)}");

    // Very large exponents
    BigFloat veryLarge = new("1234e+300");
    Console.WriteLine($"Very large: {veryLarge}");

    // Very small numbers
    BigFloat smallNumber = new("1e-300");
    Console.WriteLine($"Small: {smallNumber}");

    // Complex calculations
    BigFloat num5 = new("12121212.1212");
    BigFloat num6 = new("1234567");
    Console.WriteLine($"{num5} * {num6} = {num5 * num6}");

    Console.WriteLine($"Precision: {num6.Precision}");

Advanced Results

Large: 1.234e+10
Masked: 1234XXXXXXX
Very large: 123 * 10^301
Small: 1e-300
Complex calc: 1.496445e+11
Precision: 21 bits

Performance & Precision

Benchmarks and precision analysis demonstrating BigFloat's capabilities.

Guard Bit Effectiveness

Without Rounding 39 bits affected
With Rounding 18 bits affected

After 2⁴⁰ sequential operations, rounding maintains precision within guard bits.

Precision Scaling

IEEE Double 53 bits
IEEE Quad 113 bits
BigFloat 2B+ bits

Memory Efficiency

Base Overhead ~16 bytes
Per Precision Bit ~0.125 bytes
1000-bit Number ~141 bytes

Get Started

Quick installation and integration into your C# projects.

NuGet Package

Install-Package BigFloatLibrary

or via .NET CLI:

dotnet add package BigFloatLibrary

GitHub Source

git clone https://github.com/SunsetQuest/BigFloat.git

Add the source files directly to your project for maximum customization.

Requirements

  • C# 11 or later
  • .NET 7 or later
  • System.Numerics.BigInteger support

Quick Start Example

using BigFloatLibrary;

// Create your first BigFloat
BigFloat number = new("123.456789012345678901234567890");
BigFloat doubled = number * 2;

Console.WriteLine($"Original: {number}");
Console.WriteLine($"Doubled:  {doubled}");