void mp_Imul (mp_interval_t *rop, mp_interval_t op1, mp_interval_t op2) { // DECLARE AUX MPFR_T VARIABLE TO STORE CROSSED PRODUCTS mpfr_t aux; mpfr_init (aux); mpfr_t ropA; mpfr_init (ropA); mpfr_t ropB; mpfr_init (ropB); // FIRST CANDIDATE TO LEFT BOUNDARY mpfr_mul (ropA, op1.a, op2.a, MPFR_RNDD); mpfr_mul (aux, op1.a, op2.b, MPFR_RNDD); mpfr_min (ropA, ropA, aux, MPFR_RNDD); mpfr_mul (aux, op1.b, op2.a, MPFR_RNDD); mpfr_min (ropA, ropA, aux, MPFR_RNDD); mpfr_mul (aux, op1.b, op2.b, MPFR_RNDD); mpfr_min (ropA, ropA, aux, MPFR_RNDD); // FIRST CANDIDATE TO RIGHT BOUNDARY mpfr_mul (ropB, op1.a, op2.a, MPFR_RNDU); mpfr_mul (aux, op1.a, op2.b, MPFR_RNDU); mpfr_max (ropB, ropB, aux, MPFR_RNDU); mpfr_mul (aux, op1.b, op2.a, MPFR_RNDU); mpfr_max (ropB, ropB, aux, MPFR_RNDU); mpfr_mul (aux, op1.b, op2.b, MPFR_RNDU); mpfr_max (rop->b, ropB, aux, MPFR_RNDU); mpfr_set (rop->a, ropA, MPFR_RNDD); // CLEAN AUX VARIABLE mpfr_clear (aux); mpfr_clear (ropA); mpfr_clear (ropB); }
real max(const real & a, const real & b) { real x; mpfr_max(x.r, a.r, b.r, MPFR_RNDN); return x; }
SeedValue seed_mpfr_max (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op1, op2; gint ret; CHECK_ARG_COUNT("mpfr.max", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); if ( seed_value_is_object_of_class(ctx, args[0], mpfr_class) && seed_value_is_object_of_class(ctx, args[1], mpfr_class)) { op1 = seed_object_get_private(args[0]); op2 = seed_object_get_private(args[1]); } else { TYPE_EXCEPTION("mpfr.max", "mpfr_t"); } ret = mpfr_max(rop, op1, op2, rnd); return seed_value_from_int(ctx, ret, exception); }
void mpfr_bisect_nRoot(mpfr_t R, mpfr_t N, mpfr_t T, unsigned int n) { if(mpfr_cmp_ui(N, 0) < 0) { fprintf(stderr, "The value to square root must be non-negative\n"); exit(-1); } if(mpfr_cmp_ui(T, 0) < 0) { fprintf(stderr, "The tolerance must be non-negative\n"); exit(-1); } assert(n >= 2); mpfr_t a, b, x, f, d, fab; //Set a == 0 mpfr_init_set_ui(a, 0, MPFR_RNDN); //Set b = max{1, N} mpfr_init(b); mpfr_max(b, MPFR_ONE, N, MPFR_RNDN); //Set x = (a + b)/2 mpfr_init(x); mpfr_add(x, a, b, MPFR_RNDN); mpfr_mul(x, x, MPFR_HALF, MPFR_RNDN); //Set f = x^2 - N mpfr_init(f); mpfr_init(fab); mpfr_pow_ui(f, x, n, MPFR_RNDN); mpfr_sub(f, f, N, MPFR_RNDN); mpfr_abs(fab, f, MPFR_RNDN); //Set d = b - a mpfr_init(d); mpfr_sub(d, b, a, MPFR_RNDN); while(mpfr_cmp(fab, T) > 0 && mpfr_cmp(d, T) > 0) { //Update the bounds, a and b if(mpfr_cmp_ui(f, 0) < 0) mpfr_set(a, x, MPFR_RNDN); else mpfr_set(b, x, MPFR_RNDN); //Update x mpfr_add(x, a, b, MPFR_RNDN); mpfr_mul(x, x, MPFR_HALF, MPFR_RNDN); //Update f mpfr_pow_ui(f, x, n, MPFR_RNDN); mpfr_sub(f, f, N, MPFR_RNDN); mpfr_abs(fab, f, MPFR_RNDN); } mpfr_set(R, x, MPFR_RNDN); }
int mp_IntersectInterval (mp_interval_t *rop, mp_interval_t op1, mp_interval_t op2) { //rop->a = MAX (op1.a, op2.a); mpfr_max (rop->a, op1.a, op2.a, MPFR_RNDD); //rop->b = MIN (op1.b, op2.b); mpfr_min (rop->b, op1.b, op2.b, MPFR_RNDU); //if (rop->a < rop->b) return 1; if (mpfr_cmp (rop->a, rop->b) < 0) return 1; return 0; }
int main (void) { mpfr_t x, y, z; tests_start_mpfr (); mpfr_init (x); mpfr_init (y); mpfr_init (z); /* case x=NaN && y=NAN */ mpfr_set_nan (x); mpfr_set_nan (y); mpfr_min (z, x, y, MPFR_RNDN); if (!mpfr_nan_p (z)) { printf ("Error in mpfr_min (NaN, NaN)\n"); exit (1); } mpfr_max (z, x, y, MPFR_RNDN); if (!mpfr_nan_p (z)) { printf ("Error in mpfr_max (NaN, NaN)\n"); exit (1); } /* case x=NaN */ mpfr_set_nan (x); mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_min (z, x, y, MPFR_RNDN); if (mpfr_cmp_ui (z, 0)) { printf ("Error in mpfr_min (NaN, 0)\n"); exit (1); } mpfr_min (z, y, x, MPFR_RNDN); if (mpfr_cmp_ui (z, 0)) { printf ("Error in mpfr_min (0, NaN)\n"); exit (1); } mpfr_max (z, x, y, MPFR_RNDN); if (mpfr_cmp_ui (z, 0)) { printf ("Error in mpfr_max (NaN, 0)\n"); exit (1); } mpfr_max (z, y, x, MPFR_RNDN); if (mpfr_cmp_ui (z, 0)) { printf ("Error in mpfr_max (0, NaN)\n"); exit (1); } /* Case x=0+ and x=0- */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_set_ui (y, 0, MPFR_RNDN); MPFR_SET_NEG(y); mpfr_max (z, x, y, MPFR_RNDN); if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z)) { printf ("Error in mpfr_max (0+, 0-)\n"); exit (1); } mpfr_min (z, x, y, MPFR_RNDN); if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z)) { printf ("Error in mpfr_min (0+, 0-)\n"); exit (1); } /* Case x=0- and y=0+ */ mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_SET_NEG(x); mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_max (z, x, y, MPFR_RNDN); if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z)) { printf ("Error in mpfr_max (0+, 0-)\n"); exit (1); } mpfr_min (z, x, y, MPFR_RNDN); if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z)) { printf ("Error in mpfr_min (0+, 0-)\n"); exit (1); } /* case x=+Inf */ mpfr_set_inf (x, 1); mpfr_set_si (y, -12, MPFR_RNDN); mpfr_min (z, x, y, MPFR_RNDN); if ( mpfr_cmp_si (z, -12) ) { printf ("Error in mpfr_min (+Inf, -12)\n"); exit (1); } mpfr_max (z, x, y, MPFR_RNDN); if ( !MPFR_IS_INF(z) || MPFR_IS_NEG(z) ) { printf ("Error in mpfr_max (+Inf, 12)\n"); exit (1); } /* case x=-Inf */ mpfr_set_inf (x, -1); mpfr_set_ui (y, 12, MPFR_RNDN); mpfr_max (z, x, y, MPFR_RNDN); if ( mpfr_cmp_ui (z, 12) ) { printf ("Error in mpfr_max (-Inf, 12)\n"); exit (1); } mpfr_min (z, x, y, MPFR_RNDN); if ( !MPFR_IS_INF(z) || MPFR_IS_POS(z) ) { printf ("Error in mpfr_min (-Inf, 12)\n"); exit (1); } /* case x=17 and y=42 */ mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_set_ui (y, 42, MPFR_RNDN); mpfr_max (z, x, y, MPFR_RNDN); if ( mpfr_cmp_ui (z, 42) ) { printf ("Error in mpfr_max (17, 42)\n"); exit (1); } mpfr_max (z, y, x, MPFR_RNDN); if ( mpfr_cmp_ui (z, 42) ) { printf ("Error in mpfr_max (42, 17)\n"); exit (1); } mpfr_min (z, y, x, MPFR_RNDN); if ( mpfr_cmp_ui (z, 17) ) { printf ("Error in mpfr_min (42, 17)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); tests_end_mpfr (); return 0; }