BigFloatLibrary 4.0.2
dotnet add package BigFloatLibrary --version 4.0.2
NuGet\Install-Package BigFloatLibrary -Version 4.0.2
<PackageReference Include="BigFloatLibrary" Version="4.0.2" />
<PackageVersion Include="BigFloatLibrary" Version="4.0.2" />
<PackageReference Include="BigFloatLibrary" />
paket add BigFloatLibrary --version 4.0.2
#r "nuget: BigFloatLibrary, 4.0.2"
#:package BigFloatLibrary@4.0.2
#addin nuget:?package=BigFloatLibrary&version=4.0.2
#tool nuget:?package=BigFloatLibrary&version=4.0.2
BigFloat Library for C#
BigFloat is a C# struct/class library for arbitrary-precision floating-point numbers, backed by System.Numerics.BigInteger. It combines a flexible mantissa, a wide exponent range, and a 32-bit guard-bit region to deliver high-precision arithmetic for very large or very small values.
Ryan Scott White / MIT License
Updated: December 1st, 2025
Current BigFloat version: 4.0.0
- GitHub repo: https://github.com/SunsetQuest/BigFloat
- Documentation site: https://bigfloat.org
- NuGet package (
BigFloatLibrary): https://www.nuget.org/packages/BigFloatLibrary - Original CodeProject article: “A BigFloat Library in C#”
Contents
- What is BigFloat?
- Key features
- Use cases
- Requirements
- Installation
- Quick start
- Core concepts: representation & guard bits
- Precision, accuracy, and guard-bit notation
- Accuracy & precision APIs (2025+)
- Math functions & constants
- Working with very large / very small numbers
- Project layout
- Background & related work
- License
What is BigFloat?
BigFloat is an arbitrary-precision floating-point type implemented as a readonly partial struct in the BigFloatLibrary namespace. Internally, a BigFloat value is stored as:
- A
BigIntegermantissa (_mantissa) - An integer binary scale (
Scale) - A cached bit count (
_size), which includes guard bits
Mathematically, values are represented in the form:
value = mantissa × 2^(Scale - GuardBits)
Most users can treat BigFloat as “a very large double with configurable precision,” but it exposes more information and control than standard IEEE types:
- You can inspect the effective precision (
Size) - You can tune how many effective working bits you want for integers and conversions
- You can grow or shrink the working accuracy explicitly without silently losing information
Key features
Arbitrary precision
- Mantissa backed by
System.Numerics.BigIntegerwith a design target of up to billions of bits of precision (limited by memory and patience).
Guard bits for stability
- A fixed 32 hidden guard bits live in the least-significant portion of the mantissa. These bits are not counted as “in-precision,” but they carry extra context so chained operations round more accurately.
Binary-first design
- BigFloat is fundamentally base-2; decimal formatting/parsing is layered on top.
- Decimal input like
0.1is converted to repeating binary internally; the API makes these trade-offs explicit rather than hiding them.
Accuracy & precision controls (2025 refresh)
Modern APIs are available for explicitly managing the accuracy “budget”:
AdjustAccuracy,SetAccuracySetPrecisionWithRound,AdjustPrecision(and compatibility helpers aroundExtendPrecision/ReducePrecision)- Rounding helpers like
ToNearestIntand truncation helpers that preserve accuracy metadata
Rich operator and helper set
- Full arithmetic and comparison operators (
+ - * / %,==,<,>, etc.) - Constructors from
int,long,ulong,double,decimal,BigInteger, and string - Conversion helpers such as
FitsInADouble, explicit casts to primitive numeric types, and formatting with standard and custom format strings
Math functions & algorithms
- Extended math library (exponential, log, roots, powers, trig, etc.) implemented in
BigFloatMathand related partials. - High-performance algorithms such as Newton-Plus-style square root, Karatsuba multiplication, and Burnikel–Ziegler-style division for very large operands.
Constants library
Constants.csexposes fundamental constants such as π and e with configurable accuracy.- Pre-computed decimal value files in the
valuesfolder extend constants up to roughly 1,000,000 digits.
Use cases
BigFloat is designed for scenarios where IEEE double/decimal are not accurate enough:
- Scientific and engineering simulations that accumulate error over long compute chains
- Financial or actuarial models where rounding behavior must be controlled and auditable
- Cryptographic or number-theoretic experimentation (huge exponents, large primes)
- Teaching / research into floating-point and arbitrary-precision arithmetic
Requirements
BigFloat follows the modern .NET toolchain.
Runtime / SDK
- .NET 8.0 SDK or later (the library targets
net8.0,net9.0ornet10.0).
Language
- C# 12 or later (the version shipped with the .NET 8 SDK at the time of writing).
Dependencies
System.Numerics.BigInteger(ships with .NET) – no external library dependencies.
Check your SDKs with:
dotnet --list-sdks
and confirm a 8.x, 9.x, or 10.x entry is available.
Installation
Option 1 – NuGet (recommended)
Add the library via NuGet:
dotnet add package BigFloatLibrary
or via PackageReference:
<ItemGroup>
<PackageReference Include="BigFloatLibrary" Version="*" />
</ItemGroup>
Refer to NuGet for the current package version (the README describes the 4.0.0 library APIs; NuGet version numbers may differ). (NuGet)
Option 2 – From source
Clone the repository:
git clone https://github.com/SunsetQuest/BigFloat.gitAdd the
BigFloatLibraryproject to your solution.Reference it from your main project.
Optionally include
Constants.csand thevaluesfolder for extended constant precision. (GitHub)
Quick start
A minimal console application using BigFloat:
using System;
using BigFloatLibrary;
class Program
{
static void Main()
{
// Standard double loses precision
double d = 0.1 + 0.2;
Console.WriteLine($"Double: 0.1 + 0.2 = {d}");
Console.WriteLine($"Equals 0.3? {d == 0.3}");
// BigFloat maintains a stable representation
BigFloat x = new("0.1");
BigFloat y = new("0.2");
BigFloat sum = x + y;
Console.WriteLine();
Console.WriteLine($"BigFloat: {x} + {y} = {sum}");
Console.WriteLine($"Equals 0.3? {sum == new BigFloat(\"0.3\")}");
// Use a constant and perform higher-precision math
BigFloat radius = new("10.123456789");
BigFloat pi = Constants.Fundamental.Pi;
BigFloat area = pi * radius * radius;
Console.WriteLine();
Console.WriteLine($"Area with radius {radius} ≈ {area}");
Console.WriteLine($"Nearest integer (guard-bit aware): {BigFloat.ToNearestInt(area)}");
}
}
The examples in the documentation site’s Getting Started and Examples pages are kept in sync with the current APIs and are suitable for copy/paste into your projects. (bigfloat.org)
Core concepts: representation & guard bits
DataBits, Scale, and Size
Internally, each BigFloat has three important fields/properties: (GitHub)
DataBits (
_mantissa : BigInteger) Holds the signed binary representation, including guard bits.Scale (
int) A binary offset that effectively slides the radix point left or right. Positive scale moves the point left (larger values), negative scale moves it right (more fractional bits).Size (
int) The number of in-precision bits, excluding guard bits (SizeWithGuardBitsincludes them). For zero, size is zero.

The public API exposes these either directly or via helper properties/methods (Size, Scale, BinaryExponent, etc.).
GuardBits
BigFloat reserves:
public const int GuardBits = 32;
as a fixed number of least-significant bits dedicated to guard precision. (GitHub)
These bits:
- Are not counted as in-precision, but they retain “extra evidence” from intermediate calculations.
- Make long chains of additions, multiplications, and transcendental operations behave more predictably than a pure fixed-precision representation.
Conceptually:
precise_bits | guard_bits
For example (conceptually):
101.01100|110011001100... ≈ 5.4
100.01001|100110011001... ≈ 4.3
---------------------------------
1001.1011|001100110011... ≈ 9.7
If we discarded the right side of the |, the final rounding decision would be based on much less information. With guard bits preserved, BigFloat can round closer to the “true” value in the final representation. (GitHub)
Precision, accuracy, and guard-bit notation
“X” digits and | separator
The library uses several conventions to show what is in-precision versus out-of-precision: (GitHub)
- Decimal outputs may show trailing
Xcharacters (e.g.,232XXXXXXX) where digits are beyond the guaranteed precision. - Binary diagnostics can include a
|between in-precision bits and guard bits. - Some parsing/formatting helpers accept or emit the
|separator to round-trip accuracy hints.
For example:
// "123.456|789" - '789' bits are treated as guard / accuracy hints
BigFloat value = BigFloat.Parse("123.456|789");
Decimal vs. binary precision
Most decimal fractions cannot be represented exactly in binary:
5.4→101.0110011001100…(repeating)4.3→100.0100110011001…(repeating)0.25→0.01(exact) (GitHub)
BigFloat is explicit about this:
- Conversions are always done in binary.
- Guard bits exist specifically to preserve more of the repeating tail when it is helpful.
- Precision and accuracy APIs let you control how many effective bits you want to keep.
Accuracy & precision APIs (2025+)
The 2025 releases focus on making precision control explicit and less error-prone. New APIs are described in the docs site’s Accuracy Controls section. (bigfloat.org)
Growing or shrinking accuracy
BigFloat x = new("123.456");
// Add 64 working bits without changing the represented value
BigFloat wider = BigFloat.AdjustAccuracy(x, +64);
// Remove 32 bits and round before dropping them
BigFloat trimmed = BigFloat.AdjustPrecision(x, -32);
// Set a specific accuracy target (including guard bits)
BigFloat withBudget = BigFloat.SetAccuracy(x, 256);
Console.WriteLine(withBudget.Size); // around 256 - GuardBits
Use these helpers instead of manually manipulating scale or trying to emulate precision changes yourself.
Rounding and truncation
BigFloat v = new("9876.54321");
// Round to nearest integer value without burning all guard bits
int nearest = BigFloat.ToNearestInt(v);
// Truncate to an integer but keep accuracy metadata in the BigFloat
BigFloat integer = v.TruncateToIntegerKeepingAccuracy();
// Reduce size with rounding at the cut point
BigFloat compact = BigFloat.SetPrecisionWithRound(v, 80);
The older ExtendPrecision / ReducePrecision helpers are still present for backward compatibility, but the newer APIs are preferred because they encode intent (accuracy vs. bare precision) more clearly. (bigfloat.org)
Math functions & constants
Math functions
The core struct is extended via partial classes:
BigFloatMath.cs– higher-order math (log, exp, powers, trig, roots, etc.) (GitHub)BigFloatRoundShiftTruncate.cs– shifting, rounding, truncation, splittingBigFloatParsing.cs– string/Span parsing, binary/decimal utilitiesBigFloatStringsAndSpans.cs– string/Span formatting
These modules use algorithms such as:
- Newton-Plus variants for square root on large inputs
- Karatsuba multiplication and Burnikel–Ziegler-style division once operands cross certain thresholds (GitHub)
Constants
Constants.cs exposes a hierarchy of constants collections, e.g.:
using BigFloatLibrary;
var constants = new BigFloat.Constants(
requestedAccuracyInBits: 1000,
onInsufficientBitsThenSetToZero: true,
cutOnTrailingZero: true);
BigFloat pi = constants.Pi;
BigFloat e = constants.E;
Constants.csprovides constants with thousands of decimal digits.- Text files in the
valuesfolder can be included to extend certain constants close to 1,000,000 digits. (GitHub)
Working with very large / very small numbers
Basic arithmetic
BigFloat a = new("123456789.012345678901234");
BigFloat b = new(1234.56789012345678); // from double
BigFloat sum = a + b;
BigFloat diff = a - b;
BigFloat product = a * b;
BigFloat quotient = a / b;
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {diff}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
The examples in the current README show how results may end with X characters in decimal form when you have exceeded the guaranteed precision range for that value. (GitHub)
Comparisons and base-10 vs. base-2 intuition
Because BigFloat is base-2, decimal rounding intuition can be misleading. For example:
BigFloat num1 = new("12345.6789");
BigFloat num2 = new("12345.67896");
bool equal = num1 == num2;
bool bigger = num1 > num2;
Even though 12345.6789 and 12345.67896 are different in base-10, their binary encodings may compare equal at the available precision. The README includes extended examples of this behavior and how guard bits mitigate surprises by preserving more of the repeating tail. (GitHub)
Extreme scales
BigFloat is designed to represent values with very large positive or negative exponents:
BigFloat large = new("1234e+7");
BigFloat veryLarge = new("1e+300");
BigFloat verySmall = new("1e-300");
- Very large values naturally move towards exponential notation (
1 * 10^300style strings) for readability. - Very small values may output as long rows of zeros followed by a small non-zero tail; the guard bits and accuracy metadata still track how meaningful that tail is. (GitHub)
Project layout
The repository is organized roughly as follows: (GitHub)
BigFloatLibrary/Core
BigFloatstruct (BigFloat.cs)Partial classes:
BigFloatCompareTo.csBigFloatExtended.csBigFloatMath.csBigFloatParsing.csBigFloatRandom.csBigFloatRoundShiftTruncate.csBigFloatStringsAndSpans.cs
Constants.csand supporting constant builder/catalog files
BigFloat.Tests/- Unit tests covering arithmetic, conversions, parsing, rounding, and edge cases.
- Use
dotnet testat the solution level to exercise the test suite.
BigFloatPlayground/Small exploratory console projects for:
- Benchmarking (
Benchmarks.cs) - Showcases / sample scenarios
- Experimental testing of new APIs or algorithms (GitHub)
- Benchmarking (
ChangeLog.md- High-level history of releases and notable changes (consult this file for detailed version history, including 4.0.0 notes).
BigFloatSpecification.md- A deeper technical specification describing the format, invariants, and accuracy rules for BigFloat values.
For curated, user-oriented documentation (architecture notes, examples, and API overviews), refer to https://bigfloat.org. (bigfloat.org)
Background & related work
BigFloat grew out of earlier work on high-precision numerics and the Newton-Plus square-root algorithm for large BigInteger values. (GitHub)
- Original write-up: “A BigFloat Library in C#” on CodeProject.
- Companion project: NewtonPlus – Fast BigInteger and BigFloat Square Root, which explores performance-optimized sqrt for very large integers and shows how the algorithm is adapted for BigFloat. (GitHub)
BigFloat is designed as an educational tool as much as a production-quality numeric type: the source is heavily commented, the specification is public, and the tests and playground make it easier to experiment with tricky numerics.
License
This project is distributed under the MIT License. See the MIT License file in this repository for details. (GitHub)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.