summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorenge <enge@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2011-01-18 12:40:20 +0000
committerenge <enge@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2011-01-18 12:40:20 +0000
commit3237c68dfc83a83a1271755ed0cda374aebe0069 (patch)
treee785ad170d0e061c2a5017adfcc214e8f4ae718d
parent1e2626822cbd2464f318a5f65df8e8a6469e7b14 (diff)
downloadmpc-3237c68dfc83a83a1271755ed0cda374aebe0069.tar.gz
add_si: replaced macro by function, since the macro evaluated its argument
twice git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@881 211d60ee-9f03-0410-a15a-8952a2c7a4e4
-rw-r--r--NEWS2
-rw-r--r--src/Makefile.am2
-rw-r--r--src/add_si.c33
-rw-r--r--src/mpc.h3
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/tadd_si.c69
6 files changed, 107 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index cefa690..1337af3 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,8 @@ Changes in version 0.9:
- trigonometric functions: infinite loop due to overflow for large arguments
- exp: close to infinite loop for argument close to 0
- sqrt: close to infinite loop for argument close to 1
+ - add_si: replaced macro by function, since the macro evaluated the same
+ expression twice
- Logging feature for debugging:
./configure --enable-logging
#include "mpc-log.h" instead of #include "mpc.h"
diff --git a/src/Makefile.am b/src/Makefile.am
index b9cb03b..7c2c960 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,7 +4,7 @@ AM_CFLAGS=@WARNINGCFLAGS@
lib_LTLIBRARIES = libmpc.la
libmpc_la_LDFLAGS = -version-info 2:0:0
libmpc_la_SOURCES = mpc-impl.h abs.c acos.c acosh.c add.c add_fr.c \
- add_ui.c arg.c asin.c asinh.c atan.c atanh.c clear.c cmp.c cmp_si_si.c \
+ add_si.c add_ui.c arg.c asin.c asinh.c atan.c atanh.c clear.c cmp.c cmp_si_si.c \
conj.c cosh.c div_2exp.c div.c div_fr.c div_ui.c exp.c fma.c fr_div.c \
fr_sub.c get.c get_prec2.c get_prec.c get_str.c get_version.c imag.c \
init2.c init3.c inp_str.c log.c mem.c mul_2exp.c mul.c mul_fr.c mul_i.c \
diff --git a/src/add_si.c b/src/add_si.c
new file mode 100644
index 0000000..663e036
--- /dev/null
+++ b/src/add_si.c
@@ -0,0 +1,33 @@
+/* mpc_add_si -- Add a complex number and a signed long int.
+
+Copyright (C) 2011 Andreas Enge
+
+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 "mpc-impl.h"
+
+int
+mpc_add_si (mpc_ptr rop, mpc_srcptr op1, long int op2, mpc_rnd_t rnd)
+{
+ int inex_re, inex_im;
+
+ inex_re = mpfr_add_si (MPC_RE (rop), MPC_RE (op1), op2, MPC_RND_RE (rnd));
+ inex_im = mpfr_set (MPC_IM (rop), MPC_IM (op1), MPC_RND_IM (rnd));
+
+ return MPC_INEX (inex_re, inex_im);
+}
diff --git a/src/mpc.h b/src/mpc.h
index a6cae48..d19036d 100644
--- a/src/mpc.h
+++ b/src/mpc.h
@@ -146,6 +146,7 @@ extern "C" {
__MPC_DECLSPEC int mpc_add __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t));
__MPC_DECLSPEC int mpc_add_fr __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpfr_srcptr, mpc_rnd_t));
+__MPC_DECLSPEC int mpc_add_si __MPC_PROTO ((mpc_ptr, mpc_srcptr, long int, mpc_rnd_t));
__MPC_DECLSPEC int mpc_add_ui __MPC_PROTO ((mpc_ptr, mpc_srcptr, unsigned long int, mpc_rnd_t));
__MPC_DECLSPEC int mpc_sub __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t));
__MPC_DECLSPEC int mpc_sub_fr __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpfr_srcptr, mpc_rnd_t));
@@ -259,8 +260,6 @@ __MPC_DECLSPEC size_t mpc_out_str __MPC_PROTO ((FILE *, int, size_t, mpc_srcptr,
#define mpc_realref(x) ((x)->re)
#define mpc_imagref(x) ((x)->im)
-#define mpc_add_si(x, y, z, rnd) \
- ( (z) >= 0 ? mpc_add_ui ((x), (y), (unsigned long int) (z), (rnd)) : mpc_sub_ui ((x), (y), (unsigned long int) (-(z)), (rnd)) )
#define mpc_cmp_si(x, y) \
( mpc_cmp_si_si ((x), (y), 0l) )
#define mpc_ui_sub(x, y, z, r) mpc_ui_ui_sub (x, y, 0ul, z, r)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1415c03..cac6756 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,7 +7,7 @@ LDADD = libmpc-tests.la $(top_builddir)/src/libmpc.la
# "make foo".
LOADLIBES=$(DEFS) -I$(top_srcdir)/src -I$(top_builddir) $(CPPFLAGS) $(CFLAGS) $(top_builddir)/tests/.libs/libmpc-tests.a $(top_builddir)/src/.libs/libmpc.a $(LIBS)
-check_PROGRAMS = tabs tacos tacosh tadd tadd_fr tadd_ui targ tasin tasinh \
+check_PROGRAMS = tabs tacos tacosh tadd tadd_fr tadd_si tadd_ui targ tasin tasinh \
tatan tatanh tconj tcos tcosh tdiv tdiv_2exp tdiv_fr tdiv_ui texp tfma \
tfr_div tfr_sub timag tio_str tlog tmul tmul_2exp tmul_fr tmul_i tmul_si \
tmul_ui tneg tnorm tpow tpow_ld tpow_d tpow_fr tpow_si tpow_ui tpow_z tprec \
diff --git a/tests/tadd_si.c b/tests/tadd_si.c
new file mode 100644
index 0000000..a4b6136
--- /dev/null
+++ b/tests/tadd_si.c
@@ -0,0 +1,69 @@
+/* test file for mpc_add_si.
+
+Copyright (C) 2011 Andreas Enge
+
+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 <stdlib.h>
+#include "mpc-tests.h"
+
+static void
+check_ternary_value (void)
+{
+ mpfr_prec_t prec;
+ mpc_t z;
+ const long int s = -1;
+
+ mpc_init2 (z, 2);
+
+ for (prec=2; prec <= 1024; prec++) {
+ mpc_set_prec (z, prec);
+ mpc_set_ui (z, 3ul, MPC_RNDNN);
+ if (mpc_add_si (z, z, s, MPC_RNDDU)) {
+ printf ("Error in mpc_add_si: 3+(-1) should be exact\n");
+ exit (1);
+ }
+ else if (mpc_cmp_si (z, 2l) != 0) {
+ printf ("Error in mpc_add_si: 3+(-1) should be 2\n");
+ exit (1);
+ }
+
+ mpc_mul_2exp (z, z, (unsigned long int) prec, MPC_RNDNN);
+ if (mpc_add_si (z, z, s, MPC_RNDNN) == 0) {
+ printf ("Error in mpc_add_si: 2^(prec+1)-1 cannot be exact\n");
+ exit (1);
+ }
+ }
+
+ mpc_clear (z);
+}
+
+int
+main (void)
+{
+ DECL_FUNC (CCU, f, mpc_add_ui);
+
+ test_start ();
+
+ check_ternary_value ();
+ tgeneric (f, 2, 1024, 11, -2);
+
+ test_end ();
+
+ return 0;
+}