PIPS
arith_mulprec.h File Reference

This header provides functions for performing multiple-precision arithmetic on integer or rational numbers, using GMP. More...

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "assert.h"
#include "arithmetique.h"
+ Include dependency graph for arith_mulprec.h:

Go to the source code of this file.

Macros

#define LINEAR_ARITH_MULPREC_H
 
#define NOWUNUSED   __attribute__((unused))
 

Integers Functions

This section describes the functions for performing integer arithmetic.

These functions start with the prefix zval_. Integers are stored in objects of type zval_t.

#define zval_init(z)   (mpz_init(z))
 Initialize z and set its value to 0. More...
 
#define zval_clear(z)   (mpz_clear(z))
 Free the space occupied by z. More...
 
#define zval_set(z1, z2)   (mpz_set(z1, z2))
 Set the value of z1 from z2. More...
 
#define zval_set_i(z, n)   (mpz_set_si(z, n))
 Set the value of z from the signed long n. More...
 
#define zval_init_set(z1, z2)   (mpz_init_set(z1, z2))
 Initialize z1 and set its value from z2. More...
 
#define zval_init_set_i(z, n)   (mpz_init_set_si(z, n))
 Initialize z and set its value from the signed long n. More...
 
#define zval_get_i(z)   (mpz_get_si(z))
 Return the value of z as a signed long. More...
 
#define zval_add(z1, z2, z3)   (mpz_add(z1, z2, z3))
 Set z1 to z2 + z3. More...
 
#define zval_sub(z1, z2, z3)   (mpz_sub(z1, z2, z3))
 Set z1 to z2 - z3. More...
 
#define zval_mul(z1, z2, z3)   (mpz_mul(z1, z2, z3))
 Set z1 to z2 times z3. More...
 
#define zval_div(z1, z2, z3)   (mpz_fdiv_q(z1, z2, z3))
 Set z1 to z2/z3. More...
 
#define zval_addmul(z1, z2, z3)   (mpz_addmul(z1, z2, z3))
 Set z1 to z1 + z2 times z3. More...
 
#define zval_submul(z1, z2, z3)   (mpz_submul(z1, z2, z3))
 Set z1 to z1 - z2 times z3. More...
 
#define zval_neg(z1, z2)   (mpz_neg(z1, z2))
 Set z1 to -z2. More...
 
#define zval_abs(z1, z2)   (mpz_abs(z1, z2))
 Set z1 to the absolute value of z2. More...
 
#define zval_mod(z1, z2, z3)   (mpz_mod(z1, z2, z3))
 Set z1 to z2 mod z3. More...
 
#define zval_gcd(z1, z2, z3)   (mpz_gcd(z1, z2, z3))
 Set z1 to the greatest common divisor of z2 and z3. More...
 
#define zval_lcm(z1, z2, z3)   (mpz_lcm(z1, z2, z3))
 Set z1 to the least common multiple of z2 and z3. More...
 
#define zval_cmp(z1, z2)   (mpz_cmp(z1, z2))
 Compare z1 and z2. More...
 
#define zval_cmp_i(z, n)   (mpz_cmp_si(z, n))
 Compare z with a signed long n. More...
 
#define zval_sgn(z)   (mpz_sgn(z))
 Return +1 if z > 0, 0 if z = 0, and -1 if z < 0. More...
 
#define zval_equal(z1, z2)   (zval_cmp(z1, z2) == 0)
 Return non-zero if z1 and z2 are equal, zero if they are non-equal. More...
 
#define zval_equal_i(z, n)   (zval_cmp_i(z, n) == 0)
 Return non-zero if z and the unsigned long n are equal, zero if they are non-equal. More...
 
#define zval_fprint(stream, z)   (mpz_out_str(stream, 10, z))
 Output z on stdio stream stream. More...
 
#define zval_print(z)   (mpz_out_str(stdout, 10, z))
 Output z on stdout. More...
 
typedef mpz_t zval_t
 Type of integer numbers. More...
 

Rational Number Functions

This section describes the functions for performing arithmetic on rational numbers.

These functions start with the prefix qval_. Rational numbers are stored in objects of type qval_t.

#define qval_canonicalize(q)   (mpq_canonicalize(q))
 Remove any factors that are common to the numerator and denominator of q, and make the denominator positive. More...
 
#define qval_init(q)   (mpq_init(q))
 Initialize q and set its value to 0/1. More...
 
#define qval_clear(q)   (mpq_clear(q))
 Free the space occupied by q. More...
 
#define qval_set(q1, q2)   (mpq_set(q1, q2))
 Set the value of q1 from q2. More...
 
#define qval_set_i(q1, q2num, q2den)   (mpq_set_si(q1, q2num, q2den))
 Set the value of q to q2num/q2den. More...
 
#define qval_add(q1, q2, q3)   (mpq_add(q1, q2, q3))
 Set q1 to q2 + q3. More...
 
#define qval_sub(q1, q2, q3)   (mpq_sub(q1, q2, q3)(
 Set q1 to q2 - q3. More...
 
#define qval_mul(q1, q2, q3)   (mpq_mul(q1, q2, q3))
 Set q1 to q2 times q3. More...
 
#define qval_div(q1, q2, q3)   (mpq_div(q1, q2, q3))
 Set q1 to q2/q3. More...
 
#define qval_neg(q1, q2)   (mpq_neg(q1, q2))
 Set q1 to -q2. More...
 
#define qval_abs(q1, q2)   (mpq_abs(q1, q2))
 Set q1 to the absolute value of q2. More...
 
#define qval_inv(q1, q2)   (mpq_inv(q1, q2))
 Set q1 to 1/q2. More...
 
#define qval_cmp(q1, q2)   (mpq_cmp(q1, q2))
 Compare q1 and q2. More...
 
#define qval_cmp_i(q1, q2num, q2den)   (mpq_cmp_si(q1, q2num, q2den))
 Compare q1 and q2num/q2den. More...
 
#define qval_sgn(q)   (mpq_sgn(q))
 Return +1 if q > 0, 0 if q = 0, and -1 if q < 0. More...
 
#define qval_equal(q1, q2)   (mpq_equal(q1, q2))
 Return non-zero if q1 and q2 are equal, zero if they are non-equal. More...
 
#define qval_equal_i(q1, q2num, q2den)   (qval_cmp_i(q1, q2num, q2den) == 0)
 Return non-zero if q and q2num/q2den are equal, zero if they are non-equal. More...
 
#define qval_fprint(stream, q)   (mpq_out_str(stream, 10, q))
 Output q on stdio stream stream. More...
 
#define qval_print(q)   (mpq_out_str(stdout, 10, q))
 Output q on stdout. More...
 
typedef __mpq_struct qval_s
 
typedef mpq_ptr qval_p
 
typedef qval_s qval_t[1]
 Type of rational numbers. More...
 

Detailed Description

This header provides functions for performing multiple-precision arithmetic on integer or rational numbers, using GMP.

Definition in file arith_mulprec.h.

Macro Definition Documentation

◆ LINEAR_ARITH_MULPREC_H

#define LINEAR_ARITH_MULPREC_H

Definition at line 44 of file arith_mulprec.h.

◆ NOWUNUSED

#define NOWUNUSED   __attribute__((unused))

Definition at line 54 of file arith_mulprec.h.

◆ qval_abs

#define qval_abs (   q1,
  q2 
)    (mpq_abs(q1, q2))

Set q1 to the absolute value of q2.

Definition at line 277 of file arith_mulprec.h.

◆ qval_add

#define qval_add (   q1,
  q2,
  q3 
)    (mpq_add(q1, q2, q3))

Set q1 to q2 + q3.

Definition at line 252 of file arith_mulprec.h.

◆ qval_canonicalize

#define qval_canonicalize (   q)    (mpq_canonicalize(q))

Remove any factors that are common to the numerator and denominator of q, and make the denominator positive.

Definition at line 227 of file arith_mulprec.h.

◆ qval_clear

#define qval_clear (   q)    (mpq_clear(q))

Free the space occupied by q.

Definition at line 237 of file arith_mulprec.h.

◆ qval_cmp

#define qval_cmp (   q1,
  q2 
)    (mpq_cmp(q1, q2))

Compare q1 and q2.

Return a positive value if q1 > q2, qero if q1 = q2, or a negative value if q1 < q2. To determine if two rationals are equal, qval_equal is faster than qval_cmp.

Definition at line 291 of file arith_mulprec.h.

◆ qval_cmp_i

#define qval_cmp_i (   q1,
  q2num,
  q2den 
)    (mpq_cmp_si(q1, q2num, q2den))

Compare q1 and q2num/q2den.

Return a positive value if q1 > q2num/q2den, zero if q1 = q2num/q2den, or a negative value if q1 < q2num/q2den.

Definition at line 299 of file arith_mulprec.h.

◆ qval_div

#define qval_div (   q1,
  q2,
  q3 
)    (mpq_div(q1, q2, q3))

Set q1 to q2/q3.

Definition at line 267 of file arith_mulprec.h.

◆ qval_equal

#define qval_equal (   q1,
  q2 
)    (mpq_equal(q1, q2))

Return non-zero if q1 and q2 are equal, zero if they are non-equal.

Although qval_cmp can be used for the same purpose, this function is faster.

Definition at line 311 of file arith_mulprec.h.

◆ qval_equal_i

#define qval_equal_i (   q1,
  q2num,
  q2den 
)    (qval_cmp_i(q1, q2num, q2den) == 0)

Return non-zero if q and q2num/q2den are equal, zero if they are non-equal.

Definition at line 317 of file arith_mulprec.h.

◆ qval_fprint

#define qval_fprint (   stream,
 
)    (mpq_out_str(stream, 10, q))

Output q on stdio stream stream.

Definition at line 322 of file arith_mulprec.h.

◆ qval_init

#define qval_init (   q)    (mpq_init(q))

Initialize q and set its value to 0/1.

Definition at line 232 of file arith_mulprec.h.

◆ qval_inv

#define qval_inv (   q1,
  q2 
)    (mpq_inv(q1, q2))

Set q1 to 1/q2.

Definition at line 282 of file arith_mulprec.h.

◆ qval_mul

#define qval_mul (   q1,
  q2,
  q3 
)    (mpq_mul(q1, q2, q3))

Set q1 to q2 times q3.

Definition at line 262 of file arith_mulprec.h.

◆ qval_neg

#define qval_neg (   q1,
  q2 
)    (mpq_neg(q1, q2))

Set q1 to -q2.

Definition at line 272 of file arith_mulprec.h.

◆ qval_print

#define qval_print (   q)    (mpq_out_str(stdout, 10, q))

Output q on stdout.

Definition at line 327 of file arith_mulprec.h.

◆ qval_set

#define qval_set (   q1,
  q2 
)    (mpq_set(q1, q2))

Set the value of q1 from q2.

Definition at line 242 of file arith_mulprec.h.

◆ qval_set_i

#define qval_set_i (   q1,
  q2num,
  q2den 
)    (mpq_set_si(q1, q2num, q2den))

Set the value of q to q2num/q2den.

Definition at line 247 of file arith_mulprec.h.

◆ qval_sgn

#define qval_sgn (   q)    (mpq_sgn(q))

Return +1 if q > 0, 0 if q = 0, and -1 if q < 0.

Definition at line 304 of file arith_mulprec.h.

◆ qval_sub

#define qval_sub (   q1,
  q2,
  q3 
)    (mpq_sub(q1, q2, q3)(

Set q1 to q2 - q3.

Definition at line 257 of file arith_mulprec.h.

◆ zval_abs

#define zval_abs (   z1,
  z2 
)    (mpz_abs(z1, z2))

Set z1 to the absolute value of z2.

Definition at line 142 of file arith_mulprec.h.

◆ zval_add

#define zval_add (   z1,
  z2,
  z3 
)    (mpz_add(z1, z2, z3))

Set z1 to z2 + z3.

Definition at line 107 of file arith_mulprec.h.

◆ zval_addmul

#define zval_addmul (   z1,
  z2,
  z3 
)    (mpz_addmul(z1, z2, z3))

Set z1 to z1 + z2 times z3.

Definition at line 127 of file arith_mulprec.h.

◆ zval_clear

#define zval_clear (   z)    (mpz_clear(z))

Free the space occupied by z.

Definition at line 77 of file arith_mulprec.h.

◆ zval_cmp

#define zval_cmp (   z1,
  z2 
)    (mpz_cmp(z1, z2))

Compare z1 and z2.

Return a positive value if z1 > z2, zero if z1 = z2, or a negative value if z1 < z2.

Definition at line 168 of file arith_mulprec.h.

◆ zval_cmp_i

#define zval_cmp_i (   z,
 
)    (mpz_cmp_si(z, n))

Compare z with a signed long n.

Return a positive value if z > n, zero if z = n, or a negative value if z < n.

Definition at line 175 of file arith_mulprec.h.

◆ zval_div

#define zval_div (   z1,
  z2,
  z3 
)    (mpz_fdiv_q(z1, z2, z3))

Set z1 to z2/z3.

Definition at line 122 of file arith_mulprec.h.

◆ zval_equal

#define zval_equal (   z1,
  z2 
)    (zval_cmp(z1, z2) == 0)

Return non-zero if z1 and z2 are equal, zero if they are non-equal.

Definition at line 185 of file arith_mulprec.h.

◆ zval_equal_i

#define zval_equal_i (   z,
 
)    (zval_cmp_i(z, n) == 0)

Return non-zero if z and the unsigned long n are equal, zero if they are non-equal.

Definition at line 191 of file arith_mulprec.h.

◆ zval_fprint

#define zval_fprint (   stream,
 
)    (mpz_out_str(stream, 10, z))

Output z on stdio stream stream.

Definition at line 196 of file arith_mulprec.h.

◆ zval_gcd

#define zval_gcd (   z1,
  z2,
  z3 
)    (mpz_gcd(z1, z2, z3))

Set z1 to the greatest common divisor of z2 and z3.

The result is always positive, irrespective of the signs of z2 and z3. Except if both inputs are zero; then it is undefined.

Definition at line 154 of file arith_mulprec.h.

◆ zval_get_i

#define zval_get_i (   z)    (mpz_get_si(z))

Return the value of z as a signed long.

Definition at line 102 of file arith_mulprec.h.

◆ zval_init

#define zval_init (   z)    (mpz_init(z))

Initialize z and set its value to 0.

Definition at line 72 of file arith_mulprec.h.

◆ zval_init_set

#define zval_init_set (   z1,
  z2 
)    (mpz_init_set(z1, z2))

Initialize z1 and set its value from z2.

Definition at line 92 of file arith_mulprec.h.

◆ zval_init_set_i

#define zval_init_set_i (   z,
 
)    (mpz_init_set_si(z, n))

Initialize z and set its value from the signed long n.

Definition at line 97 of file arith_mulprec.h.

◆ zval_lcm

#define zval_lcm (   z1,
  z2,
  z3 
)    (mpz_lcm(z1, z2, z3))

Set z1 to the least common multiple of z2 and z3.

The result is always positive, irrespective of the signs of z2 and z3. z1 will be zero if either z2 or z3 is zero.

Definition at line 161 of file arith_mulprec.h.

◆ zval_mod

#define zval_mod (   z1,
  z2,
  z3 
)    (mpz_mod(z1, z2, z3))

Set z1 to z2 mod z3.

Definition at line 147 of file arith_mulprec.h.

◆ zval_mul

#define zval_mul (   z1,
  z2,
  z3 
)    (mpz_mul(z1, z2, z3))

Set z1 to z2 times z3.

Definition at line 117 of file arith_mulprec.h.

◆ zval_neg

#define zval_neg (   z1,
  z2 
)    (mpz_neg(z1, z2))

Set z1 to -z2.

Definition at line 137 of file arith_mulprec.h.

◆ zval_print

#define zval_print (   z)    (mpz_out_str(stdout, 10, z))

Output z on stdout.

Definition at line 201 of file arith_mulprec.h.

◆ zval_set

#define zval_set (   z1,
  z2 
)    (mpz_set(z1, z2))

Set the value of z1 from z2.

Definition at line 82 of file arith_mulprec.h.

◆ zval_set_i

#define zval_set_i (   z,
 
)    (mpz_set_si(z, n))

Set the value of z from the signed long n.

Definition at line 87 of file arith_mulprec.h.

◆ zval_sgn

#define zval_sgn (   z)    (mpz_sgn(z))

Return +1 if z > 0, 0 if z = 0, and -1 if z < 0.

Definition at line 180 of file arith_mulprec.h.

◆ zval_sub

#define zval_sub (   z1,
  z2,
  z3 
)    (mpz_sub(z1, z2, z3))

Set z1 to z2 - z3.

Definition at line 112 of file arith_mulprec.h.

◆ zval_submul

#define zval_submul (   z1,
  z2,
  z3 
)    (mpz_submul(z1, z2, z3))

Set z1 to z1 - z2 times z3.

Definition at line 132 of file arith_mulprec.h.

Typedef Documentation

◆ qval_p

typedef mpq_ptr qval_p

Definition at line 216 of file arith_mulprec.h.

◆ qval_s

typedef __mpq_struct qval_s

Definition at line 214 of file arith_mulprec.h.

◆ qval_t

typedef qval_s qval_t[1]

Type of rational numbers.

Definition at line 221 of file arith_mulprec.h.

◆ zval_t

typedef mpz_t zval_t

Type of integer numbers.

Definition at line 67 of file arith_mulprec.h.