Go to the previous, next section.

Rational Number Functions

All rational arithmetic functions canonicalize the result, so that the denominator and the numerator have no common factors. Zero has the unique representation 0/1.

The set of functions is quite small. Maybe it will be extended in a future release.

Function: void mpq_init (MP_RAT *dest_rational)

Initialize dest_rational with limb space and set the initial numeric value to 0/1. Each variable should normally only be initialized once, or at least cleared out (using the function mpq_clear) between each initialization.

Function: void mpq_clear (MP_RAT *rational_number)

Free the limb space occupied by rational_number. Make sure to call this function for all MP_RAT variables when you are done with them.

Function: void mpq_set (MP_RAT *dest_rational, MP_RAT *src_rational)

Assign dest_rational from src_rational.

Function: void mpq_set_ui (MP_RAT *rational_number, unsigned long int numerator, unsigned long int denominator)

Set the value of rational_number to numerator/denominator. If numerator and denominator have common factors, they are divided out before rational_number is assigned.

Function: void mpq_set_si (MP_RAT *rational_number, signed long int numerator, unsigned long int denominator)

Like mpq_set_ui, but numerator is signed.

Function: void mpq_add (MP_RAT *sum, MP_RAT *addend1, MP_RAT *addend2)

Set sum to addend1 + addend2.

Function: void mpq_sub (MP_RAT *difference, MP_RAT *minuend, MP_RAT *subtrahend)

Set difference to minuend - subtrahend.

Function: void mpq_mul (MP_RAT *product, MP_RAT *multiplicator, MP_RAT *multiplicand)

Set product to multiplicator * multiplicand

Function: void mpq_div (MP_RAT *quotient, MP_RAT *dividend, MP_RAT *divisor)

Set quotient to dividend / divisor.

Function: void mpq_neg (MP_RAT *negated_operand, MP_RAT *operand)

Set negated_operand to -operand.

Function: int mpq_cmp (MP_RAT *operand1, MP_RAT *operand2)

Compare operand1 and operand2. Return a positive value if operand1 > operand2, zero if operand1 = operand2, and a negative value if operand1 < operand2.

Function: void mpq_inv (MP_RAT *inverted_number, MP_RAT *number)

Invert number by swapping the numerator and denominator. If the new denominator becomes zero, this routine will divide by zero.

Function: void mpq_set_num (MP_RAT *rational_number, MP_INT *numerator)

Make numerator become the numerator of rational_number by copying.

Function: void mpq_set_den (MP_RAT *rational_number, MP_INT *denominator)

Make denominator become the denominator of rational_number by copying. If denominator < 0 the denominator of rational_number is set to the absolute value of denominator, and the sign of the numerator of rational_number is changed.

Function: void mpq_get_num (MP_INT *numerator, MP_RAT *rational_number)

Copy the numerator of rational_number to the integer numerator, to prepare for integer operations on the numerator.

Function: void mpq_get_den (MP_INT *denominator, MP_RAT *rational_number)

Copy the denominator of rational_number to the integer denominator, to prepare for integer operations on the denominator.

Go to the previous, next section.