summaryrefslogtreecommitdiff
path: root/texp.c
blob: 8450f30f6b292f81f43dab7e66fd24dc510ab685 (plain)
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
/* test file for mpc_exp.

Copyright (C) 2002 Andreas Enge, Paul Zimmermann

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 (f);
   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 (f, prec);
      mpfr_set_prec (g, prec);

      /* check that exp(I*b) = cos(b) + I*sin(b) */
      mpfr_set_ui (MPC_RE (x), 0, GMP_RNDN);
      mpfr_random (MPC_IM (x));

      mpc_exp (z, x, MPC_RNDNN);
      mpfr_sin_cos (f, g, MPC_IM(x), GMP_RNDN);
      if (mpfr_cmp (g, MPC_RE(z)) || mpfr_cmp (f, MPC_IM(z)))
      {
         fprintf (stderr, "Error in mpc_exp: exp(I*x) <> cos(x)+I*sin(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 (MPC_IM(z), f, 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_exp (z, x, MPC_RNDNN);
    mpc_exp (t, x, MPC_RNDNN);
    mpc_set (u, t, MPC_RNDNN);

    if (mpc_cmp (z, t))
    {
       fprintf (stderr, "rounding in exp might be incorrect for\nx=");
       mpc_out_str (stderr, 2, 0, x, MPC_RNDNN);
       fprintf (stderr, "\nmpc_exp                     gives ");
       mpc_out_str (stderr, 2, 0, z, MPC_RNDNN);
       fprintf (stderr, "\nmpc_exp 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 (f);
    mpfr_clear (g);

    return 0;
}