FLINT3.7.0-dev

n_mod_precomp

Synopsis#include <flint/ulong_extras.h>
ulong n_mod_precomp(ulong a, ulong n, double ninv)source

Returns a \bmod n. Requires n < 2^{53}, a < n^2 and a < 2^{\mathtt{FLINT\_BITS} - 1}.

n_mod_precomp takes the quotient to be a * ninv truncated to an integer, and corrects the remainder by at most one multiple of n. This suffices: the processor is assumed to round to nearest, so ninv is correct to 53 bits (call the weight of the last bit a place) and is at most half a place out. Since a is exact and the mantissa of ninv is below 2, the product a * ninv is at most one place out; truncating it to an integer gives either the exact quotient or one less, because half a place is less than 1 when the product is below n < 2^{53}. The only other case is that the product was rounded up to an integer, which implies that a/n lies within 2^{-54} of an integer; this is handled by subtracting 1 from the quotient. The computed quotient is therefore exact or one too small, and a single correction step suffices.

Cite this page

The FLINT team, “n_mod_precomp (ulong_extras.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/ulong_extras/n_mod_precomp.html (generated 2026-10-20).