gmp_libmpz_gcdext Method |
Namespace: Math.Gmp.Native
The value in g is always positive, even if one or both of a and b are negative (or zero if both inputs are zero). The values in s and t are chosen such that normally, | s | < | b | / (2 g) and | t | < | a | / (2 g), and these relations define s and t uniquely. There are a few exceptional cases:
If | a | = | b |, then s = 0, t = sgn(b).
Otherwise, s = sgn(a) if b = 0 or | b | = 2 g, and t = sgn(b) if a = 0 or | a | = 2 g.
In all cases, s = 0 if and only if g = | b |, i.e., if b divides a or a = b = 0.
If t is null then that value is not computed.
// Create, initialize, and set the value of op1 to 63. mpz_t a = new mpz_t(); gmp_lib.mpz_init_set_ui(a, 63U); // Create, initialize, and set the value of op2 to 70. mpz_t b = new mpz_t(); gmp_lib.mpz_init_set_ui(b, 70U); // Create, initialize, and set the values of g, s, and t to 0. mpz_t g = new mpz_t(); mpz_t s = new mpz_t(); mpz_t t = new mpz_t(); gmp_lib.mpz_inits(g, s, t, null); // Set g to the the greatest common divisor of a and b, and set s and t such that a * s + b * t = g. gmp_lib.mpz_gcdext(g, s, t, a, b); // Assert that g is 7, and that s and t are respectively -1 and 1. Assert.IsTrue(gmp_lib.mpz_get_si(g) == 7); Assert.IsTrue(gmp_lib.mpz_get_si(s) == -1); Assert.IsTrue(gmp_lib.mpz_get_si(t) == 1); // Release unmanaged memory allocated for g, s, t, a, and b. gmp_lib.mpz_clears(g, s, t, a, b, null);