1 /*
2 test file for add
3 
4 Copyright (C) 2009, 2010 Andreas Enge
5 
6 This file is part of the MPFRCX Library.
7 
8 The MPFRCX Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The MPFRCX Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the MPFRCX library; see the file COPYING.LESSER.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA.
22 */
23 
24 #include <stdio.h>
25 #include "mpfrcx.h"
26 
27 
error(mpcx_t h1,mpcx_t h2,mpcx_t f,mpcx_t g)28 static void error (mpcx_t h1, mpcx_t h2, mpcx_t f, mpcx_t g) {
29    fprintf (stderr, "Error in add: f + g yields h1 and h2 with\nf: ");
30    mpcx_out_str (stderr, 10, 0, f);
31    fprintf (stderr, "\ng: ");
32    mpcx_out_str (stderr, 10, 0, g);
33    fprintf (stderr, "\nh1: ");
34    mpcx_out_str (stderr, 10, 0, h1);
35    fprintf (stderr, "\nh2: ");
36    mpcx_out_str (stderr, 10, 0, h2);
37    fprintf (stderr, "\n");
38    exit (1);
39 }
40 
41 
check_add(mpcx_t f,mpcx_t g)42 static void check_add (mpcx_t f, mpcx_t g) {
43    /* checks whether addition in place works; f and g must have the
44       same precision */
45    mpcx_t f2, g2, h;
46 
47    mpcx_init_set (f2, f);
48    mpcx_init_set (g2, g);
49    mpcx_init (h, 10, mpcx_get_prec (f));
50 
51    mpcx_add (h, f, g);
52 
53    /* check for difference between normal and in place operation */
54    mpcx_add (f2, f2, g);
55    if (mpcx_cmp (h, f2))
56       error (h, f2, f, g);
57 
58    /* check for difference between normal and in place operation */
59    mpcx_add (g2, f, g2);
60    if (mpcx_cmp (h, g2))
61       error (h, g2, f, g);
62 
63    /* check whether f+g == f - (-g) */
64    mpcx_neg (g2, g);
65    mpcx_sub (f2, f, g2);
66    if (mpcx_cmp (h, f2))
67       error (h, f2, f, g);
68 
69    /* check whether f+g = g+f; should hold even in floating point */
70    mpcx_add (f2, g, f);
71    if (mpcx_cmp (h, f2))
72       error (h, f2, f, g);
73 
74    mpcx_clear (f2);
75    mpcx_clear (g2);
76    mpcx_clear (h);
77 }
78 
79 
check_add_random(gmp_randstate_t state)80 static void check_add_random (gmp_randstate_t state) {
81    int deg;
82    mpcx_t f, g;
83 
84    mpcx_init (f, 10, 103);
85    mpcx_init (g, 11, 103);
86 
87    for (deg = -1; deg <= 1000; deg += 10) {
88       mpcx_urandom (f, deg, state);
89       mpcx_urandom (g, deg, state);
90       check_add (f, g);
91       check_add (f, f);
92    }
93 
94    mpcx_clear (f);
95    mpcx_clear (g);
96 }
97 
98 
main(void)99 int main (void) {
100    gmp_randstate_t state;
101 
102    gmp_randinit_default (state);
103 
104    check_add_random (state);
105 
106    gmp_randclear (state);
107 
108    return 0;
109 }
110