1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/* tsqr -- test file for mpc_sqr.
Copyright (C) INRIA, 2002, 2005, 2008, 2010, 2011
This file is part of the MPC Library.
The MPC Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The MPC Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the MPC Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <stdio.h>
#include <stdlib.h>
#include "mpc-tests.h"
static void cmpsqr (mpc_srcptr x, mpc_rnd_t rnd)
/* computes the square of x with the specific function or by simple */
/* multiplication using the rounding mode rnd and compares the results */
/* and return values. */
/* In our current test suite, the real and imaginary parts of x have */
/* the same precision, and we use this precision also for the result. */
/* Furthermore, we check whether computing the square in the same */
/* place yields the same result. */
/* We also compute the result with four times the precision and check */
/* whether the rounding is correct. Error reports in this part of the */
/* algorithm might still be wrong, though, since there are two */
/* consecutive roundings. */
{
mpc_t z, t, u;
int inexact_z, inexact_t;
mpc_init2 (z, MPC_MAX_PREC (x));
mpc_init2 (t, MPC_MAX_PREC (x));
mpc_init2 (u, 4 * MPC_MAX_PREC (x));
inexact_z = mpc_sqr (z, x, rnd);
inexact_t = mpc_mul (t, x, x, rnd);
if (mpc_cmp (z, t))
{
fprintf (stderr, "sqr and mul differ for rnd=(%s,%s) \nx=",
mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr gives ");
mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
fprintf (stderr, "\nmpc_mul gives ");
mpc_out_str (stderr, 2, 0, t, MPC_RNDNN);
fprintf (stderr, "\n");
exit (1);
}
if (inexact_z != inexact_t)
{
fprintf (stderr, "The return values of sqr and mul differ for rnd=(%s,%s) \nx= ",
mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
fprintf (stderr, "\nx^2=");
mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr gives %i", inexact_z);
fprintf (stderr, "\nmpc_mul gives %i", inexact_t);
fprintf (stderr, "\n");
exit (1);
}
mpc_set (t, x, MPC_RNDNN);
inexact_t = mpc_sqr (t, t, rnd);
if (mpc_cmp (z, t))
{
fprintf (stderr, "sqr and sqr in place differ for rnd=(%s,%s) \nx=",
mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr gives ");
mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr in place gives ");
mpc_out_str (stderr, 2, 0, t, MPC_RNDNN);
fprintf (stderr, "\n");
exit (1);
}
if (inexact_z != inexact_t)
{
fprintf (stderr, "The return values of sqr and sqr in place differ for rnd=(%s,%s) \nx= ",
mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
fprintf (stderr, "\nx^2=");
mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr gives %i", inexact_z);
fprintf (stderr, "\nmpc_sqr in place gives %i", inexact_t);
fprintf (stderr, "\n");
exit (1);
}
mpc_sqr (u, x, rnd);
mpc_set (t, u, rnd);
if (mpc_cmp (z, t))
{
fprintf (stderr, "rounding in sqr might be incorrect for rnd=(%s,%s) \nx=",
mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr gives ");
mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
fprintf (stderr, "\nmpc_sqr quadruple precision gives ");
mpc_out_str (stderr, 2, 0, u, MPC_RNDNN);
fprintf (stderr, "\nand is rounded to ");
mpc_out_str (stderr, 2, 0, t, MPC_RNDNN);
fprintf (stderr, "\n");
exit (1);
}
mpc_clear (z);
mpc_clear (t);
mpc_clear (u);
}
static void
testsqr (long a, long b, mpfr_prec_t prec, mpc_rnd_t rnd)
{
mpc_t x;
mpc_init2 (x, prec);
mpc_set_si_si (x, a, b, rnd);
cmpsqr (x, rnd);
mpc_clear (x);
}
static void
reuse_bug (void)
{
mpc_t z1;
/* reuse bug found by Paul Zimmermann 20081021 */
mpc_init2 (z1, 2);
/* RE (z1^2) overflows, IM(z^2) = -0 */
mpfr_set_str (MPC_RE (z1), "0.11", 2, GMP_RNDN);
mpfr_mul_2si (MPC_RE (z1), MPC_RE (z1), mpfr_get_emax (), GMP_RNDN);
mpfr_set_ui (MPC_IM (z1), 0, GMP_RNDN);
mpc_conj (z1, z1, MPC_RNDNN);
mpc_sqr (z1, z1, MPC_RNDNN);
if (!mpfr_inf_p (MPC_RE (z1)) || mpfr_signbit (MPC_RE (z1))
||!mpfr_zero_p (MPC_IM (z1)) || !mpfr_signbit (MPC_IM (z1)))
{
printf ("Error: Regression, bug 20081021 reproduced\n");
MPC_OUT (z1);
exit (1);
}
mpc_clear (z1);
}
int
main (void)
{
DECL_FUNC (CC, f, mpc_sqr);
test_start ();
testsqr (247, -65, 8, 24);
testsqr (5, -896, 3, 2);
testsqr (-3, -512, 2, 16);
testsqr (266013312, 121990769, 27, 0);
testsqr (170, 9, 8, 0);
testsqr (768, 85, 8, 16);
testsqr (145, 1816, 8, 24);
testsqr (0, 1816, 8, 24);
testsqr (145, 0, 8, 24);
data_check (f, "sqr.dat");
tgeneric (f, 2, 1024, 1, 0);
reuse_bug ();
return 0;
}
|