Skip to content

Releases: sdcb/Arithmetic

Sdcb.Arithmetic 1.1.0

Choose a tag to compare

@sdcb sdcb released this 26 Oct 10:34

Sdcb.Arithmetic 1.1.0

dotnet add package Sdcb.Arithmetic --version 1.1.0

性能改进:Real.Pi 提速约 3–5 倍

高精度(≥ 2000 位)下的 Real.Pi 由 Brent–Salamin AGM 改为 Chudnovsky 级数 + 二分拆分(binary splitting)。AGM 每一轮都要做一次全精度开方与乘法,约需 log₂(n) 轮;Chudnovsky 每一项贡献约 14 位十进制数字,整体只需一次除法和一次开方。

π 精度 1.0.0(AGM) 1.1.0(Chudnovsky)
262,144 bit(≈ 78,913 位十进制) ~0.4 s ~0.1 s
100 万位十进制(≈ 3.32M bit),冷启动含 JIT 2.72 s 0.83 s
100 万位十进制,预热后 — 0.51 s

(x64 原生 .NET 10 测得;浏览器 WebAssembly 下同样受益。)

正确性

  • 结果依旧是正确舍入(Ziv 策略不变),五种舍入模式全部适用。
  • 全部 204 个单元测试通过;与 MPFR mpfr_const_pi 的逐位比对(含 4097 / 9000 bit)全部一致。
  • 另在 2001 – 3,400,000 bit 的 13 个精度上,以 TowardZero / TowardPositive / NearestEven 与独立的高精度参考值比对,全部正确舍入。

其他

  • API 无任何变化,可直接升级。
  • 文档(docs/real.md、docs/performance.md、docs/references.md)与 pi.linq 示例已同步更新。

完整变更:v1.0.0...v1.1.0

v1.0.0

Choose a tag to compare

@sdcb sdcb released this 25 Oct 15:56

Sdcb.Arithmetic 1.0.0

First stable release of the pure managed C# rewrite of Sdcb.Arithmetic: high-performance arbitrary-precision integers, rationals and correctly-rounded floating point, with no native dependencies.

dotnet add package Sdcb.Arithmetic

Relationship to the previous Sdcb.Arithmetic

The previous generation, sdcb/Sdcb.Arithmetic, is a set of P/Invoke bindings (Sdcb.Arithmetic.Gmp / Sdcb.Arithmetic.Mpfr) over native GMP and MPFR. It needs native runtime packages per platform, and those native libraries are LGPL.

This repository is a from-scratch, 100% C# implementation:

  • Pure managed, no native libraries. Runs anywhere .NET 10 runs: x64, ARM64, AOT, with no runtime.* packages to ship.
  • MIT licensed. It is an independent implementation of published algorithms and contains no GMP or MPFR source code, so there is no LGPL obligation. Native GMP/MPFR are used only as black-box references for tests and benchmarks.
  • Slightly slower than native. It is a managed implementation, so expect a small gap to GMP/MPFR. It is already faster than System.Numerics.BigInteger in most cases. See the numbers below.
  • The old P/Invoke bindings remain available in the old repository if you specifically need the native libraries.

What's included

Type Models Description
BigInt GMP mpz_t Immutable arbitrary-precision signed integer: arithmetic, bitwise ops, GCD, modular power, primality (BPSW + Miller-Rabin), combinatorics, random numbers, string conversion in any base 2..62
BigRational GMP mpq_t Immutable rational number, always kept in canonical form
Real MPFR mpfr_t Correctly-rounded binary floating point with arbitrary precision: five rounding modes, NaN / ±∞ / ±0, 100+ functions (elementary, transcendental, hyperbolic, Gamma/Beta, Bessel, Airy, erf, and more)
  • All three types implement .NET generic math (IBinaryInteger<T>, INumber<T>, IBinaryFloatingPointIeee754<T>) and interoperate with System.Numerics.BigInteger.
  • SIMD acceleration with AVX-512 / AVX2 / SSE / NEON where available, with portable fallbacks elsewhere.
  • Large-number algorithms include NTT multiplication, Burnikel-Ziegler and Newton division, and Montgomery modular arithmetic.

Quick start

using Sdcb.Arithmetic;

BigInt p = BigInt.Pow(2, 521) - 1;          // Mersenne prime M521
bool prime = p.IsProbablePrime();

BigRational r = new BigRational(1, 3) + new BigRational(1, 6); // 1/2

Real.DefaultPrecision = 256;                // bits
Real pi = Real.Pi();
Real z = Real.Exp(Real.Sin(Real.Parse("1.5")), precision: 512, RealRounding.TowardPositive);

Performance

Measured on AMD Ryzen 7 5800X (AVX2), .NET 10. Full tables and methodology are in docs/performance.md.

Operation Speedup over BigInteger Time vs native GMP 6.3 (lower is better)
Multiply 1.9-28x depending on size 1.3-1.6x (>= 16 limbs)
Divide 2N/N 2.2-5.9x 1.5-2.6x
Modular power 4.2-7x 1.3-2.8x
GCD 1.5-5.8x 0.9-1.4x

Real against native MPFR 4.2 (53-16384 bits, both correctly rounded): add/mul 1.3-2.5x, div/sqrt 1.3-3.3x, exp/sin/atan 0.9-1.8x, log/pow 1.6-2.7x of MPFR's time. So expect results in the same ballpark as native, slightly slower.

Correctness

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

Documentation

中文简介

Sdcb.Arithmetic 1.0.0 是纯 C# 重写的高性能任意精度算术库,提供 BigInt(整数)、BigRational(有理数)和 Real(正确舍入的任意精度浮点,5 种舍入模式,NaN/±∞/±0,100+ 函数)。

前身是 sdcb/Sdcb.Arithmetic,那是对原生 GMP/MPFR 的 P/Invoke 封装,需要按平台带原生库(LGPL)。这一版:

  • 纯 C#,无任何原生依赖,x64 / ARM64 / AOT 都能跑
  • MIT 开源,是对公开算法的独立实现,不含 GMP/MPFR 源码,没有 LGPL 约束
  • 性能比原生略差一点:多数运算约为原生 GMP/MPFR 耗时的 1-3 倍,但大多数场景仍明显快于 System.Numerics.BigInteger
  • 实现了 .NET 泛型数学接口,并使用 AVX-512/AVX2/SSE/NEON 加速
  • 所有运算都与 BigInteger、原生 GMP/MPFR 做过对拍验证