Sdcb.Arithmetic 1.1.0

Prefix Reserved
dotnet add package Sdcb.Arithmetic --version 1.1.0
                    
NuGet\Install-Package Sdcb.Arithmetic -Version 1.1.0
                    
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="Sdcb.Arithmetic" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sdcb.Arithmetic" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Sdcb.Arithmetic" />
                    
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 Sdcb.Arithmetic --version 1.1.0
                    
#r "nuget: Sdcb.Arithmetic, 1.1.0"
                    
#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 Sdcb.Arithmetic@1.1.0
                    
#: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=Sdcb.Arithmetic&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Sdcb.Arithmetic&version=1.1.0
                    
Install as a Cake Tool

Sdcb.Arithmetic CI NuGet License: MIT QQ

English | 中文

Looking for the GMP/MPFR P/Invoke bindings (Sdcb.Arithmetic.Gmp / Sdcb.Arithmetic.Mpfr) that used to live in this repository? They moved to sdcb/Sdcb.Arithmetic.

High-performance, pure managed C# arbitrary-precision arithmetic with GMP/MPFR-class functionality and no native dependencies. It runs anywhere .NET 10 runs (x64, ARM64, AOT), uses AVX-512/AVX2/SSE/NEON where available, and falls back to portable code elsewhere.

Type Models Description
BigInt GMP mpz_t Immutable arbitrary-precision signed integer
BigRational GMP mpq_t Immutable rational, always canonical
Real MPFR mpfr_t Correctly-rounded binary floating point: five rounding modes, NaN/±∞/±0, 100+ functions

All three implement .NET generic math (IBinaryInteger<T>, INumber<T>, IBinaryFloatingPointIeee754<T>) and interoperate with System.Numerics.BigInteger.

Install

dotnet add package Sdcb.Arithmetic

Quick start

using Sdcb.Arithmetic;

// Integers (mpz)
BigInt a = BigInt.Parse("123456789012345678901234567890");
BigInt p = BigInt.Pow(2, 521) - 1;                  // Mersenne prime M521
bool prime = p.IsProbablePrime();                   // BPSW + Miller-Rabin
BigInt g = BigInt.Gcd(a, p);
BigInt m = BigInt.ModPow(a, p, BigInt.Pow(10, 30));
Console.WriteLine(p.ToString(16));                  // any base 2..62

// Rationals (mpq), always canonical
BigRational r = new BigRational(1, 3) + new BigRational(1, 6); // 1/2
double d = r.ToDouble();                            // correctly rounded

// Correctly-rounded floating point (mpfr)
Real.DefaultPrecision = 256;                        // bits
Real pi = Real.Pi();
Real y = Real.Sin(Real.Parse("1.5"));
Real z = Real.Exp(y, precision: 512, RealRounding.TowardPositive);
Console.WriteLine(z);                               // round-trip decimal output

Performance

AMD Ryzen 7 5800X (AVX2), .NET 10, operand sizes in 64-bit limbs. Full tables and methodology: docs/performance.md.

Operation Speedup over BigInteger Time vs native GMP 6.3 (lower is better)
Multiply 1.9–2.8× (≤ 16 limbs), 3.7–5× (32–2048), 7–28× (4096–65536) 1.3–1.6× (≥ 16 limbs)
Divide 2N/N 2.2–4.3× (16–4096 limbs), 5.9× (16384) 1.5–2.6×
Modular power 4.2–7× 1.3–2.8×
GCD 1.5–5.8× 0.9–1.4×
Add / sub / shift 2.5–11× 1.1–3.1×
Decimal ToString / Parse up to 16× / 4× (large); ToString 0.9× at ≤ 4 limbs 0.5–1.9× / 0.6–1.4×

Real against native MPFR 4.2 (53–16384 bits, both sides correctly rounded): add/mul 1.3–2.5×, div/sqrt 1.3–3.3×, exp/sin/atan 0.9–1.8×, log/pow 1.6–2.7×.

Every BigInt operation is cross-checked against BigInteger and native GMP, and every Real function bit-for-bit against MPFR in all rounding modes.

Documentation

WeChat group

alternate text is missing from this package README image

If the WeChat QR code has expired, join the QQ group .NET骚操作 495782587.

License

MIT. Sdcb.Arithmetic is an independent C# implementation of published algorithms and contains no GMP or MPFR source code. Native GMP/MPFR are used only as black-box references for testing and benchmarking. See docs/references.md for the literature.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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.

Version Downloads Last Updated
1.1.0 63 10/26/2026
1.0.0 57 10/25/2026