Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active October 26, 2022 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/b605f9193ecb7e4231fa to your computer and use it in GitHub Desktop.
Save komasaru/b605f9193ecb7e4231fa to your computer and use it in GitHub Desktop.
C source code to calculate values with GMP library.
/*************************************************************
* GMP(The GNU Multi Presicion Arithmetic Library) Test by C.
************************************************************/
#include <gmp.h>
int main(int argc, char *argv[])
{
// Declaration
mpz_t op1, op2, op3, op5, op6, op7, op8; // GMP Integer
mpz_t rop, rop2; // GMP Integer
unsigned int op4; // Unsigned integer
// Initialization
op4 = 100;
mpz_init(op1);
mpz_init(op2);
mpz_init(op3);
mpz_init(op5);
mpz_init(op6);
mpz_init(op7);
mpz_init(op8);
mpz_init(rop);
mpz_init(rop2);
mpz_set_ui(op1, 123456789); // GMP Unsigned integer
mpz_set_ui(op2, 987654); // GMP Unsigned integer
mpz_set_ui(op3, 2); // GMP Unsigned integer
mpz_set_ui(op5, op4); // GMP Unsigned integer
mpz_set_ui(op6, 123456789); // GMP Unsigned integer
mpz_set_ui(op7, 987654); // GMP Unsigned integer
mpz_set_si(op8, -999999999); // GMP Signed integer
// Addition
mpz_add(rop, op1, op2);
gmp_printf("%Zd + %Zd = %Zd\n", op1, op2, rop);
// Subtraction
mpz_sub(rop, op1, op2);
gmp_printf("%Zd - %Zd = %Zd\n", op1, op2, rop);
// Multiplication
mpz_mul(rop, op1, op2);
gmp_printf("%Zd * %Zd = %Zd\n", op1, op2, rop);
// Multiplication & addition
mpz_addmul(rop, op2, op3);
gmp_printf("+ %Zd * %Zd = %Zd\n", op2, op3, rop);
// Multiplication & subtraction
mpz_submul(rop, op2, op3);
gmp_printf("- %Zd * %Zd = %Zd\n", op2, op3, rop);
// Division(Remaider)
mpz_cdiv_q(rop, op6, op7);
gmp_printf("%Zd / %Zd = %Zd\n", op6, op7, rop);
mpz_cdiv_qr(rop, rop2, op6, op7);
gmp_printf("%Zd / %Zd = %Zd ... %Zd\n", op6, op7, rop, rop2);
mpz_fdiv_q(rop, op6, op7);
gmp_printf("%Zd / %Zd = %Zd\n", op6, op7, rop);
mpz_fdiv_qr(rop, rop2, op6, op7);
gmp_printf("%Zd / %Zd = %Zd ... %Zd\n", op6, op7, rop, rop2);
mpz_tdiv_q(rop, op6, op7);
gmp_printf("%Zd / %Zd = %Zd\n", op6, op7, rop);
mpz_tdiv_qr(rop, rop2, op6, op7);
gmp_printf("%Zd / %Zd = %Zd ... %Zd\n", op6, op7, rop, rop2);
// Modulo
mpz_mod(rop, op6, op7);
gmp_printf("%Zd mod %Zd = %Zd\n", op6, op7, rop);
// Left shift
mpz_mul_2exp(rop, op3, op4);
gmp_printf("%Zd << %d = %Zd\n", op3, op4, rop);
// Power
mpz_pow_ui(rop, op3, op4);
gmp_printf("%Zd ^ %d = %Zd\n", op3, op4, rop);
// Power and modulo
mpz_powm(rop, op3, op5, op6);
gmp_printf("(%Zd ^ %Zd) mod %Zd = %Zd\n", op3, op5, op6, rop);
// Negative value (Sign inversion)
mpz_neg(rop, op1);
gmp_printf("%Zd * (-1) = %Zd\n", op1, rop);
// Absolute value
mpz_abs(rop, op8);
gmp_printf("abs(%Zd) = %Zd\n", op8, rop);
// Square root
mpz_sqrt(rop, op1);
gmp_printf("sqrt(%Zd) = %Zd\n", op1, rop);
mpz_sqrtrem(rop, rop2, op1);
gmp_printf("sqrt(%Zd) = %Zd ... %Zd\n", op1, rop, rop2);
// Deallocation
mpz_clear(op1);
mpz_clear(op2);
mpz_clear(op3);
mpz_clear(op5);
mpz_clear(op6);
mpz_clear(op7);
mpz_clear(op8);
mpz_clear(rop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment