API Documentation
Complete reference for BigFloat classes, methods, and properties
Constructors
public BigFloat(string value)
Creates a BigFloat from a string representation. This is the most precise way to initialize a BigFloat.
Parameters:
value- String representation of the number. Supports decimal, hexadecimal (0x), binary (0b), and scientific notation.
Example:
BigFloat a = new("123.456789012345678901234567890");
BigFloat b = new("1.23e+100");
BigFloat c = new("0xABC.DEF");
BigFloat d = new("0b1101.1011");
public BigFloat(double value)
Creates a BigFloat from a double value.
Parameters:
value- The double value to convert.
Note:
Due to the limitations of double precision, some precision may be lost. Use string constructor for exact values.
public BigFloat(int value)
public BigFloat(long value)
public BigFloat(BigInteger value)
Creates a BigFloat from various integer types.
Properties
public int Sign { get; }
Gets the sign of the BigFloat value.
Returns:
-1if negative0if zero1if positive
Sign collapses to 0 whenever the guard-bit zero tolerance applies (see IsZero).
public bool IsZero { get; }
Determines whether this BigFloat represents zero.
Zero tolerance: returns true when the mantissa length is below the 32 guard-bit boundary and the scaled window of significant bits stays entirely under that boundary (_size == 0 or _size + Scale < GuardBits while _size < GuardBits). IsPositive, IsNegative, and Sign all honor this rule.
public bool IsInteger { get; }
Determines whether this BigFloat represents an integer value (no fractional part).
Example:
BigFloat a = new("123.0");
Console.WriteLine(a.IsInteger); // True
BigFloat b = new("123.456");
Console.WriteLine(b.IsInteger); // False
public int Precision { get; }
Gets the precision of this BigFloat in bits (excluding guard bits).
public int BinaryExponent { get; }
Returns the unbiased base-2 exponent for the normalized value (1.x)·2e.
Useful for inspecting magnitude without converting to double or decimal.
Operators
public static BigFloat operator +(BigFloat a, BigFloat b)
public static BigFloat operator -(BigFloat a, BigFloat b)
public static BigFloat operator *(BigFloat a, BigFloat b)
public static BigFloat operator /(BigFloat a, BigFloat b)
public static BigFloat operator %(BigFloat a, BigFloat b)
Standard arithmetic operators with full precision preservation.
Example:
BigFloat a = new("100.5");
BigFloat b = new("25.25");
BigFloat sum = a + b; // 125.75
BigFloat diff = a - b; // 75.25
BigFloat prod = a * b; // 2537.625
BigFloat quot = a / b; // 3.9801980198...
BigFloat rem = a % b; // 0.5
public static bool operator ==(BigFloat a, BigFloat b)
public static bool operator !=(BigFloat a, BigFloat b)
public static bool operator <(BigFloat a, BigFloat b)
public static bool operator >(BigFloat a, BigFloat b)
public static bool operator <=(BigFloat a, BigFloat b)
public static bool operator >=(BigFloat a, BigFloat b)
Comparison operators for BigFloat values.
Arithmetic Helpers
These helpers adjust scaling and magnitude while preserving BigFloat precision metadata.
public static BigFloat AdjustScale(BigFloat value, int changeScaleAmount)
public BigFloat AdjustScale(int changeScaleAmount)
Moves the binary radix point left or right without changing the numeric value. Useful for aligning operands before custom algorithms.
Example:
BigFloat value = new("42.5");
BigFloat shifted = value.AdjustScale(+4); // multiply by 2^4 while keeping precision fields consistent
BigFloat back = shifted.AdjustScale(-4); // returns to the original magnitude
public static BigFloat Abs(BigFloat value)
public BigFloat Abs()
Returns the non-negative magnitude while preserving guard bits and current precision.
Example:
BigFloat negative = new("-0.125");
BigFloat magnitude = BigFloat.Abs(negative); // 0.125 with original accuracy
bool unchanged = magnitude.Sign >= 0; // true
Comparison Methods
Choose the comparison that matches your intent: canonical ordering, tolerant ULP checks, or bitwise identity.
public int CompareTo(BigFloat other)
public bool Equals(BigFloat other)
Canonical .NET ordering and equality. Guard bits are rounded away so values like 2.5 and 2.5000 compare equal.
Example:
var values = new[] { new BigFloat("2.5"), new BigFloat("2.5000") };
bool canonical = values[0].Equals(values[1]); // true
values = values.OrderBy(v => v).ToArray(); // uses CompareTo for stable ordering
public int CompareUlp(BigFloat other, int ulpTolerance = 0, bool ulpScopeIncludeGuardBits = false)
public bool EqualsUlp(BigFloat other, int ulpTolerance = 0, bool ulpScopeIncludeGuardBits = false)
Alignment-aware comparisons that allow a configurable ULP tolerance. Ideal for convergence tests or scenarios where “close enough” is acceptable.
Example:
BigFloat expected = new("0.1");
BigFloat actual = BigFloat.SetPrecisionWithRound(expected, expected.Size - 4);
bool closeEnough = actual.EqualsUlp(expected, ulpTolerance: 1); // tolerance counts the last main-bit
public int CompareTotalOrder(BigFloat other)
public bool IsBitwiseEqual(BigFloat other)
CompareTotalOrder produces a deterministic ordering that includes guard bits (no canonicalization) and is useful for fast sorts. IsBitwiseEqual checks the exact mantissa, scale, and precision.
Example:
BigFloat a = new("2.5");
BigFloat b = new("2.5000");
bool identical = a.IsBitwiseEqual(b); // false (different precision encodings)
int ordering = a.CompareTotalOrder(b); // distinguishes encodings while staying stable
Conversion & Interop
Explicit conversions honor IEEE-754 rounding rules and throw on overflow when using IConvertible.
public static explicit operator double(BigFloat value)
public static explicit operator float(BigFloat value)
public static explicit operator decimal(BigFloat value)
Round-to-nearest-even casts to built-in floating-point types. Magnitudes outside the finite range produce Infinity when casting, or an OverflowException when using the corresponding IConvertible methods.
Example:
BigFloat high = new("1e+400");
bool fits = high.FitsInADouble();
double narrowed = fits ? (double)high : double.PositiveInfinity;
public static explicit operator BigFloat(double value)
public static explicit operator BigFloat(BigInteger value)
public decimal ToDecimal(IFormatProvider? provider)
Create BigFloat instances from standard numeric types while zeroing guard bits to maximize reproducibility. Use the IConvertible methods when you need overflow validation or culture-aware formatting.
Mathematical Functions
public static BigFloat Sqrt(BigFloat value)
Calculates the square root using the Newton-Plus algorithm.
Parameters:
value- The value to calculate the square root of.
Returns:
The square root of the value.
Example:
BigFloat x = new("2");
BigFloat sqrt2 = BigFloat.Sqrt(x);
Console.WriteLine(sqrt2); // 1.41421356237309504880168872420969807...
public static BigFloat Pow(BigFloat baseValue, int exponent)
Raises a BigFloat to an integer power using binary exponentiation.
public static BigFloat Sin(BigFloat x)
public static BigFloat Cos(BigFloat x)
public static BigFloat Tan(BigFloat x)
Trigonometric functions with high precision using Payne-Hanek reduction.
public static BigFloat Log2(BigFloat value)
Calculates the base-2 logarithm.
public BigFloat FloorPreservingAccuracy()
public BigFloat CeilingPreservingAccuracy()
Floor and ceiling operations that maintain BigFloat precision (new in 2025).
Example:
BigFloat x = new("123.456");
BigFloat floor = x.FloorPreservingAccuracy(); // 123
BigFloat ceil = x.CeilingPreservingAccuracy(); // 124
Precision & Accuracy
Updated for the 2025-12 release. Prefer the accuracy-first helpers below; legacy
SetPrecision, ExtendPrecision, ReducePrecision, and the
obsolete Zero/One properties remain only for compatibility.
Preferred precision & accuracy APIs
Use these helpers for all new code paths:
AdjustAccuracy/SetAccuracyAdjustPrecision(delta-based resizing)SetPrecisionWithRoundwhen shrinking with explicit roundingZeroWithAccuracy/OneWithAccuracyfor context-aware identity values
public static BigFloat AdjustAccuracy(BigFloat value, int deltaBits)
public static BigFloat AdjustPrecision(BigFloat value, int deltaBits)
public static BigFloat SetAccuracy(BigFloat value, int accuracyBits)
Grow or shrink the working accuracy while keeping the numeric value stable where possible. AdjustPrecision is the underlying implementation and remains available for direct control.
Parameters:
deltaBits- Positive values append zero bits; negative values round then drop bits.accuracyBits- Target total accuracy (precision + scale).
Example:
BigFloat x = new("123.456");
BigFloat wider = BigFloat.AdjustAccuracy(x, 64); // add headroom
BigFloat trimmed = BigFloat.AdjustPrecision(x, -32); // round then drop bits
BigFloat target = BigFloat.SetAccuracy(x, 256); // exact accuracy target
[Obsolete] public static BigFloat SetPrecision(BigFloat value, int newSize)
public static BigFloat SetPrecisionWithRound(BigFloat value, int newSize)
[Obsolete] public static BigFloat ExtendPrecision(BigFloat value, int bitsToAdd)
[Obsolete] public static BigFloat ReducePrecision(BigFloat value, int bitsToRemove)
Rebuild the mantissa to a target size. SetPrecision pads or trims without rounding, but
it is obsolete—use AdjustPrecision or SetPrecisionWithRound so the rounding
behavior is explicit.
Example:
BigFloat value = new("0.1");
BigFloat rounded = BigFloat.SetPrecisionWithRound(value, 96);
BigFloat padded = BigFloat.SetPrecision(value, 256);
Legacy migration tips
Older members are kept for compatibility and delegate to the accuracy-first helpers:
SetPrecision→AdjustPrecision(orSetPrecisionWithRoundwhen shrinking)ExtendPrecision→AdjustPrecisionwith a positive deltaReducePrecision→AdjustPrecisionwith a negative deltaZero/One→ numeric literals orZeroWithAccuracy/OneWithAccuracyfor preserved context
public bool FitsInADouble(bool allowDenormalized = false)
public bool FitsInADecimal()
Test whether the value can safely down-convert to built-in numeric types.
Example:
BigFloat big = new("1e+400");
if (!big.FitsInADouble())
{
Console.WriteLine("Too large for double!");
}
Rounding Helpers (Guard-Bit Aware)
public static int ToNearestInt(BigFloat value)
public BigFloat TruncateToIntegerKeepingAccuracy()
Round without consuming guard bits. ToNearestInt uses the first fractional bit; the
truncation helper keeps accuracy metadata intact for subsequent operations.
Example:
BigFloat y = new("999.4999");
int rounded = BigFloat.ToNearestInt(y); // 999 or 1000
BigFloat integral = y.TruncateToIntegerKeepingAccuracy(); // retains guard bits
Console.WriteLine(integral.Size); // includes guard bits
public static BigFloat Round(BigFloat value)
public static BigFloat RoundToInteger(BigFloat value)
public static BigFloat TruncateByAndRound(BigFloat value, int bitsToRemove)
Higher-level rounding helpers used internally by precision setters and scale adjustments. They are useful when you need predictable guard-bit handling before serialization or interop.
Mathematical Constants
Constants.Fundamental.Pi
Constants.Fundamental.E
Constants.Fundamental.Sqrt2
Constants.Fundamental.GoldenRatio
Pre-computed mathematical constants with up to 1 million decimal digits precision.
Example:
// Direct access
BigFloat pi = Constants.Fundamental.Pi;
// With specific precision
var config = Constants.WithConfig(precisionInBits: 10000);
BigFloat precisePi = config.Get("Pi");
Constants.WithConfig(int precisionInBits)
Creates a constants configuration with specific precision requirements.
Parsing and Formatting
public static BigFloat Parse(string s)
public static bool TryParse(string s, out BigFloat result)
Parses a string representation into a BigFloat.
Supported Formats:
- Decimal:
"123.456","1.23e+10" - Hexadecimal:
"0xABC.DEF" - Binary:
"0b1101.1011" - Precision separator:
"123.456|789"
public string ToString()
public string ToString(string format)
Converts the BigFloat to a string representation.
Format Specifiers:
"G"or default - General format with precision masking"E"- Scientific notation"X"- Hexadecimal"B"- Binary
Example:
BigFloat x = new("123.456789");
Console.WriteLine(x.ToString()); // "123.456789"
Console.WriteLine(x.ToString("E")); // "1.23456789E+2"
Console.WriteLine(x.ToString("X")); // "7B.74BC6A7EF9D"
Known Limitations & Considerations
Base-2 Representation
BigFloat uses binary internally. Most decimal numbers cannot be represented exactly, leading to small precision differences. For example, 0.1 in decimal is a repeating binary fraction.
Guard Bit Consumption
The 32 guard bits gradually consume through operations. After approximately 10²¹ operations, precision may degrade. Not all functions implement perfect rounding.
Memory Usage
Very high precision numbers consume significant memory. A 1 million bit number requires approximately 125KB of memory.
Performance Considerations
Operations on very large precision numbers can be computationally intensive. Consider using appropriate precision for your use case.