xref: /netbsd-src/external/lgpl3/mpc/dist/tests/trootofunity.c (revision 39f28e1e142c5bfb6be935a49cb55e2287fec7ea)
1 /* trootofunity -- test file for mpc_rootofunity.
2 
3 Copyright (C) 2012, 2016 INRIA
4 
5 This file is part of GNU MPC.
6 
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
19 */
20 
21 #include "mpc-tests.h"
22 
23 #define MPC_FUNCTION_CALL                                               \
24   P[0].mpc_inex = mpc_rootofunity (P[1].mpc, P[2].ui, P[3].ui, P[4].mpc_rnd)
25 
26 #include "data_check.tpl"
27 #include "tgeneric.tpl"
28 
29 static void
check(unsigned long int n)30 check (unsigned long int n)
31    /* checks whether zeta_n^n = 1, which is somewhat dangerous in floating
32       point                                                                */
33 {
34    mpc_t z, zero;
35    mpfr_prec_t prec;
36 
37    mpc_init2 (z, 2);
38    mpc_init2 (zero, 2);
39 
40    for (prec = 2*n; prec < 1000; prec = (mpfr_prec_t) (prec * 1.1 + 1)) {
41       mpc_set_prec (z, prec);
42       mpc_set_prec (zero, prec);
43 
44       mpc_rootofunity (z, n, 1, MPC_RNDNN);
45       mpc_pow_ui (zero, z, n, MPC_RNDNN);
46       mpc_sub_ui (zero, zero, 1u, MPC_RNDNN);
47       if (MPC_MAX (mpfr_get_exp (mpc_realref (zero)), mpfr_get_exp (mpc_imagref (zero)))
48           > - ((long int) prec - (long int) n)) {
49          fprintf (stderr, "rootofunity too imprecise for n=%lu\n", n);
50          MPC_OUT (z);
51          MPC_OUT (zero);
52          exit (1);
53       }
54    }
55 
56    mpc_clear (z);
57    mpc_clear (zero);
58 }
59 
60 
61 int
main(void)62 main (void)
63 {
64    unsigned long int n;
65 
66    for (n = 1; n < 10000; n += 10)
67       check (n);
68 
69    test_start ();
70 
71    data_check_template ("rootofunity.dsc", "rootofunity.dat");
72 
73    /* Avoid checking roots of unity of high order at very low precision,
74       so start only at 20. */
75    tgeneric_template ("rootofunity.dsc", 20, 512, 7, 256);
76 
77    test_end ();
78 
79    return 0;
80 }
81