2016-04-26 17:20:05 +02:00
|
|
|
/*
|
2013-05-27 16:21:37 +02:00
|
|
|
Copyright (C) 2012, 2013 Fredrik Johansson
|
2012-08-05 19:57:08 +02:00
|
|
|
|
2016-04-26 17:20:05 +02:00
|
|
|
This file is part of Arb.
|
|
|
|
|
|
|
|
Arb is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version. See <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-08-05 19:57:08 +02:00
|
|
|
|
|
|
|
#include "fmpr.h"
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2015-11-05 18:03:08 +00:00
|
|
|
slong iter;
|
2012-08-05 19:57:08 +02:00
|
|
|
flint_rand_t state;
|
|
|
|
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("add....");
|
2012-08-05 19:57:08 +02:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
flint_randinit(state);
|
|
|
|
|
2012-09-04 15:17:03 +02:00
|
|
|
/* test exact addition: (x + y) + z == x + (y + z) */
|
2018-12-13 16:49:21 +01:00
|
|
|
for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
|
2012-08-05 19:57:08 +02:00
|
|
|
{
|
2015-11-05 18:03:08 +00:00
|
|
|
slong bits, res1, res2, res3, res4;
|
2012-09-04 15:17:03 +02:00
|
|
|
fmpr_t x, y, z, t, u;
|
|
|
|
|
|
|
|
bits = 2 + n_randint(state, 200);
|
|
|
|
|
|
|
|
fmpr_init(x);
|
|
|
|
fmpr_init(y);
|
|
|
|
fmpr_init(z);
|
|
|
|
fmpr_init(t);
|
|
|
|
fmpr_init(u);
|
|
|
|
|
|
|
|
fmpr_randtest_special(x, state, bits, 10);
|
|
|
|
fmpr_randtest_special(y, state, bits, 10);
|
|
|
|
fmpr_randtest_special(z, state, bits, 10);
|
|
|
|
fmpr_randtest_special(t, state, bits, 10);
|
|
|
|
fmpr_randtest_special(u, state, bits, 10);
|
|
|
|
|
|
|
|
res1 = fmpr_add(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
res2 = fmpr_add(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
res3 = fmpr_add(u, y, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
res4 = fmpr_add(u, x, u, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
|
|
|
|
if (!fmpr_equal(t, u) ||
|
|
|
|
res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT ||
|
|
|
|
res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT)
|
|
|
|
{
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("FAIL\n\n");
|
|
|
|
flint_printf("bits = %wd\n", bits);
|
|
|
|
flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
|
|
|
|
flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
|
|
|
|
flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
|
|
|
|
flint_printf("t = "); fmpr_print(t); flint_printf("\n\n");
|
|
|
|
flint_printf("u = "); fmpr_print(u); flint_printf("\n\n");
|
2017-02-28 16:50:03 +01:00
|
|
|
flint_abort();
|
2012-09-04 15:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmpr_clear(x);
|
|
|
|
fmpr_clear(y);
|
|
|
|
fmpr_clear(z);
|
|
|
|
fmpr_clear(t);
|
|
|
|
fmpr_clear(u);
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:21:37 +02:00
|
|
|
/* compare with add_naive */
|
2018-12-13 16:49:21 +01:00
|
|
|
for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++)
|
2013-05-27 16:21:37 +02:00
|
|
|
{
|
2015-11-05 18:03:08 +00:00
|
|
|
slong prec, ret1, ret2;
|
2013-05-27 16:21:37 +02:00
|
|
|
fmpr_t x, y, z, w;
|
|
|
|
fmpr_rnd_t rnd;
|
|
|
|
|
|
|
|
fmpr_init(x);
|
|
|
|
fmpr_init(y);
|
|
|
|
fmpr_init(z);
|
|
|
|
fmpr_init(w);
|
|
|
|
|
|
|
|
fmpr_randtest_special(x, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));
|
|
|
|
fmpr_randtest_special(y, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));
|
|
|
|
fmpr_randtest_special(z, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));
|
|
|
|
|
|
|
|
prec = 2 + n_randint(state, 1000);
|
|
|
|
if (n_randint(state, 10) == 0 &&
|
|
|
|
fmpz_bits(fmpr_expref(x)) < 10 &&
|
|
|
|
fmpz_bits(fmpr_expref(y)) < 10)
|
|
|
|
prec = FMPR_PREC_EXACT;
|
|
|
|
|
|
|
|
switch (n_randint(state, 4))
|
|
|
|
{
|
|
|
|
case 0: rnd = FMPR_RND_DOWN; break;
|
|
|
|
case 1: rnd = FMPR_RND_UP; break;
|
|
|
|
case 2: rnd = FMPR_RND_FLOOR; break;
|
|
|
|
default: rnd = FMPR_RND_CEIL; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret1 = fmpr_add(z, x, y, prec, rnd);
|
|
|
|
ret2 = fmpr_add_naive(w, x, y, prec, rnd);
|
|
|
|
|
2014-03-10 16:34:13 +01:00
|
|
|
if (!fmpr_equal(z, w) || ret1 != ret2 ||
|
|
|
|
!fmpr_check_ulp(z, ret1, prec) || !fmpr_check_ulp(w, ret2, prec))
|
2013-05-27 16:21:37 +02:00
|
|
|
{
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("FAIL\n\n");
|
|
|
|
flint_printf("iter %wd\n", iter);
|
|
|
|
flint_printf("prec = %wd\n", prec);
|
|
|
|
flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
|
|
|
|
flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
|
|
|
|
flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
|
|
|
|
flint_printf("w = "); fmpr_print(w); flint_printf("\n\n");
|
|
|
|
flint_printf("ret1 = %wd, ret2 = %wd\n", ret1, ret2);
|
2017-02-28 16:50:03 +01:00
|
|
|
flint_abort();
|
2013-05-27 16:21:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmpr_clear(x);
|
|
|
|
fmpr_clear(y);
|
|
|
|
fmpr_clear(z);
|
|
|
|
fmpr_clear(w);
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:17:03 +02:00
|
|
|
/* compare rounding with mpfr */
|
2018-12-13 16:49:21 +01:00
|
|
|
for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++)
|
2012-09-04 15:17:03 +02:00
|
|
|
{
|
2015-11-05 18:03:08 +00:00
|
|
|
slong bits, res;
|
2012-09-04 15:17:03 +02:00
|
|
|
int mpfr_res;
|
2012-08-05 19:57:08 +02:00
|
|
|
fmpr_t x, y, z, w;
|
|
|
|
mpfr_t X, Y, Z;
|
|
|
|
|
2013-05-27 16:21:37 +02:00
|
|
|
bits = 2 + n_randint(state, 500);
|
2012-08-05 19:57:08 +02:00
|
|
|
|
|
|
|
fmpr_init(x);
|
|
|
|
fmpr_init(y);
|
|
|
|
fmpr_init(z);
|
|
|
|
fmpr_init(w);
|
|
|
|
|
2013-05-27 16:21:37 +02:00
|
|
|
mpfr_init2(X, bits + 500);
|
|
|
|
mpfr_init2(Y, bits + 500);
|
2012-08-05 19:57:08 +02:00
|
|
|
mpfr_init2(Z, bits);
|
|
|
|
|
2013-05-27 16:21:37 +02:00
|
|
|
fmpr_randtest_special(x, state, bits + n_randint(state, 500), 10);
|
|
|
|
fmpr_randtest_special(y, state, bits + n_randint(state, 500), 10);
|
2012-08-05 19:57:08 +02:00
|
|
|
fmpr_randtest_special(z, state, bits, 10);
|
|
|
|
|
|
|
|
fmpr_get_mpfr(X, x, MPFR_RNDN);
|
|
|
|
fmpr_get_mpfr(Y, y, MPFR_RNDN);
|
|
|
|
|
|
|
|
switch (n_randint(state, 4))
|
|
|
|
{
|
|
|
|
case 0:
|
2012-09-04 15:17:03 +02:00
|
|
|
mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDZ);
|
|
|
|
res = fmpr_add(z, x, y, bits, FMPR_RND_DOWN);
|
2012-08-05 19:57:08 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2012-09-04 15:17:03 +02:00
|
|
|
mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDA);
|
|
|
|
res = fmpr_add(z, x, y, bits, FMPR_RND_UP);
|
2012-08-05 19:57:08 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2012-09-04 15:17:03 +02:00
|
|
|
mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDD);
|
|
|
|
res = fmpr_add(z, x, y, bits, FMPR_RND_FLOOR);
|
2012-08-05 19:57:08 +02:00
|
|
|
break;
|
2012-09-04 15:17:03 +02:00
|
|
|
default:
|
|
|
|
mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDU);
|
|
|
|
res = fmpr_add(z, x, y, bits, FMPR_RND_CEIL);
|
2012-08-05 19:57:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmpr_set_mpfr(w, Z);
|
|
|
|
|
2014-03-10 16:34:13 +01:00
|
|
|
if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0)
|
|
|
|
|| !fmpr_check_ulp(z, res, bits))
|
2012-08-05 19:57:08 +02:00
|
|
|
{
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("FAIL\n\n");
|
|
|
|
flint_printf("iter %wd\n", iter);
|
|
|
|
flint_printf("bits = %wd\n", bits);
|
|
|
|
flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
|
|
|
|
flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
|
|
|
|
flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
|
|
|
|
flint_printf("w = "); fmpr_print(w); flint_printf("\n\n");
|
|
|
|
flint_printf("returned: %wd, %d\n", res, mpfr_res);
|
2017-02-28 16:50:03 +01:00
|
|
|
flint_abort();
|
2012-08-05 19:57:08 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:17:03 +02:00
|
|
|
/* check error bound */
|
|
|
|
if (!fmpr_is_nan(z) && !fmpr_is_inf(z))
|
|
|
|
{
|
|
|
|
fmpr_t z_exact, error_bound, true_error;
|
|
|
|
|
|
|
|
fmpr_init(z_exact);
|
|
|
|
fmpr_init(error_bound);
|
|
|
|
fmpr_init(true_error);
|
|
|
|
|
|
|
|
fmpr_set_error_result(error_bound, z, res);
|
|
|
|
fmpr_add(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
|
|
|
|
fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN);
|
|
|
|
fmpr_abs(true_error, true_error);
|
|
|
|
|
|
|
|
if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) ||
|
|
|
|
fmpr_cmp(true_error, error_bound) > 0)
|
|
|
|
{
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("FAIL: error bound\n\n");
|
|
|
|
flint_printf("bits = %wd\n", bits);
|
|
|
|
flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
|
|
|
|
flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
|
|
|
|
flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
|
|
|
|
flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n");
|
|
|
|
flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n");
|
|
|
|
flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n");
|
2017-02-28 16:50:03 +01:00
|
|
|
flint_abort();
|
2012-09-04 15:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmpr_clear(z_exact);
|
|
|
|
fmpr_clear(error_bound);
|
|
|
|
fmpr_clear(true_error);
|
|
|
|
}
|
|
|
|
|
2012-08-05 19:57:08 +02:00
|
|
|
fmpr_clear(x);
|
|
|
|
fmpr_clear(y);
|
|
|
|
fmpr_clear(z);
|
|
|
|
fmpr_clear(w);
|
|
|
|
|
|
|
|
mpfr_clear(X);
|
|
|
|
mpfr_clear(Y);
|
|
|
|
mpfr_clear(Z);
|
|
|
|
}
|
|
|
|
|
|
|
|
flint_randclear(state);
|
2013-07-30 13:53:10 +02:00
|
|
|
flint_cleanup();
|
2015-11-06 16:17:27 +00:00
|
|
|
flint_printf("PASS\n");
|
2012-08-05 19:57:08 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|