gmp_libmpq_set_str Method |
Namespace: Math.Gmp.Native
Assembly: Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)

public static int mpq_set_str( mpq_t rop, char_ptr str, int base )
Public Shared Function mpq_set_str ( rop As mpq_t, str As char_ptr, base As Integer ) As Integer
public: static int mpq_set_str( mpq_t^ rop, char_ptr str, int base )
static member mpq_set_str : rop : mpq_t * str : char_ptr * base : int -> int
Parameters
- rop
- Type: Math.Gmp.Nativempq_t
The result rational. - str
- Type: Math.Gmp.Nativechar_ptr
The source string. - base
- Type: SystemInt32
The base,
Return Value
Type: Int32The return value is 0 if the entire string is a valid number, or -1 if not.

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);
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
