static awk_value_t * convert_base(int nargs, awk_value_t *resval, int to_internal_base) { awk_value_t number_node, base_node; mpfr_t val; char * result; size_t len; int from_base, to_base; if (do_lint && nargs != 2) lintwarn(ext_id, _("convert_base: called with incorrect number of arguments")); mpfr_set_default_prec((int) NUMVAL(MPFR_PRECISION)); if (!get_argument(0, AWK_STRING, &number_node)) fatal(ext_id, _("first argument must be a string")); if (!get_argument(1, AWK_NUMBER, &base_node)) fatal(ext_id, _("second argument must be a number")); if (to_internal_base) { from_base = base_node.num_value; to_base = NUMVAL(MPFR_BASE); } else { from_base = NUMVAL(MPFR_BASE); to_base = base_node.num_value; } mpfr_init_set_str(val, number_node.str_value.str, from_base, (int) NUMVAL(MPFR_ROUND)); /* Set the return value */ result = malloc(10*(int) NUMVAL(MPFR_PRECISION)); len = mpfr_out_string(result, to_base, 0, val, (int) NUMVAL(MPFR_ROUND)); make_string_malloc(result, len, resval); free(result); mpfr_clear(val); return resval; }
static void overflows (void) { mpfr_t a, b; /* bug found by Ming J. Tsai <*****@*****.**>, 4 Oct 2003 */ mpfr_init_set_str (a, "5.1e32", 10, GMP_RNDN); mpfr_init (b); test_pow (b, a, a, GMP_RNDN); if (!(mpfr_inf_p (b) && mpfr_sgn (b) > 0)) { printf ("Error for a^a for a=5.1e32\n"); printf ("Expected +Inf, got "); mpfr_out_str (stdout, 10, 0, b, GMP_RNDN); printf ("\n"); exit (1); } mpfr_clear(a); mpfr_clear(b); }
int main() { mpfr_t b, p; mpfr_init (p); mpfr_init_set_str (b, "31", 10, GMP_RNDN); mpfr_mul_ui (p, b, 75, GMP_RNDU); /* generate product */ char *str = new char[50]; // 50 should be enough mp_exp_t exp; str = mpfr_get_str(str, &exp, 10, 0, p, GMP_RNDU); std::cout << str << " E " << exp << std::endl; delete[] str; #ifdef MPFR_VERSION std::cout << "version=" << MPFR_VERSION_MAJOR << "." << MPFR_VERSION_MINOR << "." << MPFR_VERSION_PATCHLEVEL << std::endl; #else // MPFR versions < 2.2.0 did not have version strings std::cout << "version=unknown" << std::endl; #endif return 0; }
static awk_value_t * mpfr_ordinary_op (int argc, awk_value_t *result, int arity, int is_predicate, void * ordinary_op) { mpfr_t number_mpfr[10]; char * result_func; int result_pred; size_t len; int i, base, precision; mp_rnd_t round; base = NUMVAL(MPFR_BASE); round = NUMVAL(MPFR_ROUND); precision = NUMVAL(MPFR_PRECISION); if (argc < arity) fatal(ext_id, _("too few arguments to MPFR function")); if (argc > 5) fatal(ext_id, _("too many arguments to MPFR function")); /* First optional argument is rounding mode. */ if (argc > arity) { awk_value_t val; if (!get_argument(arity+1, AWK_STRING, &val)) fatal(ext_id, _("optional round argument must be a string")); round = mpfr_get_round(val.str_value.str); } mpfr_set_default_prec(precision); /* Second optional argument is precision mode. */ if (argc > arity+1) { awk_value_t val; if (!get_argument(arity+2, AWK_NUMBER, &val)) fatal(ext_id, _("optional precision argument must be numeric")); precision = val.num_value; } for (i=0; i < arity; i++) { awk_value_t val; if (!get_argument(i, AWK_STRING, &val)) fatal(ext_id, _("missing required argument")); mpfr_init2(number_mpfr[i], precision); mpfr_set_str(number_mpfr[i], val.str_value.str, base, round); } switch (arity) { case 0: mpfr_init_set_str(number_mpfr[0], "0", base, round); if (is_predicate) { result_pred = ((constpred_t) ordinary_op) (); } else { set_exact(((constop_t) ordinary_op) (number_mpfr[0], round)); } break; case 1: if (is_predicate) { result_pred = ((unpred_t ) ordinary_op) (number_mpfr[0]); } else { set_exact(((unop_t ) ordinary_op) (number_mpfr[0], number_mpfr[0], round)); } break; case 2: if (is_predicate) { result_pred = ((binpred_t ) ordinary_op) (number_mpfr[0], number_mpfr[1]); } else { set_exact(((binop_t ) ordinary_op) (number_mpfr[0], number_mpfr[0], number_mpfr[1], round)); } break; } if (is_predicate) { make_number(result_pred, result); } else { result_func = malloc(10*(int) NUMVAL(MPFR_PRECISION)); len = mpfr_out_string(result_func, base, 0, number_mpfr[0], round); make_string_malloc(result_func, len, result); free(result_func); } for (i=0; i < arity; i++) mpfr_clear(number_mpfr[i]); return result; }
int main(int argc, char **argv) { double x, y; unsigned int n, D, p; mpfr_t X, Y, R; mpz_t N; char sf[50]; FILE *in; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("exp(%.*lf) ~= %.*lf (naive)\n", d(D), x, D, naive_exp(x, n)); else printf("Usage: %s a <x=Value for exp(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("exp(%.*lf) ~= %.*lf\n", d(D), x, D, taylor_exp(x, n)); else printf("Usage: %s b <x=Value for exp(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'c': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("ln(%.*lf) ~= %.*lf\n", d(D), x, D, taylor_nat_log(x, n)); else printf("Usage: %s c <x=Value for ln(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'd': if(argc == 6 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%lf", &y) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("pow(%.*lf, %.*lf) ~= %.*lf\n", d(D), x, d(D), y, D, taylor_pow(x, y, n)); else printf("Usage: %s d <x=Value for pow(x,y)> " "<y=Value for pow(x,y)> <n> " "<D=digits to display>\n", argv[0]); break; case 'e': if(argc == 6 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%lf", &y) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("log(%.*lf, %.*lf) ~= %.*lf\n", d(D), x, d(D), y, D, taylor_log(x, y, n)); else printf("Usage: %s e <x=Value for log(x, y)> " "<y=Value for log(x, y)> <n> " "<D=digits to display>\n", argv[0]); break; case 'f': if(argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if(mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "exp(%%.%uRNf) =~\t(naive)" "\n\t%%.%uRNf\n", d(D), D); mpfr_naive_exp(R, X, N); mpfr_printf(sf, X, R); } else printf("Usage: %s f <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s f <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); break; case 'g': if(argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if(mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "exp(%%.%uRNf) =~" "\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_exp(R, X, N); mpfr_printf(sf, X, R); } else printf("Usage: %s g <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); }
int main(int argc, char **argv) { double x, y; unsigned int n, p, D; mpfr_t R, X; char sf[50]; FILE *in; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Cos(%.*lf) = %.*lf\n", d(D), x, D, taylor_cos(x, n)); else printf("Usage: %s a <x=value for Cos(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Sin(%.*lf) = %.*lf\n", d(D), x, D, taylor_sin(x, n)); else printf("Usage: %s b <x=value for Sin(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'c': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Tan(%.*lf) = %.*lf\n", d(D), x, D, taylor_tan(x, n)); else printf("Usage: %s a <x=value for Tan(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'd': if(argc == 6 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); sprintf(sf, "cos(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_cos(R, X, n); mpfr_printf(sf, X, R); } else printf("Usage: %s d <x=value for Cos(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); } else printf("Usage: %s d <x=value for Cos(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); break; case 'e': if(argc == 6 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); sprintf(sf, "sin(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_sin(R, X, n); mpfr_printf(sf, X, R); } else printf("Usage: %s d <x=value for Sin(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); }
int main(int argc, char** argv) { double N, T; unsigned int n, D, p; mpfr_t Nr, Tr, R; int c; char sf[50]; if (argc == 1) { printf("Usage: %s [a/b/c/d/e] [arguments]\n", argv[0]); exit(1); } switch(argv[1][0]) { case 'a': if (argc == 5 && sscanf(argv[2], "%lf", &N) == 1 && sscanf(argv[3], "%lf", &T) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("sqrt(%.*lf) =~ %.*lf\n", d(D), N, D, bisect_sqrt(N, T)); else printf("Usage: %s a <N=Value to sqrt> " "<T=Tolerance> <D=Number of digits to display>\n", argv[0]); break; case 'b': if (argc == 6 && sscanf(argv[2], "%lf", &N) == 1 && sscanf(argv[3], "%lf", &T) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("%u_Root(%.*lf) =~ %.*lf\n", n, d(D), N, D, bisect_nRoot(N, T, n)); else printf("Usage: %s b <N=Value to root> <T=Tolerance>" "<n=nth Root> <D=Number of digits to display>\n", argv[0]); break; case 'c': if (argc == 5 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(Nr, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); //Sets the tolerance to Tr = 10^-D mpfr_digits_to_tolerance(D, Tr); //Generates the required format string sprintf(sf, "sqrt(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_bisect_sqrt(R, Nr, Tr); mpfr_printf(sf, Nr, R); } else printf("Usage: %s c <N=Value to sqrt> " "<D=Number of digits to calculate to> " "<p=bits of precision>\n", argv[0]); } else
int main(int argc, char **argv) { double x; unsigned int n, D, p; mpfr_t X, R; mpz_t N; char sf[50]; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("(%.*lf)^(%d) = %.*lf (Naive)\n", d(D), x, n, D, naive_int_exp(x, n)); else printf("Uasge: %s a <x=Base for exp> " "<n=Exponent for exp> " "<D=Number of digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("(%.*lf)^(%d) = %.*lf (Squaring)\n", d(D), x, n, D, squaring_int_exp(x, n)); else printf("Uasge: %s b <x=Base for exp> " "<n=Exponent for exp> " "<D=Number of digits to display>\n", argv[0]); break; case 'c': if (argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u" , &p) == 1) { mpfr_set_default_prec(p); if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN)==0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "(%%.%uRNf)^%%Zd =~\t(Naive)" "\n\t%%.%uRNf\n", d(D), D); mpfr_naive_int_exp(R, X, N); mpfr_printf(sf, X, N, R); } else printf("Usage: %s c <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s c <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); break; case 'd': if (argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u" , &p) == 1) { mpfr_set_default_prec(p); if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN)==0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "(%%.%uRNf)^%%Zd =~\t(Squaring)" "\n\t%%.%uRNf\n", d(D), D); mpfr_squaring_int_exp(R, X, N); mpfr_printf(sf, X, N, R); } else printf("Usage: %s d <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s d <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); break; default: printf("Usage: %s <a/b/c/d> <arguments>\n", argv[0]); } } else printf("Usage: %s <a/b/c/d> <arguments>\n", argv[0]); }