gmp_libmpq_inp_str Method |
Namespace: Math.Gmp.Native
Any initial white-space characters are read and discarded.
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 GNU MP - Rational Number Functions).
The base can be between 2 and 36, or can be 0 in which case the leading characters of the string determine the base, "0x" or "0X" for hexadecimal, "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.
// Create, initialize, and set the value of op to 123/456. mpq_t op = new mpq_t(); gmp_lib.mpq_init(op); // Write rational to a temporary file. string pathname = System.IO.Path.GetTempFileName(); System.IO.File.WriteAllText(pathname, "123/456"); // Read op from the temporary file, and assert that the number of bytes read is 7. ptr<FILE> stream = new ptr<FILE>(); _wfopen_s(out stream.Value.Value, pathname, "r"); Assert.IsTrue(gmp_lib.mpq_inp_str(op, stream, 10) == 7); fclose(stream.Value.Value); // Assert that op is 123/456. Assert.IsTrue(gmp_lib.mpq_cmp_ui(op, 123, 456U) == 0); // Delete temporary file. System.IO.File.Delete(pathname); // Release unmanaged memory allocated for op. gmp_lib.mpq_clear(op);