FLINT3.7.0-dev
Module · 749 functions

decimal.h

#include <flint/decimal.h>

This module implements decimal floating-point arithmetic and decimal ball arithmetic of arbitrary precision: real numbers (decfloat_t), real balls (decball_t), complex numbers (deccfloat_t) and complex balls (deccball_t), together with the radius type decmag_t. Precision is counted in decimal digits, and the floating-point operations are correctly rounded to that many significant digits in one of seven rounding modes, with unbounded precision and exponents. Many of the operations and ideas follow the IEEE 754 standard for decimal arithmetic and the General Decimal Arithmetic specification [IEEE754] [Cow2009], but the module is not an implementation of either. Mantissas are integers in the radix B = 10^e of the radix.h module (e = 19 on 64-bit machines), so that rounding to a number of digits and conversion to and from decimal strings are cheap.

The module is built on the generic rings interface (gr.h): every type has a context object (gr_ctx_t) holding the precision, the rounding modes, the radius precision, exponent limits and flags, every function returns a status (GR_SUCCESS, GR_DOMAIN or GR_UNABLE), and the generic structures such as gr_poly_t and gr_mat_t and the generic special functions (gr_gamma, gr_zeta, ...) work over the decimal rings. The elementary and special functions are evaluated through arb and acb and rounded correctly by Ziv's strategy [Ziv1991], with the exact cases and extreme arguments detected separately.

The guide Getting started with decimal arithmetic introduces the contexts and the four types; Correctly rounded arithmetic and Correctly rounded elementary and special functions describe correct rounding of the arithmetic and the functions, and Decimal ball arithmetic the ball arithmetic. The algorithms are described in Decimal floating-point arithmetic.

Guides#

Representation of numbers#

A finite nonzero decimal floating-point number is

x = (-1)^s\, M\, B^v, \qquad B = 10^e,

where the mantissa M \ge 1 is a radix_integer_t with limbs in the radix B (its sign holds s) and the exponent v is an fmpz counted in limbs. Mantissas are canonical: zero limbs are stripped at both ends, so equal values have identical representations. The precision of a number is its number of significant digits, \operatorname{sd}(x) = \operatorname{digits}(M) - \nu_{10}(M_0), where the trailing zero digits of the lowest limb do not count; all rounding operations give results with at most prec significant digits, which is the classical definition of decimal arithmetic to prec digits. Because the exponent is counted in limbs, additions align operands without digit shifts and rounding costs O(1) beyond dropping limbs; the price is that a prec-digit number may occupy one limb more than \lceil \mathit{prec}/e \rceil. Special values \pm\infty and NaN are encoded by a zero mantissa with a code in the exponent field; zero is unsigned.

AlgorithmDecimal floating-point arithmeticCorrectly rounded decimal arithmetic with limb-aligned exponents in radix 10^19: rounding with sticky bits, sums of far-apart operands, truncated products with interval rounding, division and square roots, decimal radii, conversions to and from binary, and correctly rounded functions by Ziv's strategy with detection of the hard cases.

Contexts#

Every decimal ring is described by a gr_ctx_t. The context determines the type of the elements (real or complex, floating-point numbers or balls), the limb radix, and the settings that control rounding: the precision, the rounding modes, the radius precision, the exponent limits and the flags. The settings can be changed at any time (see Operands and results), and contexts are cleared with gr_ctx_clear.

The kind of a decimal ring: DECIMAL_CTX_FLOAT, DECIMAL_CTX_BALL, DECIMAL_CTX_CFLOAT or DECIMAL_CTX_CBALL.

The data of a decimal context, accessed through the macros DECIMAL_CTX_RADIX, DECIMAL_CTX_PREC, DECIMAL_CTX_RND, DECIMAL_CTX_RND_IM, DECIMAL_CTX_FLAGS, DECIMAL_CTX_RAD_PREC, DECIMAL_CTX_EMIN, DECIMAL_CTX_EMAX, DECIMAL_CTX_E (the limb exponent e) and DECIMAL_CTX_B (the limb radix B). The macros DECIMAL_CTX_WHICH, DECIMAL_CTX_IS_BALL and DECIMAL_CTX_IS_COMPLEX identify the kind of the elements.

void gr_ctx_init_decfloat(gr_ctx_t ctx, slong prec, int flags)
void gr_ctx_init_decball(gr_ctx_t ctx, slong prec, int flags)
void gr_ctx_init_deccfloat(gr_ctx_t ctx, slong prec, int flags)
void gr_ctx_init_deccball(gr_ctx_t ctx, slong prec, int flags)

Initializes ctx to the ring of decimal floating-point numbers, decimal balls, complex decimal floating-point numbers, respectively complex decimal balls, with default precision prec digits (at least 1) and the given flags. The default rounding mode is DECIMAL_RND_NEAR (for both real and imaginary parts) and the radius precision is DECMAG_DEFAULT_PREC digits. Exponents are unbounded. The largest possible limb radix is used.

More
void _gr_ctx_init_decimal(gr_ctx_t ctx, decimal_ctx_which which, unsigned int e, slong prec, int rnd, int flags)

Generalized initialization where which is DECIMAL_CTX_FLOAT, DECIMAL_CTX_BALL, DECIMAL_CTX_CFLOAT or DECIMAL_CTX_CBALL, e is the limb exponent (B = 10^e, or e = 0 to select the largest possible), and rnd is the default rounding mode. Elements of contexts with different limb exponents are not interchangeable (but can be converted with gr_set_other).

Initializes a random context (random limb exponent, precision, rounding modes, radius precision, flags and exponent limits).

void decimal_ctx_set_rnd(gr_ctx_t ctx, int rnd)
void decimal_ctx_set_rnd_im(gr_ctx_t ctx, int rnd)
void decimal_ctx_set_flags(gr_ctx_t ctx, int flags)

Changes the default precision, the default rounding mode, the radius precision (clamped to [1, R] where R = DECMAG_MAX_PREC is 9 on a 64-bit machine and 4 on a 32-bit machine), the exponent limits and the flags of a context. Exponent limits apply to the scientific exponent E of a number x, 10^E \le |x| < 10^{E+1}; WORD_MIN and WORD_MAX denote the absence of a limit. All settings can be changed at any time; see Operands and results.

More

Operands and results#

Operands of an operation need not have been produced by the context (or by a context with the same settings) performing the operation. The requirements are only that the operands have the same limb radix as the context (elements of contexts with different limb exponents e are converted with gr_set_other) and that they are elements of the ring the context represents:

  • midpoints and floating-point numbers may have any number of digits (the context only rounds results to its precision);
  • radii may have any radius precision: the arithmetic on radii rounds operands to the radius precision of the context (in the direction which preserves the bound) and normalizes its results to it, so changing the radius precision of a context is always safe;
  • the exponent limits apply to results only, not to operands (an operation can take an operand with an exponent out of bounds and return a result within the bounds);
  • infinities and NaN are elements of the floating-point rings created with the DECIMAL_ALLOW_INF and DECIMAL_ALLOW_NAN flags, and of nothing else. Passing an infinity to a floating-point context without DECIMAL_ALLOW_INF returns GR_DOMAIN and passing a NaN to one without DECIMAL_ALLOW_NAN returns GR_UNABLE, even where the result would be finite (1/\infty, \infty^0, comparisons); predicates returning truth_t (which have no error channel) treat NaN as unknown and infinities as ordinary values. Balls represent real (or complex) numbers only: their midpoints are always finite, and an operation whose result would be infinite or undefined (\log 0, \Gamma(0), 1/0, a midpoint overflowing the exponent limits) returns GR_DOMAIN or GR_UNABLE. A ball with an infinite radius is the whole real line (-\infty, \infty) and is returned whenever nothing better is known (for example when an intermediate arb computation overflows).

Functions on radii (decmag_t) take a context argument but are not gr methods; all their names carry a leading underscore. The same convention is used for predicates and comparisons which return an int or slong result directly instead of following the gr conventions (a truth_t, or a status code with the result in an output argument): _decfloat_cmp versus decfloat_cmp, or _decball_contains, _decball_is_positive, _decfloat_is_finite.

Accessors for the corresponding settings of a context; the limb digits are the number e such that the internal limb radix is 10^e.

The gr methods for clearing a decimal context (called by gr_ctx_clear) and for printing a description of it (called by gr_ctx_write), such as Decimal floating-point numbers (prec 30, rnd near).

The gr methods ctx_is_vector_space (true for rounding contexts, which behave as real vector spaces, and false for the exact ring) and ctx_set_real_prec, ctx_get_real_prec, with the conversion between bits and digits described under decimal_ctx_set_prec.

Rounding modes#

Rounding toward zero, away from zero, toward -\infty, toward +\infty, to nearest with ties to even, to nearest with ties away from zero, and to nearest with ties toward zero. All operations are correctly rounded in the selected mode.

Special precision value indicating that no rounding should be performed.

Flags#

Admit infinities, respectively NaN, as floating-point numbers (the flags have no effect in ball contexts). Without these flags, operations that would produce such values (overflow when exponent limits are set, 1/0, \infty - \infty) return GR_DOMAIN or GR_UNABLE.

Flush results whose exponent falls below the lower exponent limit to zero. Without this flag such results return GR_UNABLE. In the ball ring, a flushed midpoint is accompanied by an increase of the radius by 10^{e_{\min}} so that the ball remains valid.

In the ball ring, bound the rounding error of each midpoint operation by one ulp (half an ulp in the nearest rounding modes) instead of computing a tight upper bound (the exact error rounded up to the radius precision), which is the default. The tight bound costs essentially nothing extra, so this flag is mainly for testing.

Print numbers in scientific notation (except for numbers with scientific exponent zero, such as 1 and -2.5, so that generators of polynomial rings print as expected).

Floating-point numbers#

A decimal floating-point number. The mantissa is the m field (a radix_integer_struct) and the limb exponent is the exp field (an fmpz).

Memory management and assignment#

Memory management. decfloat_init sets res to zero without allocating; decfloat_set_shallow copies the structure without copying the mantissa, so that only one of the two may be cleared.

Constants. The special values return GR_DOMAIN (infinities) or GR_UNABLE (NaN) if not admitted by the context.

int decfloat_set_d(decfloat_t res, double x, gr_ctx_t ctx)
int decfloat_set_str(decfloat_t res, const char * s, gr_ctx_t ctx)
int decfloat_set_arf(decfloat_t res, const arf_t x, gr_ctx_t ctx)

Set res to the given value, correctly rounded to the context precision in the context rounding mode. Strings may be plain decimal literals ("-12.5e-3", "inf", "nan") or arithmetic expressions ("1/3"), the latter evaluated by the generic expression parser with every operation rounded. decfloat_set_other converts elements of other rings.

More
int decfloat_set_round_qqbar(decfloat_t res, const qqbar_t x, slong prec, int rnd, gr_ctx_t ctx)

Converts the algebraic number x; these are also used by set_other for elements of the qqbar rings. The prototypes are available when qqbar.h is included (before or after decimal.h). The real and imaginary parts are converted separately: floating-point results are correctly rounded (the imaginary part of a deccfloat in the imaginary rounding mode), and a ball part is exact (has zero radius) whenever the part is a decimal number of at most the context precision that is within the exponent range; for example, \sqrt{2} + 0.0023 i gives an exact imaginary part at a precision of two or more digits. The real conversions return GR_DOMAIN for a nonreal x.

More
int decfloat_set_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_si(decfloat_t res, slong x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_ui(decfloat_t res, ulong x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_fmpz(decfloat_t res, const fmpz_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_fmpq(decfloat_t res, const fmpq_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_d(decfloat_t res, double x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_str(decfloat_t res, const char * s, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_fmpz_10exp_fmpz(decfloat_t res, const fmpz_t m, const fmpz_t e, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_fmpz_2exp_fmpz(decfloat_t res, const fmpz_t m, const fmpz_t e, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_set_round_arf(decfloat_t res, const arf_t x, slong prec, int rnd, gr_ctx_t ctx)

Versions with an explicit precision and rounding mode. Passing DECIMAL_PREC_EXACT sets the exact value.

int decfloat_set_round_info(decfloat_t res, const decfloat_t x, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)

Rounds x, additionally reporting whether the result is inexact in info and, if err is not NULL, an upper bound for the absolute rounding error in err. Both pointers are optional.

int decfloat_set_round_fmpq_reference(decfloat_t res, const fmpq_t x, slong prec, int rnd, gr_ctx_t ctx)

Reference implementation of correctly rounded conversion using plain fmpz arithmetic, used as an oracle by the test code.

Conversions#

int decfloat_get_si(slong * res, const decfloat_t x, gr_ctx_t ctx)
int decfloat_get_ui(ulong * res, const decfloat_t x, gr_ctx_t ctx)
int decfloat_get_d(double * res, const decfloat_t x, gr_ctx_t ctx)
int decfloat_get_arf(arf_t res, const decfloat_t x, slong prec_bits, int rnd, gr_ctx_t ctx)
int decfloat_get_arb(arb_t res, const decfloat_t x, slong prec_bits, gr_ctx_t ctx)
int decfloat_get_fmpz_fixed_si(fmpz_t res, const decfloat_t x, slong e, int rnd, gr_ctx_t ctx)

Conversions out. The integer conversions return GR_DOMAIN for non-integers and GR_UNABLE for integers with more than DECIMAL_CONV_DIGITS_LIMIT digits. The decomposition x = m \cdot 10^e is returned with m not divisible by 10. decfloat_get_fmpz_fixed_si computes x / 10^e rounded to an integer in the rounding mode rnd.

More

The digit c \in \{0, \ldots, 9\} of |x| at the absolute position 10^k, respectively the limb 0 \le c < B at position B^k (zero outside the mantissa and for special values), and x with that digit or limb replaced by c. Setting is exact (no rounding to the context precision, although the exponent limits are applied); the sign of x is kept, and setting a digit of zero gives a positive number. GR_DOMAIN is returned for an out-of-range c or a special value of x, and GR_UNABLE if the result would have more than DECIMAL_CONV_DIGITS_LIMIT digits.

Conversions between x and integers in the limb radix of the context (DECIMAL_CTX_RADIX(ctx)), in time linear in the number of limbs. The first function requires an integer x (GR_DOMAIN otherwise); the second gives the exact representation x = m B^e with m the canonical mantissa (no zero limbs at either end). The set functions round m or m B^e to the context precision.

char * decfloat_get_str(const decfloat_t x, gr_ctx_t ctx)

Prints x with all its significant digits, either in positional notation (when -6 \le E \le 20 and the context does not select scientific notation, or when E = 0) or in scientific notation (1.2345e-30).

Properties and comparisons#

int decfloat_cmp(int * res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int decfloat_cmpabs(int * res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)

Predicates and comparisons following the gr conventions: predicates return T_UNKNOWN for NaN, comparisons return GR_UNABLE for NaN (and GR_DOMAIN for infinities when the context does not allow them). A number is an integer iff its limb exponent is nonnegative; infinities are not integers.

int _decfloat_cmp(const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int _decfloat_cmpabs(const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)

Raw versions returning the result directly. Comparisons involving NaN return 0.

Writing |x| = M \cdot 10^v with M an integer not divisible by 10: the number of digits of M (the smallest precision at which x is representable; decimal numbers do not track significance, so this is a property of the value), the number of limbs of the mantissa, the number of digits of the limb mantissa including the trailing zeros of its lowest limb, the exponent v (clamped to \pm DECFLOAT_EXP_CLAMP), and the scientific exponent E with 10^E \le |x| < 10^{E+1} (the last function returns 0 if E does not fit in a word). The digit counts are zero for special values; the value must be nonzero and finite for the exponent functions.

Arithmetic#

int decfloat_add(decfloat_t res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int decfloat_sub(decfloat_t res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int decfloat_mul(decfloat_t res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int decfloat_div(decfloat_t res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)
int decfloat_add_fmpz(decfloat_t res, const decfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int decfloat_sub_fmpz(decfloat_t res, const decfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int decfloat_mul_fmpz(decfloat_t res, const decfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int decfloat_div_fmpz(decfloat_t res, const decfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int decfloat_pow_fmpz(decfloat_t res, const decfloat_t x, const fmpz_t y, gr_ctx_t ctx)

Arithmetic operations, correctly rounded to the context precision in the context rounding mode. Integer powers are computed exactly and then rounded once when the exact result has at most 100000 digits (always for powers of ten), and correctly rounded via arb otherwise. Multiplication by 10^e is exact for any e (a digit shift is only needed when e is not a multiple of the limb exponent). The integer rounding functions first round to an integer exactly and then round the integer to the context precision. Division by zero returns GR_DOMAIN unless infinities are admitted; square roots of negative numbers return GR_DOMAIN.

Full documentation
int decfloat_neg_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_abs_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_add_round(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_sub_round(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_mul_round(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_sqr_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_div_round(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_inv_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_sqrt_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_rsqrt_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_mul_10exp_si_round(decfloat_t res, const decfloat_t x, slong e, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_mul_10exp_fmpz_round(decfloat_t res, const decfloat_t x, const fmpz_t e, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_mul_2exp_fmpz_round(decfloat_t res, const decfloat_t x, const fmpz_t e, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_floor_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_ceil_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_trunc_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)
int decfloat_nint_round(decfloat_t res, const decfloat_t x, slong prec, int rnd, gr_ctx_t ctx)

Versions with an explicit precision and rounding mode, allowing variable-precision computations without modifying the context.

Full documentation
int _decfloat_add(decfloat_t res, const decfloat_t x, const decfloat_t y, int negate_y, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_mul(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_div(decfloat_t res, const decfloat_t x, const decfloat_t y, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_sqrt(decfloat_t res, const decfloat_t x, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_mul_10exp(decfloat_t res, const decfloat_t x, const fmpz_t e, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_round_to_int(decfloat_t res, const decfloat_t x, int int_rnd, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)

Underlying implementations which additionally report rounding information (see decfloat_set_round_info). These are used by the ball arithmetic.

Full documentation
int _decfloat_set_round_limbs(decfloat_t res, nn_srcptr d, slong n, int negative, const fmpz_t exp, int eps, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
slong _decimal_round_mantissa(nn_ptr d, slong n, int negative, int eps, slong prec, int rnd, slong * newn, decimal_rounding_info * info, decmag_ptr err, const fmpz_t exp, gr_ctx_t ctx)

The rounding core: sets res to (-1)^{\mathrm{negative}} (d, n) B^{\mathrm{exp}} (plus an unrepresented positive tail smaller than one unit of the lowest limb if eps is set) rounded to prec digits. Limbs of d may be zero at either end. The buffer must have room for n + 1 limbs (a carry may propagate out of the top) and is modified. The second function performs the in-place rounding, returning the number of low limbs to discard.

Full documentation
int decfloat_vec_dot(decfloat_t res, const decfloat_t initial, int subtract, decfloat_srcptr vec1, decfloat_srcptr vec2, slong len, gr_ctx_t ctx)
int decfloat_vec_dot_rev(decfloat_t res, const decfloat_t initial, int subtract, decfloat_srcptr vec1, decfloat_srcptr vec2, slong len, gr_ctx_t ctx)

The gr dot product methods, implemented by the generic gr_generic_vec_dot and gr_generic_vec_dot_rev: each product and each sum is rounded separately, so the result is not correctly rounded.

Full documentation

Structure with fields inexact, increased (whether the magnitude was rounded up), underflow and overflow.

Full documentation

Correct rounding#

The following operations on decfloat are guaranteed to be correctly rounded (to the target precision in the target rounding mode, with the result also being exact whenever the mathematical result is representable):

  • conversions from integers, rationals, arf, arb midpoints, doubles and strings (plain literals), and to arf;
  • add, sub, mul, sqr, div, inv, sqrt, rsqrt, mul_2exp, mul_10exp (exact), floor, ceil, trunc, nint (exact), neg, abs;
  • pow (all cases), pow_ui, pow_si, pow_fmpz;
  • every function listed under Elementary and special functions, subject to the caveat that Ziv's strategy is used for values that are neither exact nor covered by the special-argument handling, and returns GR_UNABLE in the (practically nonexistent) event that the rounding has not been decided at 64 times the working precision.

Operations reached through the generic fallbacks of the gr interface are not correctly rounded, since they are composed of several rounded operations. Examples include: gr_vec_dot and hence all matrix and polynomial arithmetic (each product and sum is rounded separately), gr_pow_fmpq, gr_hypot, gr_fac, gr_bin, gr_sqrt_ui and other functions built generically from the basic operations, power series functions, and gr_set_str applied to an arithmetic expression such as "1/3 + 2/7" (each operation in the expression is rounded).

Elementary and special functions#

Constants, correctly rounded.

Full documentation
int decfloat_atan2(decfloat_t res, const decfloat_t y, const decfloat_t x, gr_ctx_t ctx)
int decfloat_pow(decfloat_t res, const decfloat_t x, const decfloat_t y, gr_ctx_t ctx)

The most common elementary functions, correctly rounded. Versions for the other types (decball_exp, deccfloat_exp, deccball_exp, ...) are documented with those types.

Full documentation

Other elementary and special functions are only available through the generic-ring interface: with a decimal context, gr_gamma, gr_bessel_j etc. dispatch to implementations handling all four decimal types, which are correctly rounded (componentwise for complex floats) in the floating-point contexts and give enclosures in the ball contexts. The arguments and flags have the same meaning as for the corresponding gr methods. The following methods are implemented (for all four types unless marked with complex):

  • Constants: pi, euler, catalan, khinchin, glaisher.
  • Exponentials and logarithms: exp, expm1, exp2, exp10, exp_pi_i (complex), log, log1p, log2, log10, log_pi_i (complex), pow.
  • Trigonometric functions: sin, cos, sin_cos, tan, cot, sec, csc, sinc, sin_pi, cos_pi, sin_cos_pi, tan_pi, cot_pi, sec_pi, csc_pi, sinc_pi, asin, acos, atan, atan2 (real), acot, asec, acsc, asin_pi, acos_pi, atan_pi, acot_pi, asec_pi, acsc_pi.
  • Hyperbolic functions: sinh, cosh, sinh_cosh, tanh, coth, sech, csch, asinh, acosh, atanh, acoth, asech, acsch.
  • Lambert W function: lambertw, lambertw_fmpz.
  • Gamma and related functions: gamma, gamma_fmpz, gamma_fmpq, rgamma, lgamma, digamma, polygamma (complex), fac_ui, fac_fmpz, rising, rising_ui, barnes_g, log_barnes_g.
  • Zeta and related functions: zeta, hurwitz_zeta, dirichlet_eta (complex), riemann_xi (complex), polylog, dilog, lerch_phi (complex).
  • Error functions and integrals: erf, erfc, erfi, erfinv, erfcinv, fresnel, fresnel_s, fresnel_c, gamma_upper, gamma_lower, beta_lower, exp_integral, exp_integral_ei, sin_integral, cos_integral, sinh_integral, cosh_integral, log_integral.
  • Bessel and Airy functions: bessel_j, bessel_y, bessel_i, bessel_k, bessel_i_scaled, bessel_k_scaled, airy, airy_ai, airy_bi, airy_ai_prime, airy_bi_prime, coulomb_f, coulomb_g.
  • Orthogonal polynomials: chebyshev_t, chebyshev_u, jacobi_p, gegenbauer_c, laguerre_l, hermite_h, legendre_p, legendre_q.
  • Hypergeometric functions: hypgeom_0f1, hypgeom_1f1, hypgeom_u, hypgeom_2f1.
  • Arithmetic-geometric mean and elliptic functions: agm, agm1, elliptic_k, elliptic_e, elliptic_pi, elliptic_f, elliptic_e_inc (complex).
  • Modular forms: modular_j, modular_lambda, modular_delta, dedekind_eta (complex).
int decfloat_via_arb(decfloat_t res, const decfloat_t x, int (*func)(arb_t, const arb_t, slong), gr_ctx_t ctx)
int _decfloat_arb_gr(decfloat_ptr * res, slong nres, decfloat_srcptr * args, slong nargs, int has_flag, int flag, int method, gr_ctx_t ctx)

The machinery behind the real elementary and special functions: evaluates a user-supplied arb function (which must return a gr status code), or the gr method method of the arb ring applied to nargs arguments (with an integer flag if has_flag is set) producing nres results, at increasing precision (Ziv's strategy) until the rounding of every result is decided. GR_UNABLE is returned if this does not happen within 64 times the working precision; for the elementary functions (exponentials, logarithms, powers, trigonometric and hyperbolic functions and their inverses), whose cost at high precision is quasi-linear, the limit is increased by twice the number of digits of the arguments, so that arguments with more digits than the target precision (which matter near cancellation points, e.g. \log_2(1 + 10^{-10^6})) are resolved in time proportional to their length. Points outside the real domain of the inverse functions and logarithms (\mathrm{acosh}(1/2), \log(-1)) are detected before any evaluation and give GR_DOMAIN.

Full documentation

Random generation#

Generates a random element rounded to the context precision, respectively a random element which may have more significant digits than the context precision.

Radius bounds#

A nonnegative number m \cdot 10^{k} with the mantissa m stored in a single word, 1 \le m < 10^{R} where R = DECMAG_MAX_PREC (9 on a 64-bit machine, 4 on a 32-bit machine), and an fmpz exponent k measured in digits, or the special values zero (m = 0) and +\infty (m = UWORD_MAX). The radius precision rad_prec is a context setting between 1 and R digits. The results of all operations are normalized, with exactly rad_prec digits (10^{\mathrm{rad\_prec}-1} \le m < 10^{\mathrm{rad\_prec}}, tested by the macro DECMAG_IS_NORMALIZED(x, ctx)), so that products of mantissas fit in a word and all operations reduce to a few word operations plus a small exponent adjustment.

More
void _decmag_set(decmag_t res, const decmag_t x, gr_ctx_t ctx)
void _decmag_set_round(decmag_t res, const decmag_t x, slong prec, gr_ctx_t ctx)
int _decmag_is_inf(const decmag_t x, gr_ctx_t ctx)
int _decmag_equal(const decmag_t x, const decmag_t y, gr_ctx_t ctx)
int _decmag_cmp(const decmag_t x, const decmag_t y, gr_ctx_t ctx)

Basic operations. set rounds x up to the radius precision of the context (a plain copy for a normalized x), set_lower rounds down, and set_round rounds up to prec digits (clamped to [1, R]) regardless of the context setting. digits returns the number of digits of the mantissa (0 for zero and infinity) and get_sci_exp the exponent E with 10^E \le x < 10^{E+1} (x finite and nonzero).

void _decmag_set_fmpz(decmag_t res, const fmpz_t x, gr_ctx_t ctx)
int _decmag_set_d(decmag_t res, double x, gr_ctx_t ctx)
void _decmag_set_uiui_10exp_fmpz(decmag_t res, ulong hi, ulong lo, ulong hi_radix, int sticky, const fmpz_t e, gr_ctx_t ctx)
void _decmag_set_mag(decmag_t res, const mag_t x, gr_ctx_t ctx)
void _decmag_get_fmpq(fmpq_t res, const decmag_t x, gr_ctx_t ctx)
int _decmag_get_d(double * res, const decmag_t x, gr_ctx_t ctx)
void _decmag_get_mag(mag_t res, const decmag_t x, gr_ctx_t ctx)

Conversions. The absolute value is taken of signed inputs. set_uiui_10exp_fmpz sets res to an upper bound for (\mathrm{hi} \cdot \mathrm{hi\_radix} + \mathrm{lo} + \varepsilon) 10^e where hi_radix is a power of ten and 0 \le \varepsilon < 1 with \varepsilon > 0 iff sticky is set. The conversion to decfloat_t is exact. set_mag gives the tightest upper bound at the radius precision when the binary exponent is at most a few thousand (using exact integer arithmetic on a scaled truncation), and a bound within a relative error of about 10^{-12} (rounded up to the radius precision) otherwise, computed from a fixed-point approximation of \log_{10} 2 and a double-precision power of ten with a safety margin; this covers arbitrarily large exponents, in time essentially independent of their size.

void _decmag_set_ulp(decmag_t res, const decfloat_t x, slong prec, gr_ctx_t ctx)

Sets res to 10^{E - \mathrm{prec} + 1} where E is the scientific exponent of x, i.e. the unit in the last place of x rounded to prec digits. Requires x to be nonzero and finite.

void _decmag_add(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_add_lower(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_sub_lower(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_mul(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_mul_lower(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_addmul(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_div(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_div_lower(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_inv(decmag_t res, const decmag_t x, gr_ctx_t ctx)
void _decmag_sqrt(decmag_t res, const decmag_t x, gr_ctx_t ctx)
void _decmag_rsqrt(decmag_t res, const decmag_t x, gr_ctx_t ctx)
void _decmag_mul_ui(decmag_t res, const decmag_t x, ulong y, gr_ctx_t ctx)
void _decmag_div_ui(decmag_t res, const decmag_t x, ulong y, gr_ctx_t ctx)
void _decmag_mul_10exp_fmpz(decmag_t res, const decmag_t x, const fmpz_t e, gr_ctx_t ctx)
void _decmag_max(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_min(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)
void _decmag_pow_ui(decmag_t res, const decmag_t x, ulong e, gr_ctx_t ctx)
void _decmag_hypot(decmag_t res, const decmag_t x, const decmag_t y, gr_ctx_t ctx)

Arithmetic with upper (or lower) bound rounding. sub_lower computes a lower bound for \max(x - y, 0). max rounds up and min rounds down to the radius precision.

char * _decmag_get_str(const decmag_t x, gr_ctx_t ctx)

A radius is printed with all its digits, in positional notation when its scientific exponent E satisfies -3 \le E \le 5 and in scientific notation otherwise (or always, with DECIMAL_WRITE_SCIENTIFIC).

Balls#

A ball [m \pm r] consisting of a midpoint of type decfloat_struct (field mid) and a radius of type decmag_struct (field rad), representing the set of real numbers \{ x : |x - m| \le r \}. The midpoint is always finite; an infinite radius denotes the whole real line. Arithmetic operations on balls are guaranteed to produce balls containing the exact result of the operation applied to any points of the input balls, with midpoints computed to the context precision in the context rounding mode.

More

Pointers to the midpoint and radius.

decfloat_ptr decball_midref(decball_t x)

Pointers to the midpoint and radius, as functions (for use through foreign function interfaces).

Memory management, as for decfloat_init, and tests whether the radius is zero, respectively whether the ball is finite.

Constants; zero_pm_inf is the whole real line [0 \pm \infty]. The gr methods pos_inf, neg_inf, uinf, undefined and unknown return GR_DOMAIN.

int decball_set(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_set_round(decball_t res, const decball_t x, slong prec, gr_ctx_t ctx)
int decball_set_round2(decball_t res, const decball_t x, slong prec, slong rad_prec, gr_ctx_t ctx)
int decball_set_fmpz(decball_t res, const fmpz_t x, gr_ctx_t ctx)
int decball_set_fmpq(decball_t res, const fmpq_t x, gr_ctx_t ctx)
int decball_set_d(decball_t res, double x, gr_ctx_t ctx)
int decball_set_str(decball_t res, const char * s, gr_ctx_t ctx)
int decball_set_arb(decball_t res, const arb_t x, gr_ctx_t ctx)
int decball_get_arb(arb_t res, const decball_t x, slong prec_bits, gr_ctx_t ctx)
int decball_get_fmpz(fmpz_t res, const decball_t x, gr_ctx_t ctx)
int decball_get_fmpq(fmpq_t res, const decball_t x, gr_ctx_t ctx)
int decball_get_d(double * res, const decball_t x, gr_ctx_t ctx)
void decball_get_rad(decmag_t res, const decball_t x, gr_ctx_t ctx)

Assignment and conversions. Values are rounded to the context precision with the rounding error added to the radius, and radii are rounded up to the radius precision of the context; set_round rounds the midpoint to prec digits instead and set_round2 additionally rounds the radius up to rad_prec digits (both preserve the enclosure). Integer conversion succeeds for exact integer balls, returns GR_DOMAIN for balls containing no integer, and GR_UNABLE otherwise.

More

Sets res to the ball m with an upper bound for |r| added to its radius, respectively with an infinite radius. These implement the +/- operator of the generic expression parser.

int decball_mid(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_rad(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_shell(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_lower(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_upper(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_add_rad(decball_t res, const decball_t x, const decball_t r, gr_ctx_t ctx)
int decball_set_interval(decball_t res, const decball_t lo, const decball_t hi, gr_ctx_t ctx)

Components and bounds of a ball x = [m \pm r], as balls. mid gives the exact ball m and rad the exact ball r (midpoints and radii are always representable), and shell gives [0 \pm r]. lower and upper give the endpoints m - r and m + r rounded to the context precision toward -\infty and +\infty respectively, and abs_lower and abs_upper give lower and upper bounds for |x| (\max(|m| - r, 0) rounded toward zero and |m| + r rounded up), all as exact balls, so that the results enclose the true bounds. An infinite radius gives GR_DOMAIN (the endpoints are not real numbers) except for abs_lower, which gives zero, and rad, which cannot represent an infinite radius as a ball. add_rad sets res to [x \pm |r|] (the same as set_interval_mid_rad), and set_interval sets res to the smallest ball containing both lo and hi (their convex hull, in either order), with the midpoint rounded to the context precision.

int _decball_contains_fmpq(const decball_t x, const fmpq_t y, gr_ctx_t ctx)
int _decball_contains_fmpz(const decball_t x, const fmpz_t y, gr_ctx_t ctx)
int _decball_contains(const decball_t x, const decball_t y, gr_ctx_t ctx)
int _decball_overlaps(const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_cmp(int * res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_cmpabs(int * res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_sgn(decball_t res, const decball_t x, gr_ctx_t ctx)

Predicates. Containment and overlap tests are decided exactly (using rational arithmetic in the rare cases where cheap bounds do not suffice), except that overlaps conservatively returns 1 and contains conservatively returns 0 when the exponents are too large for exact rational arithmetic.

int decball_neg(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_abs(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_add(decball_t res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_sub(decball_t res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_mul(decball_t res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_sqr(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_div(decball_t res, const decball_t x, const decball_t y, gr_ctx_t ctx)
int decball_inv(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_sqrt(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_rsqrt(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_add_ui(decball_t res, const decball_t x, ulong y, gr_ctx_t ctx)
int decball_add_si(decball_t res, const decball_t x, slong y, gr_ctx_t ctx)
int decball_add_fmpz(decball_t res, const decball_t x, const fmpz_t y, gr_ctx_t ctx)
int decball_sub_ui(decball_t res, const decball_t x, ulong y, gr_ctx_t ctx)
int decball_sub_si(decball_t res, const decball_t x, slong y, gr_ctx_t ctx)
int decball_sub_fmpz(decball_t res, const decball_t x, const fmpz_t y, gr_ctx_t ctx)
int decball_mul_ui(decball_t res, const decball_t x, ulong y, gr_ctx_t ctx)
int decball_mul_si(decball_t res, const decball_t x, slong y, gr_ctx_t ctx)
int decball_mul_fmpz(decball_t res, const decball_t x, const fmpz_t y, gr_ctx_t ctx)
int decball_div_ui(decball_t res, const decball_t x, ulong y, gr_ctx_t ctx)
int decball_div_si(decball_t res, const decball_t x, slong y, gr_ctx_t ctx)
int decball_div_fmpz(decball_t res, const decball_t x, const fmpz_t y, gr_ctx_t ctx)
int decball_mul_10exp_fmpz(decball_t res, const decball_t x, const fmpz_t e, gr_ctx_t ctx)
int decball_mul_2exp_fmpz(decball_t res, const decball_t x, const fmpz_t e, gr_ctx_t ctx)
int decball_floor(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_ceil(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_trunc(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_nint(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_pow_ui(decball_t res, const decball_t x, ulong y, gr_ctx_t ctx)
int decball_pow_si(decball_t res, const decball_t x, slong y, gr_ctx_t ctx)
int decball_pow_fmpz(decball_t res, const decball_t x, const fmpz_t y, gr_ctx_t ctx)

Ball arithmetic. The radius of a product is |x_m| y_r + |y_m| x_r + x_r y_r plus the rounding error of the midpoint, the radius of a quotient is (|x_m| y_r + |y_m| x_r) / (|y_m| (|y_m| - y_r)), and so on, all evaluated with upper-bound rounding. Square roots of balls containing negative numbers return GR_UNABLE (GR_DOMAIN if the whole ball is negative). Rounding to an integer returns an exact integer when the ball contains no integer boundary and otherwise [\operatorname{round}(m) \pm (r + 1)].

int decball_add_round(decball_t res, const decball_t x, const decball_t y, slong prec, gr_ctx_t ctx)
int decball_sub_round(decball_t res, const decball_t x, const decball_t y, slong prec, gr_ctx_t ctx)
int decball_mul_round(decball_t res, const decball_t x, const decball_t y, slong prec, gr_ctx_t ctx)
int decball_div_round(decball_t res, const decball_t x, const decball_t y, slong prec, gr_ctx_t ctx)
int decball_inv_round(decball_t res, const decball_t x, slong prec, gr_ctx_t ctx)
int decball_sqrt_round(decball_t res, const decball_t x, slong prec, gr_ctx_t ctx)

Versions with an explicit midpoint precision.

int decball_trim(decball_t res, const decball_t x, gr_ctx_t ctx)

Increases the radius, respectively rounds the midpoint to the number of digits justified by the radius.

Returns the number of correct significant digits of the midpoint, computed as the difference between the scientific exponents of the midpoint and the radius (the exponent of a zero midpoint is taken as zero). Returns DECIMAL_PREC_EXACT for an exact ball and -DECIMAL_PREC_EXACT for a ball with an infinite radius.

int decball_exp(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_expm1(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_log(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_log1p(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_sin(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_cos(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_sin_cos(decball_t res1, decball_t res2, const decball_t x, gr_ctx_t ctx)
int decball_tan(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_asin(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_acos(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_atan(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_atan2(decball_t res, const decball_t y, const decball_t x, gr_ctx_t ctx)
int decball_sinh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_cosh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_tanh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_asinh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_acosh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_atanh(decball_t res, const decball_t x, gr_ctx_t ctx)
int decball_pow(decball_t res, const decball_t x, const decball_t y, gr_ctx_t ctx)

The elementary functions of balls: enclosures of the function values at every point of the input balls, with midpoints rounded to the context precision. The other special functions are available through the generic gr methods.

More
int decball_via_arb(decball_t res, const decball_t x, int (*func)(arb_t, const arb_t, slong), gr_ctx_t ctx)
int _decball_arb_gr(decball_ptr * res, slong nres, decball_srcptr * args, slong nargs, int has_flag, int flag, int method, gr_ctx_t ctx)

Evaluate a user-supplied arb function func (which must return a gr status code), respectively the gr method method of the arb ring on nargs arguments (with an integer flag if has_flag is set) producing nres results, on decimal balls, by a rigorous roundtrip through arb.

char * decball_get_str(const decball_t x, gr_ctx_t ctx)

Output in the form [mid +/- rad] (or just mid for exact balls), and random generation of finite balls.

Complex floating-point numbers#

A complex number a + bi stored as a pair of real (field re) and imaginary (field im) parts of type decfloat_struct, in the same way as acf. The real and imaginary parts are rounded separately to the context precision, the real part in the mode DECIMAL_CTX_RND and the imaginary part in the mode DECIMAL_CTX_RND_IM (which is the same mode unless changed with decimal_ctx_set_rnd_im).

More

Pointers to the real and imaginary parts.

Pointers to the real and imaginary parts, as functions (for use through foreign function interfaces).

Memory management, as for decfloat_init, and tests whether the imaginary part is zero, whether the real part is zero and the imaginary part nonzero, whether both parts are finite, and whether either part is NaN.

Constants. The infinities are the real infinities \pm \infty + 0i, and NaN has both parts indeterminate.

int deccfloat_set_round(deccfloat_t res, const deccfloat_t x, slong prec, int rnd, int rnd_im, gr_ctx_t ctx)
int deccfloat_set_d(deccfloat_t res, double x, gr_ctx_t ctx)
int deccfloat_set_str(deccfloat_t res, const char * s, gr_ctx_t ctx)
int deccfloat_get_acf(acf_t res, const deccfloat_t x, slong prec_bits, int rnd, gr_ctx_t ctx)
int deccfloat_get_acb(acb_t res, const deccfloat_t x, slong prec_bits, gr_ctx_t ctx)
int deccfloat_get_si(slong * res, const deccfloat_t x, gr_ctx_t ctx)
int deccfloat_get_ui(ulong * res, const deccfloat_t x, gr_ctx_t ctx)
int deccfloat_get_d(double * res, const deccfloat_t x, gr_ctx_t ctx)

Assignment and conversions, with the parts rounded to the context precision in the respective rounding modes (deccfloat_set_round takes the precision and both modes explicitly). The conversions to real numbers or integers return GR_DOMAIN for a nonzero imaginary part.

More

Output in the form a for real numbers, b*I for imaginary numbers and (a + b*I) or (a - b*I) otherwise, where the parts are printed like real floating-point numbers, and random generation.

int deccfloat_cmp(int * res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_cmpabs(int * res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)

Predicates and comparisons. Equality is decided exactly (T_UNKNOWN only for NaN). cmp returns GR_DOMAIN unless both numbers are real, and cmpabs compares |x|^2 and |y|^2 exactly.

int deccfloat_add(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_sub(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_mul(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_div(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_add_fmpz(deccfloat_t res, const deccfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int deccfloat_sub_fmpz(deccfloat_t res, const deccfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int deccfloat_mul_fmpz(deccfloat_t res, const deccfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int deccfloat_div_fmpz(deccfloat_t res, const deccfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int deccfloat_pow_fmpz(deccfloat_t res, const deccfloat_t x, const fmpz_t y, gr_ctx_t ctx)
int deccfloat_pow(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, gr_ctx_t ctx)
int deccfloat_vec_dot(deccfloat_t res, const deccfloat_t initial, int subtract, deccfloat_srcptr vec1, deccfloat_srcptr vec2, slong len, gr_ctx_t ctx)
int deccfloat_vec_dot_rev(deccfloat_t res, const deccfloat_t initial, int subtract, deccfloat_srcptr vec1, deccfloat_srcptr vec2, slong len, gr_ctx_t ctx)

Arithmetic. Every operation is componentwise correctly rounded: the real and imaginary parts of the exact result of the operation on the exact inputs are each rounded once, to the context precision in the respective rounding modes. The vec_dot methods are the exception: they round after each term.

More
int _decfloat_dot2(decfloat_t res, const decfloat_t x1, const decfloat_t y1, const decfloat_t x2, const decfloat_t y2, int subtract, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)

Sets res to x_1 y_1 \pm x_2 y_2 correctly rounded (with the convention that a zero factor annihilates its term even when the other factor is infinite), optionally recording the rounding error. This is the primitive behind complex multiplication and division for both floating-point numbers and balls.

int _deccfloat_add_exact(deccfloat_t res, const deccfloat_t x, const deccfloat_t y, int subtract, gr_ctx_t ctx)

Exact operations, returning GR_UNABLE when the result is not representable (or, for additions and powers, would be absurdly large).

Complex balls#

A complex ball stored as a pair of real balls (fields re and im of type decball_struct), representing the rectangle \{ a + bi : |a - m_r| \le r_r, |b - m_i| \le r_i \} as in acb. Operations produce balls containing the exact result for every point of the input balls, with midpoints computed to the context precision in the context rounding mode (the same mode for both parts).

More

Pointers to the real and imaginary parts.

Pointers to the real and imaginary parts, as functions (for use through foreign function interfaces).

Memory management, as for decfloat_init, and tests whether both radii are zero, whether both parts are finite, and whether the imaginary part is exactly zero.

int deccball_set_round2(deccball_t res, const deccball_t x, slong prec, slong rad_prec, gr_ctx_t ctx)
int deccball_set_d(deccball_t res, double x, gr_ctx_t ctx)
int deccball_set_str(deccball_t res, const char * s, gr_ctx_t ctx)
int deccball_set_acb(deccball_t res, const acb_t x, gr_ctx_t ctx)
int deccball_get_acb(acb_t res, const deccball_t x, slong prec_bits, gr_ctx_t ctx)
int deccball_get_si(slong * res, const deccball_t x, gr_ctx_t ctx)
int deccball_get_ui(ulong * res, const deccball_t x, gr_ctx_t ctx)
int deccball_get_d(double * res, const deccball_t x, gr_ctx_t ctx)

Assignment and conversions, following decball. Strings in the output format of deccball_get_str and expressions such as "[1 + 2*I +/- 0.01]", "[1 + 2*I +/- (0.01 + 0.02*I)]" or "([1 +/- 0.1] + [2 +/- 0.2]*I) / (3 - I)" are accepted, also inside strings for polynomials and matrices over complex balls.

Sets res to the ball m with the radius of the real part increased by an upper bound for |\operatorname{Re}(r)| and the radius of the imaginary part by an upper bound for |\operatorname{Im}(r)| (as in acb, so that a real r only widens the real part), respectively with an infinite radius of the real part. These implement the +/- operator of the generic expression parser.

int deccball_add_rad(deccball_t res, const deccball_t x, const deccball_t r, gr_ctx_t ctx)

The exact midpoint, the ball with the same radii centered at zero, and the same as set_interval_mid_rad, respectively.

char * deccball_get_str(const deccball_t x, gr_ctx_t ctx)

Output in the form a, b*I, (a + b*I) or (a - b*I) where the parts are printed as real balls ([mid +/- rad], or just mid when exact), and random generation.

int deccball_re(deccball_t res, const deccball_t x, gr_ctx_t ctx)
int deccball_im(deccball_t res, const deccball_t x, gr_ctx_t ctx)
int deccball_add(deccball_t res, const deccball_t x, const deccball_t y, gr_ctx_t ctx)
int deccball_sub(deccball_t res, const deccball_t x, const deccball_t y, gr_ctx_t ctx)
int deccball_mul(deccball_t res, const deccball_t x, const deccball_t y, gr_ctx_t ctx)
int deccball_div(deccball_t res, const deccball_t x, const deccball_t y, gr_ctx_t ctx)
int deccball_add_fmpz(deccball_t res, const deccball_t x, const fmpz_t y, gr_ctx_t ctx)
int deccball_sub_fmpz(deccball_t res, const deccball_t x, const fmpz_t y, gr_ctx_t ctx)
int deccball_mul_fmpz(deccball_t res, const deccball_t x, const fmpz_t y, gr_ctx_t ctx)
int deccball_div_fmpz(deccball_t res, const deccball_t x, const fmpz_t y, gr_ctx_t ctx)
int deccball_pow_fmpz(deccball_t res, const deccball_t x, const fmpz_t y, gr_ctx_t ctx)
int deccball_pow(deccball_t res, const deccball_t x, const deccball_t y, gr_ctx_t ctx)

Ball arithmetic. Products are computed with _decball_dot2, which forms the exact midpoint products so that the radius only accounts for the input radii and a single rounding per part; the same holds for quotients when the modulus of the divisor is bounded away from zero by the componentwise bounds, and otherwise (when the rectangle |y|^2 computed from the parts contains zero but y does not) the quotient is computed through acb. Square roots, absolute values, arguments and signs of non-real balls go through acb as the transcendental functions do.

int _decball_dot2(decball_t res, const decball_t x1, const decball_t y1, const decball_t x2, const decball_t y2, int subtract, slong prec, gr_ctx_t ctx)

Sets res to a ball containing x_1 y_1 \pm x_2 y_2, with the midpoint computed as the correctly rounded exact product sum of the midpoints.

Increases both radii, trims both parts, respectively returns the number of correct significant digits computed from the larger midpoint and the larger radius.

Complex functions#

The elementary functions of complex floating-point numbers, componentwise correctly rounded: the real and imaginary parts of the exact value are each rounded once, to the context precision in the respective rounding modes. The other special functions, including the complex-only ones (exp_pi_i, log_pi_i, polygamma, ...), are available through the generic gr methods.

More

The elementary functions of complex balls, computed by a rigorous roundtrip through acb, so that the output balls contain the values at every point of the input balls.

More
int _deccfloat_acb_gr(deccfloat_ptr * res, slong nres, deccfloat_srcptr * args, slong nargs, int has_flag, int flag, int method, gr_ctx_t ctx)
int _deccball_acb_gr(deccball_ptr * res, slong nres, deccball_srcptr * args, slong nargs, int has_flag, int flag, int method, gr_ctx_t ctx)

Evaluate the gr method method of the acb ring on nargs arguments (with an integer flag if has_flag is set) producing nres results: with Ziv's strategy and componentwise correct rounding for complex floating-point numbers, and by a single rigorous evaluation for complex balls. These correspond to _decfloat_arb_gr and _decball_arb_gr.

Internal functions#

The functions below are declared in decimal.h for use by the implementation and the tests. Their interfaces may change.

Set a special value regardless of the context flags; return whether x is admitted as an operand (GR_SUCCESS for a finite value or an admitted special value, otherwise GR_DOMAIN for an infinity and GR_UNABLE for NaN); and check the exponent limits and the admitted special values of a computed result, flushing or overflowing it as the context prescribes.

Compare |x| and |y| for nonzero finite x and y; return the scientific exponent of a finite nonzero x, clamped to \pmDECFLOAT_EXP_CLAMP.

int _decfloat_set_scalar_exact(decfloat_t res, const void * y, int type, gr_ctx_t ctx)
int _decfloat_set_str_literal(decfloat_t res, const char * s, slong prec, int rnd, gr_ctx_t ctx)

Exact conversion of the scalar *y of type DECIMAL_SCALAR_UI, DECIMAL_SCALAR_SI, DECIMAL_SCALAR_FMPZ or DECIMAL_SCALAR_D (without rounding or exponent limits); parsing of a plain decimal literal with rounding to prec digits, returning GR_DOMAIN if s is not a literal (no expression parsing); and conversion from an element of a decimal context x_ctx with a possibly different limb radix.

int _decfloat_set_round_fmpz_2exp_err(decfloat_t res, const fmpz_t m, const fmpz_t t, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)
int _decfloat_set_round_fmpz_2exp_scaled(decfloat_t res, const fmpz_t m, slong t, slong prec, int rnd, decimal_rounding_info * info, decmag_ptr err, gr_ctx_t ctx)

The binary-to-decimal conversion of m 2^t: correctly rounded with an optional error bound; by exact integer arithmetic on a scaled truncation when the sizes are moderate (returning 0 when they are too large, so that the caller uses arb); setting res to m 2^t and returning 1 when it is exactly representable with at most prec digits; and a fast path for a short arf with a moderate exponent. _decfloat_maybe_dyadic is a cheap necessary condition for x to be a dyadic rational.

int _decfloat_set_round_arb_err(decfloat_t res, decmag_ptr err, const arb_t x, slong prec, int rnd, gr_ctx_t ctx)
int _decfloat_round_arb(decfloat_t res, const arb_t x, slong prec, int rnd, gr_ctx_t ctx)
int _decfloat_round_arb_small(decfloat_t res, const arf_t mid, const mag_t rad, slong prec, int rnd, gr_ctx_t ctx)
int _decfloat_round_ball(decfloat_t res, const decball_t Y, slong prec, int rnd, gr_ctx_t bctx, gr_ctx_t ctx)

The steps of Ziv's strategy: round an arb ball (or a decball Y of the context bctx) to prec digits if both endpoints round to the same value, returning 1, or 0 if the rounding is undecided (the first function returns GR_UNABLE instead, and sets err to a tight bound for the rounding error); compute 10^t to wp bits in time essentially linear in the size of t; and the number of bits, with guard bits, corresponding to prec digits.

int _decfloat_round_with_tail(decfloat_t res, const decfloat_t S, int tail_sign, slong tail_exp, slong prec, int rnd, gr_ctx_t ctx)

Round S + t to prec digits, where S is an exact nonzero finite decfloat and t a nonzero real number of sign tail_sign with |t| < 10^{\mathit{tail\_exp}}. Return 1 if tail_exp lies at least prec + 4e digits below the leading digit of S, so that t only acts as a sticky bit; 0 if this precondition fails, and -1 on an error (exponent limits). This implements the handling of tiny arguments of the elementary and special functions.

slong _decimal_ziv_wp_max(slong prec, slong operand_digits)
int _decimal_arb_args_infeasible(int method, decfloat_srcptr * re, decfloat_srcptr * im, slong nargs, slong prec, gr_ctx_t ctx)

The limits of Ziv's strategy: the largest working precision in digits, 64\,\mathit{prec} + 1000 + 2 \min(\mathit{operand\_digits}, DECIMAL_CONV_DIGITS_LIMIT); whether the gr method method is an elementary function, whose loop may go up to the length of the operands (other functions stop at 64\,\mathit{prec} + 1000 digits); and whether the arguments (real parts re, imaginary parts im or NULL) are rejected before any evaluation as too costly for the method.

int _decfloat_pow_int_exact(decfloat_t res, const decfloat_t x, const fmpz_t n, slong prec, int rnd, gr_ctx_t ctx)

x^n for an integer n, exactly and then rounded when feasible (returning 1 if handled); x^y = \exp(y \log x) for finite x > 0 and finite y by Ziv's strategy; and a ball power by binary powering, which does not use the method table of the context and so can be called with a complex context.

void _decball_add_rounding_error(decball_t res, const decimal_rounding_info * info, const decmag_t precise_err, slong prec, gr_ctx_t ctx)

Add the rounding error of a midpoint operation, described by info and the tight bound precise_err, to the radius of res: the tight bound, or one ulp at prec digits (half an ulp for the nearest modes) with DECIMAL_SLOPPY_RADIUS.

char * _decfloat_get_str_mode(const decfloat_t x, int mode, gr_ctx_t ctx)
slong _decfloat_get_digits(char * buf, fmpz_t t, const decfloat_t x, gr_ctx_t ctx)
slong _decmag_get_digits(char * buf, fmpz_t t, const decmag_t x, gr_ctx_t ctx)
slong _decimal_write_number(char * out, int negative, const char * digits, slong L, const fmpz_t t, int mode)

Output helpers. mode 0 prints in positional notation when the scientific exponent satisfies -6 \le E \le 20, mode 1 in scientific notation unless E = 0, and mode 2 (for radii) in positional notation when -3 \le E \le 5. The digit functions write the digits of |x| with trailing zeros stripped and set t to the exponent of the last digit, returning the number of digits; _decimal_write_number writes (-1)^{\mathit{negative}}\,\mathit{digits} \cdot 10^t without a null terminator and returns the number of characters, and _decimal_write_number_bound bounds that number.

int _decfloat_unary_method(decfloat_t res, const decfloat_t x, int method, gr_ctx_t ctx)
int _decfloat_unary2_method(decfloat_t res1, decfloat_t res2, const decfloat_t x, int method1, int method2, gr_ctx_t ctx)
int _decball_unary_fn(decball_t res, const decball_t x, int method, gr_ctx_t ctx)
int _decball_unary2_method(decball_t res1, decball_t res2, const decball_t x, int method, gr_ctx_t ctx)
int _deccfloat_unary_method(deccfloat_t res, const deccfloat_t x, int method, gr_ctx_t ctx)
int _deccball_unary_method(deccball_t res, const deccball_t x, int method, gr_ctx_t ctx)
int _decfloat_arb_args(decfloat_ptr res, decfloat_srcptr x, decfloat_srcptr y, decfloat_srcptr z, decfloat_srcptr w, int flag, int spec, gr_ctx_t ctx)
int _decball_arb_args(decball_ptr res, decball_srcptr x, decball_srcptr y, decball_srcptr z, decball_srcptr w, int flag, int spec, gr_ctx_t ctx)
int _decfloat_constant(decfloat_t res, int method, gr_ctx_t ctx)
int _decball_constant(decball_t res, int method, gr_ctx_t ctx)

The drivers behind the special functions: the unary function given by the gr method method, correctly rounded (componentwise for complex numbers), respectively enclosed for balls; two functions at once (sin_cos, sinh_cosh); a single-result function of up to four arguments with spec = DECIMAL_ARGSPEC(method, nargs, has_flag); and constants.

int _decball_lambertw_fmpz(decball_t res, const decball_t x, const fmpz_t k, gr_ctx_t ctx)

The gr methods gamma_fmpz, gamma_fmpq, fac_ui, fac_fmpz, rising_ui and lambertw_fmpz of the real decimal rings, correctly rounded resp. enclosed.

References#

Cite this page

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