ktsu.PreciseNumber 1.7.2

Prefix Reserved
dotnet add package ktsu.PreciseNumber --version 1.7.2
                    
NuGet\Install-Package ktsu.PreciseNumber -Version 1.7.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ktsu.PreciseNumber" Version="1.7.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.PreciseNumber" Version="1.7.2" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.PreciseNumber" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ktsu.PreciseNumber --version 1.7.2
                    
#r "nuget: ktsu.PreciseNumber, 1.7.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ktsu.PreciseNumber@1.7.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ktsu.PreciseNumber&version=1.7.2
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.PreciseNumber&version=1.7.2
                    
Install as a Cake Tool

ktsu.PreciseNumber

A high-precision numeric type for .NET that provides arbitrary precision arithmetic with a focus on accuracy. By combining the scale benefits of scientific notation with the precision of BigInteger, this library offers reliable and accurate mathematical operations where standard floating point types fall short.

License

NuGet Version
NuGet Version
NuGet Downloads

GitHub commit activity
GitHub contributors
GitHub Actions Workflow Status

Table of Contents

Features

  • Arbitrary Precision: Based on BigInteger for the significand, allowing numbers of unlimited size.

  • Scientific Notation: Uses an exponent and significand (the coefficient or mantissa in scientific notation) model similar to scientific notation.

  • Lossless Arithmetic: Preserves precision during calculations with no rounding errors.

  • Full .NET Integration: Implements INumber<T> interface for seamless integration with .NET's numeric ecosystem.

  • Comprehensive Mathematical Support: Includes advanced mathematical functions like exponential operations (Pow, Exp, Squared, Cubed), constant values (Pi, E, Tau) with high precision, absolute value operations, and specialized numerical checks (isOdd, isEven, etc.)—all with arbitrary precision.

  • Balanced Performance: The design prioritizes accuracy and precision while maintaining reasonable performance. For calculations where extreme precision matters more than raw speed, PreciseNumber delivers excellent results, though built-in numeric types remain faster for standard precision needs.

Getting Started

Installation

To install PreciseNumber, you can use the .NET CLI:

dotnet add package ktsu.PreciseNumber  

Or you can use the NuGet Package Manager in Visual Studio by searching for ktsu.PreciseNumber.

Requirements

This library requires .NET 8.0 or later.

Quick Usage

Basic Example

using System.Numerics;  
using ktsu.PreciseNumber;  

// Create PreciseNumber from various types  
var precise1 = 123.456.ToPreciseNumber();  
var precise2 = BigInteger.Parse("1234567890").ToPreciseNumber();  

// Perform calculation with high precision  
var result = precise1 * precise2 / 7.89.ToPreciseNumber();  

Console.WriteLine(result); // Displays accurate result with no floating point errors  

Common Operations

Create and perform operations with precise numbers:

using ktsu.PreciseNumber;  

// Create PreciseNumbers from various numeric types  
var a = 123.456.ToPreciseNumber();  
var b = 2.ToPreciseNumber();  

// Basic arithmetic operations  
var sum = a + b;        // 125.456  

var difference = a - b; // 121.456  

var product = a * b;    // 246.912  

var quotient = a / b;   // 61.728  

// Comparison  
bool isGreater = a > b; // true  

When to Use PreciseNumber

PreciseNumber is ideal for:

  • Financial calculations where exact precision is required beyond what decimal offers

  • Scientific computing involving very large or small numbers with many significant digits

  • Cryptography applications requiring arbitrary precision arithmetic

  • Mathematical algorithms where rounding errors would accumulate and affect results

For everyday calculations where standard precision is sufficient, built-in types like int, double, or decimal will offer better performance.

Advanced Usage

Type Conversions

The library provides seamless round-trip conversions between standard numeric types and PreciseNumber using extension methods:

using System.Numerics;  
using ktsu.PreciseNumber;  

// Convert FROM standard types TO PreciseNumber  
int originalInt = 42;  
double originalDouble = 3.14159;  
decimal originalDecimal = 1234.5678m;  
BigInteger originalBigInt = BigInteger.Parse("123456789012345678901234567890");  

// Convert using the ToPreciseNumber() extension method  
var preciseInt = originalInt.ToPreciseNumber();  
var preciseDouble = originalDouble.ToPreciseNumber();  
var preciseDecimal = originalDecimal.ToPreciseNumber();  
var preciseBigInt = originalBigInt.ToPreciseNumber();  

// Perform precise calculations if needed  
preciseInt *= 10;  
preciseDouble += PreciseNumber.Pi;  

// Convert back FROM PreciseNumber TO standard types using To<T>()  
int roundTripInt = preciseInt.To<int>();                  // 420  
double roundTripDouble = preciseDouble.To<double>();      // ~6.28318  
decimal roundTripDecimal = preciseDecimal.To<decimal>();  // 1234.5678  
BigInteger roundTripBigInt = preciseBigInt.To<BigInteger>(); // 123456789012345678901234567890  

// Verify round-trip conversion (for values that weren't modified)  
Console.WriteLine(originalDecimal == roundTripDecimal);   // True  
Console.WriteLine(originalBigInt == roundTripBigInt);     // True  

Mathematical Functions

PreciseNumber supports a wide range of mathematical operations:

using ktsu.PreciseNumber;  

var number = 2.5.ToPreciseNumber();  

// Exponentiation  
var squared = number.Squared();  // 6.25  
var cubed = number.Cubed();      // 15.625  
var toThe4th = number.Pow(4.ToPreciseNumber());  // 39.0625  

// Constants  
var pi = PreciseNumber.Pi;  
var e = PreciseNumber.E;  

// Exponential function  
var expValue = PreciseNumber.Exp(1.ToPreciseNumber()); // e^1 = e  

// Rounding and precision control  
var roundedValue = number.Round(1);  // 2.5 (already at 1 decimal place)  
var reducedValue = number.ReduceSignificance(1); // 3 (reduced to 1 significant digit)  

// Min, Max, Abs, and Clamp  
var absValue = (-5).ToPreciseNumber().Abs();  // 5  
var maxValue = PreciseNumber.Max(2.ToPreciseNumber(), 3.ToPreciseNumber()); // 3  
var minValue = PreciseNumber.Min(2.ToPreciseNumber(), 3.ToPreciseNumber()); // 2  
var clampedValue = 10.ToPreciseNumber().Clamp(0, 5); // 5 (clamped to maximum)  

Parsing and Formatting

Parsing from Strings

using System.Globalization;  
using System.Numerics;  
using ktsu.PreciseNumber;  

// Parse from string using various formats  
var number1 = PreciseNumber.Parse("123.456", CultureInfo.InvariantCulture);  
var number2 = PreciseNumber.Parse("1.23E4", NumberStyles.Any, CultureInfo.InvariantCulture);  

// Try parsing with error handling  
if (PreciseNumber.TryParse("456.789", out var result))  
{  
    Console.WriteLine($"Parsed successfully: {result}");  
}  

String Formatting

Convert PreciseNumber to string:

using ktsu.PreciseNumber;  

var number = 123.456.ToPreciseNumber();  
string formatted = number.ToString(); // "123.456"  

Comparison with Built-in Types

PreciseNumber vs. double/float

  • Advantages of PreciseNumber:*
  • No Rounding Errors: Unlike floating-point types, PreciseNumber doesn't suffer from binary representation issues (e.g., 0.1 + 0.2 ≠ 0.3 in floating point)

  • Arbitrary Precision: Not limited to 15-17 significant digits (double) or 6-9 significant digits (float)

  • Consistent Results: Mathematical operations produce identical results regardless of magnitude

  • No Special Values: PreciseNumber doesn't have NaN or Infinity values that can propagate through calculations

// Double arithmetic issue  
double a = 0.1;  
double b = 0.2;  
Console.WriteLine(a + b == 0.3);  // False (equals 0.30000000000000004)  

// PreciseNumber solves this  
var pa = 0.1.ToPreciseNumber();  
var pb = 0.2.ToPreciseNumber();  
Console.WriteLine((pa + pb) == 0.3.ToPreciseNumber());  // True (exactly 0.3)  

PreciseNumber vs. decimal

  • Advantages of PreciseNumber:*
  • Unlimited Range: Not constrained by decimal's ±7.9E±28 range

  • Unlimited Precision: Decimal is limited to 28-29 significant digits

  • Scientific Operations: Better suited for scientific calculations requiring extreme precision

  • More Flexible Format: Exponent-significand model makes it suitable for both very large and very small numbers

// Decimal range/precision limitations  
decimal largeDecimal = 1.0m;  
for (int i = 0; i < 30; i++)  
    largeDecimal *= 10; // Will throw OverflowException  

// PreciseNumber handles this easily  
var largePrecise = PreciseNumber.One;  
for (int i = 0; i < 1000; i++)  
    largePrecise *= 10; // Works fine with arbitrary large values  

PreciseNumber vs. BigInteger

  • Advantages of PreciseNumber:*
  • Decimal Point Support: Represents both integer and fractional parts while BigInteger only handles integers

  • Scientific Notation: More convenient for very large or small numbers with fraction components

  • Mathematical Constants: Built-in support for constants like Pi and E with high precision

Technical Details

Internal Representation

PreciseNumber stores values in the form: significand × 10^exponent

  • Significand: A BigInteger that contains all the significant digits

  • Exponent: An int that determines the decimal place

This representation allows for:

  • Exact representation of integers of any size

  • High precision for decimal values

  • Accurate arithmetic without floating-point errors

Precision Control

You can control precision using:

  • Round(): Rounds to a specific number of decimal places

  • ReduceSignificance(): Reduces to a specific number of significant digits

Limitations

  • Operations that inherently require approximation (like certain roots or logarithms) fall back to double precision for calculation

  • Conversion to standard types may throw OverflowException if the value is too large

API Reference

PreciseNumber Class

  • Constants: Zero, One, NegativeOne, Pi, E, Tau

  • Arithmetic: +, -, *, /, %, ++, --

  • Comparison: ==, !=, <, >, <=, >=

  • Functions: Abs(), Round(), Clamp(), Squared(), Cubed(), Pow(), Exp()

  • Utility: ToString(), Parse(), TryParse(), To<T>()

PreciseNumberExtensions Class

  • Conversion: ToPreciseNumber<T>()extension method for anyINumber<T>

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

Acknowledgements

Thanks to the .NET community and ktsu.dev contributors for their support.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ktsu.PreciseNumber:

Package Downloads
ktsu.SignificantNumber

High-precision arithmetic class representing numbers with a significand and exponent. Supports significant figure rules, mathematical computations, and formatting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.7.2 36 9/3/2025
1.7.2-pre.17 130 5/20/2025
1.7.2-pre.15 79 5/17/2025
1.7.2-pre.14 129 5/16/2025
1.7.2-pre.13 207 5/15/2025
1.7.2-pre.12 205 5/14/2025
1.7.2-pre.11 207 5/13/2025
1.7.2-pre.10 237 5/12/2025
1.7.2-pre.9 169 5/11/2025
1.7.2-pre.8 111 5/10/2025
1.7.2-pre.7 56 5/9/2025
1.7.2-pre.6 125 5/8/2025
1.7.2-pre.5 125 5/7/2025
1.7.2-pre.4 125 5/6/2025
1.7.2-pre.3 125 5/5/2025
1.7.2-pre.2 123 5/4/2025
1.7.2-pre.1 126 5/4/2025
1.7.1 165 5/4/2025
1.7.1-pre.1 60 4/26/2025
1.7.0 326 4/13/2025
1.6.0 160 4/13/2025
1.5.0 142 4/13/2025
1.4.0 144 4/13/2025
1.3.0 145 4/13/2025
1.2.0 147 4/13/2025
1.1.2-pre.1 123 4/4/2025
1.1.1 160 3/30/2025
1.1.0 161 3/30/2025

## v1.7.2 (patch)

Changes since v1.7.1:

- Update configuration files and scripts for improved build and test processes ([@matt-edmondson](https://github.com/matt-edmondson))
- Update sdk ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.7.2-pre.17 (prerelease)

Changes since v1.7.2-pre.16:

- Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .mailmap ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .gitignore ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .gitattributes ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .runsettings ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.7.2-pre.16 (prerelease)

Changes since v1.7.2-pre.15:

- Sync .gitignore ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .mailmap ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .runsettings ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .gitattributes ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.7.2-pre.15 (prerelease)

Changes since v1.7.2-pre.14:
## v1.7.2-pre.14 (prerelease)

Changes since v1.7.2-pre.13:
## v1.7.2-pre.13 (prerelease)

Changes since v1.7.2-pre.12:
## v1.7.2-pre.12 (prerelease)

Changes since v1.7.2-pre.11:
## v1.7.2-pre.11 (prerelease)

Changes since v1.7.2-pre.10:
## v1.7.2-pre.10 (prerelease)

Changes since v1.7.2-pre.9:
## v1.7.2-pre.9 (prerelease)

Changes since v1.7.2-pre.8:
## v1.7.2-pre.8 (prerelease)

Changes since v1.7.2-pre.7:
## v1.7.2-pre.7 (prerelease)

Changes since v1.7.2-pre.6:
## v1.7.2-pre.6 (prerelease)

Changes since v1.7.2-pre.5:
## v1.7.2-pre.5 (prerelease)

Changes since v1.7.2-pre.4:
## v1.7.2-pre.4 (prerelease)

Changes since v1.7.2-pre.3:
## v1.7.2-pre.3 (prerelease)

Changes since v1.7.2-pre.2:
## v1.7.2-pre.2 (prerelease)

Changes since v1.7.2-pre.1:
## v1.7.2-pre.1 (prerelease)

Changes since v1.7.1:

- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.7.1 (patch)

Changes since v1.7.0:

- Remove obsolete build configuration files and update PreciseNumber class for improved variable declarations and copyright information. ([@matt-edmondson](https://github.com/matt-edmondson))
- Update README and project files ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.7.1-pre.1 (prerelease)

Incremental prerelease update.
## v1.7.0 (minor)

Changes since v1.6.0:

- Enhance PreciseNumber derived classes ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.6.0 (minor)

Changes since v1.5.0:

- Enhance type check in PreciseNumber conversion method ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.5.0 (minor)

Changes since v1.4.0:

- Improve type checking in ToPreciseNumber and TryCreate ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.4.0 (minor)

Changes since v1.3.0:

- Update TryParse methods to use nullable PreciseNumber ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.3.0 (minor)

Changes since v1.2.0:

- Enhance PreciseNumber functionality and tests ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.2.0 (minor)

Changes since v1.1.0:

- Added documentation in README.md ([@matt-edmondson](https://github.com/matt-edmondson))
- Update PreciseNumber properties to public access ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.1.2-pre.1 (prerelease)

Changes since v1.1.1:

- Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.1.1 (patch)

Changes since v1.1.0:

- Added documentation in README.md ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.1.0 (minor)

Changes since v1.0.0-alpha.4:

- Update license file format and content ([@matt-edmondson](https://github.com/matt-edmondson))
- Update namespace to ktsu.PreciseNumber ([@matt-edmondson](https://github.com/matt-edmondson))
- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))
- Change ReduceSignificance to public ([@matt-edmondson](https://github.com/matt-edmondson))
- Renamed metadata files ([@matt-edmondson](https://github.com/matt-edmondson))
- Update PreciseNumber.sln with new test project path ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix code style violations and add more test coverage ([@matt-edmondson](https://github.com/matt-edmondson))
- Replace LICENSE file with LICENSE.md and update copyright information ([@matt-edmondson](https://github.com/matt-edmondson))
- Rename Tests dir to PreciseNumber.Test ([@matt-edmondson](https://github.com/matt-edmondson))
- Add PreciseNumber.Test project and update solution structure ([@matt-edmondson](https://github.com/matt-edmondson))
- Add automation scripts for metadata management and versioning ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.0-alpha.4 (prerelease)

Changes since v1.0.0-alpha.3:

- Add new PreciseNumber constructor overload ([@matt-edmondson](https://github.com/matt-edmondson))
- 1.0.0-alpha.4 ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix accessibility for tests ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.0-alpha.3 (prerelease)

Changes since 1.0.0.2:

- Refactor and enhance PreciseNumber class ([@matt-edmondson](https://github.com/matt-edmondson))
- Add project files. ([@matt-edmondson](https://github.com/matt-edmondson))
- Update description ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance PreciseNumber subclassing support ([@matt-edmondson](https://github.com/matt-edmondson))
- 1.0.0-alpha.3 ([@matt-edmondson](https://github.com/matt-edmondson))
- Add .runsettings and update build props ([@matt-edmondson](https://github.com/matt-edmondson))
- 1.0.0-alpha.2 ([@matt-edmondson](https://github.com/matt-edmondson))