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:
-1
if negative0
if zero1
if positive
public bool IsZero { get; }
Determines whether this BigFloat represents zero.
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).
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.
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 Management
public static BigFloat SetPrecisionWithRound(BigFloat value, int precisionInBits)
Sets the precision of a BigFloat value with proper rounding.
Parameters:
value
- The BigFloat value to adjust.precisionInBits
- The desired precision in bits.
public static BigFloat ExtendPrecision(BigFloat value, int bitsToAdd)
Extends the precision by adding zero bits (doesn't add information).
public bool FitsInADouble(bool allowDenormalized = false)
Determines if this BigFloat can be accurately represented as a double (new in 2025).
Parameters:
allowDenormalized
- Whether to allow denormalized double values.
Example:
BigFloat big = new("1e+400");
if (!big.FitsInADouble())
{
Console.WriteLine("Too large for double!");
}
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.