Arithmetic Functions (GNU MPFR 4.2.2)
Description: How to install and use GNU MPFR, a library for reliable multiple precision floating-point arithmetic, version 4.2.2.

### 5.5 Arithmetic Functions ¶

Function: `int` mpfr_add `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_add_ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_add_si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_add_d `(mpfr_t rop, mpfr_t op1, double op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_add_z `(mpfr_t rop, mpfr_t op1, mpz_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_add_q `(mpfr_t rop, mpfr_t op1, mpq_t op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 + op2 rounded in the direction rnd. The IEEE 754 rules are used, in particular for signed zeros. But for types having no signed zeros, 0 is considered unsigned (i.e., (+0) + 0 = (+0) and (−0) + 0 = (−0)). The `mpfr_add_d` function assumes that the radix of the `double` type is a power of 2, with a precision at most that declared by the C implementation (macro `IEEE_DBL_MANT_DIG`, and if not defined 53 bits).

Function: `int` mpfr_sub `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_ui_sub `(mpfr_t rop, unsigned long int op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sub_ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_si_sub `(mpfr_t rop, long int op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sub_si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_d_sub `(mpfr_t rop, double op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sub_d `(mpfr_t rop, mpfr_t op1, double op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_z_sub `(mpfr_t rop, mpz_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sub_z `(mpfr_t rop, mpfr_t op1, mpz_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sub_q `(mpfr_t rop, mpfr_t op1, mpq_t op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 − op2 rounded in the direction rnd. The IEEE 754 rules are used, in particular for signed zeros. But for types having no signed zeros, 0 is considered unsigned (i.e., (+0) − 0 = (+0), (−0) − 0 = (−0), 0 − (+0) = (−0) and 0 − (−0) = (+0)). The same restrictions as for `mpfr_add_d` apply to `mpfr_d_sub` and `mpfr_sub_d`.

Function: `int` mpfr_mul `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_d `(mpfr_t rop, mpfr_t op1, double op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_z `(mpfr_t rop, mpfr_t op1, mpz_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_q `(mpfr_t rop, mpfr_t op1, mpq_t op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 times op2 rounded in the direction rnd. When a result is zero, its sign is the product of the signs of the operands (for types having no signed zeros, 0 is considered positive). The same restrictions as for `mpfr_add_d` apply to `mpfr_mul_d`. Note: when op1 and op2 are equal, use `mpfr_sqr` instead of `mpfr_mul` for better efficiency.

Function: `int` mpfr_sqr `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶

  Set rop to the square of op rounded in the direction rnd.

Function: `int` mpfr_div `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_ui_div `(mpfr_t rop, unsigned long int op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_si_div `(mpfr_t rop, long int op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_d_div `(mpfr_t rop, double op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_d `(mpfr_t rop, mpfr_t op1, double op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_z `(mpfr_t rop, mpfr_t op1, mpz_t op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_q `(mpfr_t rop, mpfr_t op1, mpq_t op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 / op2 rounded in the direction rnd. When a result is zero, its sign is the product of the signs of the operands. For types having no signed zeros, 0 is considered positive; but note that if op1 is non-zero and op2 is zero, the result might change from ±Inf to NaN in future MPFR versions if there is an opposite decision on the IEEE 754 side. The same restrictions as for `mpfr_add_d` apply to `mpfr_d_div` and `mpfr_div_d`.

Function: `int` mpfr_sqrt `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_sqrt_ui `(mpfr_t rop, unsigned long int op, mpfr_rnd_t rnd)` ¶

  Set rop to the square root of op rounded in the direction rnd. Set rop to −0 if op is −0, to be consistent with the IEEE 754 standard (thus this differs from `mpfr_rootn_ui` and `mpfr_rootn_si` with n = 2). Set rop to NaN if op is negative.

Function: `int` mpfr_rec_sqrt `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶

  Set rop to the reciprocal square root of op rounded in the direction rnd. Set rop to +Inf if op is ±0, +0 if op is +Inf, and NaN if op is negative. Warning! Therefore the result on −0 is different from the one of the rSqrt function recommended by the IEEE 754 standard (Section 9.2.1), which is −Inf instead of +Inf. However, `mpfr_rec_sqrt` is equivalent to `mpfr_rootn_si` with n = −2.

Function: `int` mpfr_cbrt `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_rootn_ui `(mpfr_t rop, mpfr_t op, unsigned long int n, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_rootn_si `(mpfr_t rop, mpfr_t op, long int n, mpfr_rnd_t rnd)` ¶

  Set rop to the nth root (with n = 3, the cubic root, for `mpfr_cbrt`) of op rounded in the direction rnd. For n = 0, set rop to NaN. For n odd (resp. even) and op negative (including −Inf), set rop to a negative number (resp. NaN). If op is zero, set rop to zero with the sign obtained by the usual limit rules, i.e., the same sign as op if n is odd, and positive if n is even.

  These functions agree with the rootn operation of the IEEE 754 standard.

Function: `int` mpfr_root `(mpfr_t rop, mpfr_t op, unsigned long int n, mpfr_rnd_t rnd)` ¶

  This function is the same as `mpfr_rootn_ui` except when op is −0 and n is even: the result is −0 instead of +0 (the reason was to be consistent with `mpfr_sqrt`). Said otherwise, if op is zero, set rop to op.

  This function predates IEEE 754-2008, where rootn was introduced, and behaves differently from the IEEE 754 rootn operation. It is marked as deprecated and will be removed in a future release.

Function: `int` mpfr_neg `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_abs `(mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)` ¶

  Set rop to −op and the absolute value of op respectively, rounded in the direction rnd. Just changes or adjusts the sign if rop and op are the same variable, otherwise a rounding might occur if the precision of rop is less than that of op.

  The sign rule also applies to NaN in order to mimic the IEEE 754 `negate` and `abs` operations, i.e., for `mpfr_neg`, the sign is reversed, and for `mpfr_abs`, the sign is set to positive. But contrary to IEEE 754, the NaN flag is set as usual.

Function: `int` mpfr_dim `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)` ¶

  Set rop to the positive difference of op1 and op2, i.e., op1 − op2 rounded in the direction rnd if op1 > op2, +0 if op1 <= op2, and NaN if op1 or op2 is NaN.

Function: `int` mpfr_mul_2ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_mul_2si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 times 2 raised to op2 rounded in the direction rnd. Just increases the exponent by op2 when rop and op1 are identical.

Function: `int` mpfr_div_2ui `(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_div_2si `(mpfr_t rop, mpfr_t op1, long int op2, mpfr_rnd_t rnd)` ¶

  Set rop to op1 divided by 2 raised to op2 rounded in the direction rnd. Just decreases the exponent by op2 when rop and op1 are identical.

Function: `int` mpfr_fac_ui `(mpfr_t rop, unsigned long int op, mpfr_rnd_t rnd)` ¶

  Set rop to the factorial of op, rounded in the direction rnd.

Function: `int` mpfr_fma `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_t op3, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_fms `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_t op3, mpfr_rnd_t rnd)` ¶

  Set rop to (op1 times op2) + op3 (resp. (op1 times op2) − op3) rounded in the direction rnd. Concerning special values (signed zeros, infinities, NaN), these functions behave like a multiplication followed by a separate addition or subtraction. That is, the fused operation matters only for rounding.

Function: `int` mpfr_fmma `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_t op3, mpfr_t op4, mpfr_rnd_t rnd)` ¶
Function: `int` mpfr_fmms `(mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_t op3, mpfr_t op4, mpfr_rnd_t rnd)` ¶

  Set rop to (op1 times op2) + (op3 times op4) (resp. (op1 times op2) − (op3 times op4)) rounded in the direction rnd. In case the computation of op1 times op2 overflows or underflows (or that of op3 times op4), the result rop is computed as if the two intermediate products were computed with rounding toward zero.

Function: `int` mpfr_hypot `(mpfr_t rop, mpfr_t x, mpfr_t y, mpfr_rnd_t rnd)` ¶

  Set rop to the Euclidean norm of x and y, i.e., the square root of the sum of the squares of x and y, rounded in the direction rnd. Special values are handled as described in the ISO C99 (Section F.9.4.3) and IEEE 754 (Section 9.2.1) standards: If x or y is an infinity, then +Inf is returned in rop, even if the other number is NaN.

Function: `int` mpfr_sum `(mpfr_t rop, const mpfr_ptr tab[], unsigned long int n, mpfr_rnd_t rnd)` ¶

  Set rop to the sum of all elements of tab, whose size is n, correctly rounded in the direction rnd. Warning: for efficiency reasons, tab is an array of pointers to `mpfr_t`, not an array of `mpfr_t`. If n = 0, then the result is +0, and if n = 1, then the function is equivalent to `mpfr_set`. For the special exact cases, the result is the same as the one obtained with a succession of additions (`mpfr_add`) in infinite precision. In particular, if the result is an exact zero and n >= 1:

  - if all the inputs have the same sign (i.e., all +0 or all −0), then the result has the same sign as the inputs;
  - otherwise, either because all inputs are zeros with at least a +0 and a −0, or because some inputs are non-zero (but they globally cancel), the result is +0, except for the `MPFR_RNDD` rounding mode, where it is −0.

Function: `int` mpfr_dot `(mpfr_t rop, const mpfr_ptr a[], const mpfr_ptr b[], unsigned long int n, mpfr_rnd_t rnd)` ¶

  Set rop to the dot product of elements of a by those of b, whose common size is n, correctly rounded in the direction rnd. Warning: for efficiency reasons, a and b are arrays of pointers to `mpfr_t`. This function is experimental, and does not yet handle intermediate overflows and underflows.

For the power functions (with an integer exponent or not), see mpfr_pow in Transcendental Functions.