I/O of Rationals (GNU MP 6.2.1)
Description: How to install and use the GNU multiple precision arithmetic library, version 6.2.1.

Previous: Applying Integer Functions, Up: Rational Number Functions [Index]

### 6.6 Input and Output Functions

Functions that perform input from a stdio stream, and functions that output to a stdio stream, of `mpq` numbers. Passing a `NULL` pointer for a stream argument to any of these functions will make them read from `stdin` and write to `stdout`, respectively.

When using any of these functions, it is a good idea to include `stdio.h` before `gmp.h`, since that will allow `gmp.h` to define prototypes for these functions.

See also Formatted Output and Formatted Input.

Function: size_t mpq_out_str (FILE *stream, int base, const mpq_t op)

  Output op on stdio stream stream, as a string of digits in base base. The base argument may vary from 2 to 62 or from -2 to -36. Output is in 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.

  Return the number of bytes written, or if an error occurred, return 0.

Function: size_t mpq_inp_str (mpq_t rop, FILE *stream, int base)

  Read a string of digits from stream and convert them to a rational in rop. Any initial white-space characters are read and discarded. Return the number of characters read (including white space), or 0 if a rational could not be read.

  The input can be a fraction like ‘`17/63`’ or just an integer like ‘`123`’. Reading stops at the first character not in this form, and white space is not permitted within the string. If the input might not be in canonical form, then `mpq_canonicalize` must be called (see Rational Number Functions).

  The base can be between 2 and 62, or can be 0 in which case the leading characters of the string determine the base, ‘`0x`’ or ‘`0X`’ for hexadecimal, `0b` and `0B` for binary, ‘`0`’ for octal, or decimal otherwise. The leading characters are examined separately for the numerator and denominator of a fraction, so for instance ‘`0x10/11`’ is 16/11, whereas ‘`0x10/0x11`’ is 16/17.

Previous: Applying Integer Functions, Up: Rational Number Functions [Index]