| Crates.io | pymath |
| lib.rs | pymath |
| version | 0.1.5 |
| created_at | 2025-04-22 04:42:52.147816+00 |
| updated_at | 2026-01-13 07:06:02.475245+00 |
| description | A binary representation compatible Rust implementation of Python's math library. |
| homepage | |
| repository | https://github.com/RustPython/pymath |
| max_upload_size | |
| id | 1643624 |
| size | 232,259 |
0 ULP (bit-exact) compatibility with CPython's math and cmath modules.
Every function produces identical results to Python at the binary representation level - not just "close enough", but exactly the same bits.
pymath is a strict port of CPython's math library to Rust. Each function has been carefully translated from CPython's C implementation, preserving the same algorithms, constants, and corner case handling.
acosacoshasinasinhatanatan2atanhcbrtceilcopysigncoscoshdegreesdisteerferfcexpexp2expm1fabsfloorfmafmodfrexpfsumgammahypot (n-dimensional)infiscloseisfiniteisinfisnanldexplgammaloglog10log1plog2modfnannextafterpipowprod (prod, prod_int)radiansremaindersinsinhsqrtsumprod (sumprod, sumprod_int)tantanhtautrunculpnum-bigint or malachite-bigint feature)combfactorialgcdisqrtlcmpermcomplex feature)absacosacoshasinasinhatanatanhcoscosheexpinfinfjiscloseisfiniteisinfisnanloglog10nannanjphasepipolarrectsinsinhsqrttantanhtauuse pymath::math::{sqrt, gamma, lgamma};
fn main() {
let sqrt_result = sqrt(2.0).unwrap();
let gamma_result = gamma(4.5).unwrap();
let lgamma_result = lgamma(4.5).unwrap();
}
# Python 3.14
>>> import math
>>> math.gamma(0.5).hex()
'0x1.c5bf891b4ef6ap+0'
// Rust - identical bits
assert_eq!(
pymath::math::gamma(0.5).unwrap().to_bits(),
0x3ffc5bf891b4ef6a
);
Bit representation may vary across platforms, but CPython and pymath built on the same environment will always produce identical results.
complex (default) - Enable cmath module for complex number functionsnum-bigint - Enable integer functions using num-bigintmalachite-bigint - Enable integer functions using malachite-bigintmul_add - Use hardware FMA for bit-exact macOS compatibilitypymath::math - Real number math functions (Python's math module)pymath::cmath - Complex number functions (Python's cmath module)pymath::m - Direct libm bindingsThis library guarantees compatibility with Python's math module, not mathematical correctness.