6.2 Conversion Functions

Function: double mpq_get_d (const mpq_t op)

Convert op to a double, truncating if necessary (i.e. rounding towards zero).

If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur.

Function: void mpq_set_d (mpq_t rop, double op)
Function: void mpq_set_f (mpq_t rop, const mpf_t op)

Set rop to the value of op. There is no rounding, this conversion is exact.

Function: char * mpq_get_str (char *str, int base, const mpq_t op)

Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. The string will be of the form ‘num/den’, or if the denominator is 1 then just ‘num’.

For base in the range 2..36, digits and lower-case letters are used; for −2..−36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used.

If str is NULL, the result string is allocated using the current allocation function (see Custom Allocation). The block will be strlen(str)+1 bytes, that being exactly enough for the string and null-terminator.

If str is not NULL, it should point to a block of storage large enough for the result, that being

mpz_sizeinbase (mpq_numref(op), base)
+ mpz_sizeinbase (mpq_denref(op), base) + 3

The three extra bytes are for a possible minus sign, possible slash, and the null-terminator.

A pointer to the result string is returned, being either the allocated block, or the given str.