1*90a8ff21Smrg /* mpc_sum -- Add an array of complex numbers.
2*90a8ff21Smrg
3*90a8ff21Smrg Copyright (C) 2018 INRIA
4*90a8ff21Smrg
5*90a8ff21Smrg This file is part of GNU MPC.
6*90a8ff21Smrg
7*90a8ff21Smrg GNU MPC is free software; you can redistribute it and/or modify it under
8*90a8ff21Smrg the terms of the GNU Lesser General Public License as published by the
9*90a8ff21Smrg Free Software Foundation; either version 3 of the License, or (at your
10*90a8ff21Smrg option) any later version.
11*90a8ff21Smrg
12*90a8ff21Smrg GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13*90a8ff21Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14*90a8ff21Smrg FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15*90a8ff21Smrg more details.
16*90a8ff21Smrg
17*90a8ff21Smrg You should have received a copy of the GNU Lesser General Public License
18*90a8ff21Smrg along with this program. If not, see http://www.gnu.org/licenses/ .
19*90a8ff21Smrg */
20*90a8ff21Smrg
21*90a8ff21Smrg #include <stdio.h> /* for MPC_ASSERT */
22*90a8ff21Smrg #include "mpc-impl.h"
23*90a8ff21Smrg
24*90a8ff21Smrg int
mpc_sum(mpc_ptr sum,const mpc_ptr * z,unsigned long n,mpc_rnd_t rnd)25*90a8ff21Smrg mpc_sum (mpc_ptr sum, const mpc_ptr *z, unsigned long n, mpc_rnd_t rnd)
26*90a8ff21Smrg {
27*90a8ff21Smrg int inex_re, inex_im;
28*90a8ff21Smrg mpfr_ptr *t;
29*90a8ff21Smrg unsigned long i;
30*90a8ff21Smrg
31*90a8ff21Smrg t = (mpfr_ptr *) malloc (n * sizeof(mpfr_t));
32*90a8ff21Smrg /* warning: when n=0, malloc() might return NULL (e.g., gcc119) */
33*90a8ff21Smrg MPC_ASSERT(n == 0 || t != NULL);
34*90a8ff21Smrg for (i = 0; i < n; i++)
35*90a8ff21Smrg t[i] = mpc_realref (z[i]);
36*90a8ff21Smrg inex_re = mpfr_sum (mpc_realref (sum), t, n, MPC_RND_RE (rnd));
37*90a8ff21Smrg for (i = 0; i < n; i++)
38*90a8ff21Smrg t[i] = mpc_imagref (z[i]);
39*90a8ff21Smrg inex_im = mpfr_sum (mpc_imagref (sum), t, n, MPC_RND_IM (rnd));
40*90a8ff21Smrg free (t);
41*90a8ff21Smrg
42*90a8ff21Smrg return MPC_INEX(inex_re, inex_im);
43*90a8ff21Smrg }
44