summaryrefslogtreecommitdiff
path: root/tests/tcos.c
diff options
context:
space:
mode:
authorPhilippe Theveny <philippe.theveny@laposte.net>2008-04-09 10:47:19 +0000
committerPhilippe Theveny <philippe.theveny@laposte.net>2008-04-09 10:47:19 +0000
commit71e56ff6ca4bd81b5a6fe287e9e20b3e5bc8b59c (patch)
tree7c77abc85f4e757e2bd3a02fe41557744a990565 /tests/tcos.c
parent28c509642449eff7825618daffbfc2608ecdad3a (diff)
downloadmpc-git-71e56ff6ca4bd81b5a6fe287e9e20b3e5bc8b59c.tar.gz
File reorganisation into three new directories: src, tests and doc.
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/mpc/trunk@82 211d60ee-9f03-0410-a15a-8952a2c7a4e4
Diffstat (limited to 'tests/tcos.c')
-rw-r--r--tests/tcos.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/tcos.c b/tests/tcos.c
new file mode 100644
index 0000000..0ff00fc
--- /dev/null
+++ b/tests/tcos.c
@@ -0,0 +1,101 @@
+/* test file for mpc_cos.
+
+Copyright (C) 2007 INRIA.
+
+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 "gmp.h"
+#include "mpfr.h"
+#include "mpc.h"
+
+
+int
+main()
+{
+ mpc_t x, z, t, u;
+ mpfr_t f, g;
+ mp_prec_t prec;
+
+ mpc_init (x);
+ mpc_init (z);
+ mpc_init (t);
+ mpc_init (u);
+ mpfr_init (g);
+
+ for (prec = 2; prec <= 1000; prec++)
+ {
+ mpc_set_prec (x, prec);
+ mpc_set_prec (z, prec);
+ mpc_set_prec (t, prec);
+ mpc_set_prec (u, 4*prec);
+ mpfr_set_prec (g, prec);
+
+ /* check that cos(I*b) = cosh(b) */
+ mpfr_set_ui (MPC_RE (x), 0, GMP_RNDN);
+ mpfr_random (MPC_IM (x));
+
+ mpc_cos (z, x, MPC_RNDNN);
+ mpfr_cosh (g, MPC_IM(x), GMP_RNDN);
+ if (mpfr_cmp_ui (MPC_IM(z), 0) || mpfr_cmp (g, MPC_RE(z)))
+ {
+ fprintf (stderr, "Error in mpc_cos: cos(I*x) <> cosh(x)\n"
+ "got ");
+ mpc_out_str (stderr, 10, 0, z, MPC_RNDNN);
+ fprintf (stderr, "\nexpected ");
+ mpfr_set (MPC_RE (z), g, GMP_RNDN);
+ mpfr_set_ui (MPC_IM (z), 0, GMP_RNDN);
+ mpc_out_str (stderr, 10, 0, z, MPC_RNDNN);
+ fprintf (stderr, "\n");
+ exit (1);
+ }
+ }
+
+
+ /* 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_random (x);
+ mpc_cos (z, x, MPC_RNDNN);
+ mpc_cos (u, x, MPC_RNDNN);
+ mpc_set (t, u, MPC_RNDNN);
+
+ if (mpc_cmp (z, t))
+ {
+ fprintf (stderr, "rounding in cos might be incorrect for\nx=");
+ mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
+ fprintf (stderr, "\nmpc_cos gives ");
+ mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
+ fprintf (stderr, "\nmpc_cos 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 (x);
+ mpc_clear (z);
+ mpc_clear (t);
+ mpc_clear (u);
+ mpfr_clear (g);
+
+ return 0;
+}