gmp_libmpq_set_str Method |
Namespace: Math.Gmp.Native
The string can be an integer like "41" or a fraction like "41/152". The fraction must be in canonical form (see GNU MP - Rational Number Functions), or if not then mpq_canonicalize must be called.
The numerator and optional denominator are parsed the same as in mpz_set_str (see GNU MP - Assigning Integers). White space is allowed in the string, and is simply ignored. The base can vary from 2 to 62, or if base is 0 then the leading characters are used: 0x or 0X for hex, 0b or 0B for binary, 0 for octal, or decimal otherwise. Note that this is done separately for the numerator and denominator, so for instance 0xEF/100 is 239/100, whereas 0xEF/0x100 is 239/256.
// Create and initialize a new rational x. mpq_t x = new mpq_t(); gmp_lib.mpq_init(x); // Set the value of x. char_ptr value = new char_ptr("12 345 678 909 876 543 211 234 567 890 987 654 321 / 234 567 890"); gmp_lib.mpq_set_str(x, value, 10); // Assert the value of x. char_ptr s = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x); Assert.IsTrue(s.ToString() == value.ToString().Replace(" ", "")); // Release unmanaged memory allocated for x and string values. gmp_lib.mpq_clear(x); gmp_lib.free(value); gmp_lib.free(s);