summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorenge <enge@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2010-08-31 18:11:38 +0000
committerenge <enge@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2010-08-31 18:11:38 +0000
commit3e9daad0e04c9504b6372f3ad702f9f292f3a442 (patch)
tree9dd3a059430e9c307f564e31415566befff0c995
parent8127c551c5936221227fdf7292cb2f1bc511580f (diff)
downloadmpc-3e9daad0e04c9504b6372f3ad702f9f292f3a442.tar.gz
new functions mpc_set_c, mpc_set_lc, mpc_get_c, mpc_get_lc
git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@820 211d60ee-9f03-0410-a15a-8952a2c7a4e4
-rw-r--r--NEWS4
-rw-r--r--configure.ac2
-rw-r--r--doc/mpc.texi35
-rw-r--r--src/Makefile.am6
-rw-r--r--src/mpc.h20
-rw-r--r--src/set_x.c20
-rw-r--r--tests/tset.c33
7 files changed, 103 insertions, 17 deletions
diff --git a/NEWS b/NEWS
index afaa3f5..2dd604c 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,8 @@
Changes in version 0.8.3:
+ - New functions:
+ - mpc_set_c, mpc_set_lc, mpc_get_c, mpc_get_lc for converting between
+ mpc type variables and C variables of type double _Complex or
+ long double _Complex
- Speed-up of mpc_pow_si through binary exponentiation, and of
mpc_pow_z when the exponent fits in a long
- Bug fixes:
diff --git a/configure.ac b/configure.ac
index ac35927..143c49a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -167,7 +167,7 @@ error
# Checks for header files.
AC_HEADER_STDC
-AC_CHECK_HEADERS([locale.h inttypes.h limits.h unistd.h sys/time.h])
+AC_CHECK_HEADERS([complex.h locale.h inttypes.h limits.h unistd.h sys/time.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
diff --git a/doc/mpc.texi b/doc/mpc.texi
index 147093d..62f0e0b 100644
--- a/doc/mpc.texi
+++ b/doc/mpc.texi
@@ -35,7 +35,7 @@ was introduced.
@include version.texi
@set AUTHORS Andreas Enge, Philippe Th@'eveny, Paul Zimmermann
-@set COPYRIGHTDATES 2002, 2003, 2004, 2005, 2007, 2008, 2009
+@set COPYRIGHTDATES 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
@ifinfo
@format
START-INFO-DIR-ENTRY
@@ -489,6 +489,7 @@ different word size.
@menu
* Initializing Complex Numbers::
* Assigning Complex Numbers::
+* Converting Complex Numbers::
* String and Stream Input and Output::
* Complex Comparison::
* Projection & Decomposing::
@@ -564,16 +565,20 @@ via @var{pi}.
@end deftypefun
-@node Assigning Complex Numbers, String and Stream Input and Output, Initializing Complex Numbers, Complex Functions
+@node Assigning Complex Numbers, Converting Complex Numbers, Initializing Complex Numbers, Complex Functions
@comment node-name, next, previous, up
@section Assignment Functions
@cindex Complex assignment functions
These functions assign new values to already initialized complex numbers
(@pxref{Initializing Complex Numbers}).
-When using any functions with @code{intmax_t} parameter, you must include
-@code{<stdint.h>} or @code{<inttypes.h>} before @file{mpc.h}, to allow
+When using any functions with @code{intmax_t} or @code{uintmax_t}
+parameters, you must include
+@code{<stdint.h>} or @code{<inttypes.h>} @emph{before} @file{mpc.h}, to allow
@file{mpc.h} to define prototypes for these functions.
+Similarly, functions with parameters of type @code{complex} or
+@code{long complex} are defined only if @code{<complex.h>} is included
+@emph{before} @file{mpc.h}.
If you need assignment functions that are not in the current API, you can
define them using the @code{MPC_SET_X_Y} macro (@pxref{Advanced Functions}).
@@ -588,6 +593,8 @@ with the given rounding mode @var{rnd}.
@deftypefunx int mpc_set_sj (mpc_t @var{rop}, intmax_t @var{op}, mpc_rnd_t @var{rnd})
@deftypefunx int mpc_set_d (mpc_t @var{rop}, double @var{op}, mpc_rnd_t @var{rnd})
@deftypefunx int mpc_set_ld (mpc_t @var{rop}, long double @var{op}, mpc_rnd_t @var{rnd})
+@deftypefunx int mpc_set_c (mpc_t @var{rop}, double _Complex @var{op}, mpc_rnd_t @var{rnd})
+@deftypefunx int mpc_set_lc (mpc_t @var{rop}, long double _Complex @var{op}, mpc_rnd_t @var{rnd})
@deftypefunx int mpc_set_z (mpc_t @var{rop}, mpz_t @var{op} mpc_rnd_t @var{rnd})
@deftypefunx int mpc_set_q (mpc_t @var{rop}, mpq_t @var{op} mpc_rnd_t @var{rnd})
@deftypefunx int mpc_set_f (mpc_t @var{rop}, mpf_t @var{op} mpc_rnd_t @var{rnd})
@@ -639,7 +646,25 @@ third auxiliary variable.
@end deftypefun
-@node String and Stream Input and Output, Complex Comparison, Assigning Complex Numbers, Complex Functions
+@node Converting Complex Numbers, String and Stream Input and Output, Assigning Complex Numbers, Complex Functions
+@comment node-name, next, previous, up
+@section Conversion Functions
+@cindex Conversion functions
+
+The following functions are available only if @code{<complex.h>}
+is included @emph{before} @file{mpc.h}.
+
+@deftypefun double _Complex mpc_get_c (mpc_t @var{op}, mpc_rnd_t @var{rnd})
+@deftypefunx {long double _Complex} mpc_get_lc (mpc_t @var{op}, mpc_rnd_t @var{rnd})
+Convert @var{op} to a C complex number, using the rounding mode @var{rnd}.
+@end deftypefun
+
+
+For functions converting complex variables to strings or stream output,
+@pxref{string-io,,String and Stream Input and Output}.
+
+
+@node String and Stream Input and Output, Complex Comparison, Converting Complex Numbers, Complex Functions
@comment node-name, next, previous, up
@cindex String and stream input and output
@anchor{string-io}
diff --git a/src/Makefile.am b/src/Makefile.am
index d4bc8b2..06555ce 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -6,10 +6,10 @@ 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 \
conj.c cos.c cosh.c div_2exp.c div.c div_fr.c div_ui.c exp.c fr_div.c \
- fr_sub.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 \
+ 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 \
mul_si.c mul_ui.c neg.c norm.c out_str.c pow.c pow_fr.c pow_ld.c pow_d.c \
pow_si.c pow_ui.c pow_z.c proj.c real.c urandom.c set.c set_prec.c \
set_str.c set_x.c set_x_x.c sin.c sinh.c sqr.c sqrt.c strtoc.c sub.c \
sub_fr.c sub_ui.c swap.c tan.c tanh.c uceil_log2.c ui_div.c ui_ui_sub.c
-
+
diff --git a/src/mpc.h b/src/mpc.h
index fe2b6df..c6f71b5 100644
--- a/src/mpc.h
+++ b/src/mpc.h
@@ -51,6 +51,11 @@ MA 02111-1307, USA. */
# define _MPC_H_HAVE_INTMAX_T 1
#endif
+/* Check if complex.h is included */
+#if defined (_COMPLEX_H)
+# define _MPC_H_HAVE_COMPLEX 1
+#endif
+
/* Return values */
/* Transform negative to 2, positive to 1, leave 0 unchanged */
@@ -75,8 +80,8 @@ MA 02111-1307, USA. */
typedef int mpc_rnd_t;
#define RNDC(r1,r2) (((int)(r1)) + ((int)(r2) << 4))
-#define MPC_RND_RE(x) ((mp_rnd_t)((x) & 0x0F))
-#define MPC_RND_IM(x) ((mp_rnd_t)((x) >> 4))
+#define MPC_RND_RE(x) ((mpfr_rnd_t)((x) & 0x0F))
+#define MPC_RND_IM(x) ((mpfr_rnd_t)((x) >> 4))
#define MPC_RNDNN RNDC(GMP_RNDN,GMP_RNDN)
#define MPC_RNDNZ RNDC(GMP_RNDN,GMP_RNDZ)
@@ -163,8 +168,8 @@ __MPC_DECLSPEC int mpc_div_2exp __MPC_PROTO ((mpc_ptr, mpc_srcptr, unsigned lon
__MPC_DECLSPEC int mpc_mul_2exp __MPC_PROTO ((mpc_ptr, mpc_srcptr, unsigned long int, mpc_rnd_t));
__MPC_DECLSPEC int mpc_conj __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_rnd_t));
__MPC_DECLSPEC int mpc_neg __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_rnd_t));
-__MPC_DECLSPEC int mpc_norm __MPC_PROTO ((mpfr_ptr, mpc_srcptr, mp_rnd_t));
-__MPC_DECLSPEC int mpc_abs __MPC_PROTO ((mpfr_ptr, mpc_srcptr, mp_rnd_t));
+__MPC_DECLSPEC int mpc_norm __MPC_PROTO ((mpfr_ptr, mpc_srcptr, mpfr_rnd_t));
+__MPC_DECLSPEC int mpc_abs __MPC_PROTO ((mpfr_ptr, mpc_srcptr, mpfr_rnd_t));
__MPC_DECLSPEC int mpc_sqrt __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_rnd_t));
__MPC_DECLSPEC int mpc_set __MPC_PROTO ((mpc_ptr, mpc_srcptr, mpc_rnd_t));
__MPC_DECLSPEC int mpc_set_d __MPC_PROTO ((mpc_ptr, double, mpc_rnd_t));
@@ -192,6 +197,13 @@ __MPC_DECLSPEC int mpc_set_sj_sj __MPC_PROTO ((mpc_ptr, intmax_t, intmax_t, mpc
__MPC_DECLSPEC int mpc_set_uj_uj __MPC_PROTO ((mpc_ptr, uintmax_t, uintmax_t, mpc_rnd_t));
#endif
+#ifdef _MPC_H_HAVE_COMPLEX
+__MPC_DECLSPEC int mpc_set_c __MPC_PROTO ((mpc_ptr, double _Complex, mpc_rnd_t));
+__MPC_DECLSPEC int mpc_set_lc __MPC_PROTO ((mpc_ptr, long double _Complex, mpc_rnd_t));
+__MPC_DECLSPEC double _Complex mpc_get_c __MPC_PROTO ((mpc_srcptr, mpc_rnd_t));
+__MPC_DECLSPEC long double _Complex mpc_get_lc __MPC_PROTO ((mpc_srcptr, mpc_rnd_t));
+#endif
+
__MPC_DECLSPEC void mpc_set_nan __MPC_PROTO ((mpc_ptr));
__MPC_DECLSPEC int mpc_real __MPC_PROTO ((mpfr_ptr, mpc_srcptr, mpfr_rnd_t));
diff --git a/src/set_x.c b/src/set_x.c
index 67db08c..6b9a228 100644
--- a/src/set_x.c
+++ b/src/set_x.c
@@ -1,7 +1,7 @@
-/* mpc_set_fr -- Set the real part of a complex number from a real number
+/* mpc_set_x -- Set the real part of a complex number
(imaginary part equals +0 regardless of rounding mode).
-Copyright (C) 2008, 2009 Philippe Th\'eveny, Andreas Enge
+Copyright (C) 2008, 2009, 2010 Philippe Th\'eveny, Andreas Enge
This file is part of the MPC Library.
@@ -30,6 +30,10 @@ MA 02111-1307, USA. */
# endif
#endif
+#if HAVE_COMPLEX_H
+# include <complex.h>
+#endif
+
#include "mpc-impl.h"
#define MPC_SET_X(real_t, z, real_value, rnd) \
@@ -82,6 +86,18 @@ mpc_set_sj (mpc_ptr a, intmax_t b, mpc_rnd_t rnd)
MPC_SET_X (sj, a, b, rnd)
#endif
+#ifdef _MPC_H_HAVE_COMPLEX
+int
+mpc_set_c (mpc_ptr a, double _Complex b, mpc_rnd_t rnd) {
+ return mpc_set_d_d (a, creal (b), cimag (b), rnd);
+}
+
+int
+mpc_set_lc (mpc_ptr a, long double _Complex b, mpc_rnd_t rnd) {
+ return mpc_set_ld_ld (a, creall (b), cimagl (b), rnd);
+}
+#endif
+
void
mpc_set_nan (mpc_ptr a) {
mpfr_set_nan (mpc_realref (a));
diff --git a/tests/tset.c b/tests/tset.c
index fd38ee4..729cc68 100644
--- a/tests/tset.c
+++ b/tests/tset.c
@@ -1,6 +1,6 @@
/* tset.c -- Test file for mpc_set_x and mpc_set_x_x functions.
-Copyright (C) 2009 Philippe Th\'eveny, Paul Zimmermann, Andreas Enge
+Copyright (C) 2009, 2010 Philippe Th\'eveny, Paul Zimmermann, Andreas Enge
This file is part of the MPC Library.
@@ -30,6 +30,10 @@ MA 02111-1307, USA. */
# endif
#endif
+#if HAVE_COMPLEX_H
+# include <complex.h>
+#endif
+
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
@@ -91,7 +95,13 @@ check_set (void)
mpc_set_d (z, 1.23456789, MPC_RNDNN);
if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_si (MPC_IM(z), 0) != 0)
- PRINT_ERROR ("mpc_set", prec, z);
+ PRINT_ERROR ("mpc_set_d", prec, z);
+
+#if defined _MPC_H_HAVE_COMPLEX
+ mpc_set_c (z, I*1.23456789+1.23456789, MPC_RNDNN);
+ if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0)
+ PRINT_ERROR ("mpc_set_c", prec, z);
+#endif
mpc_set_ui (z, prec, MPC_RNDNN);
if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
@@ -112,6 +122,11 @@ check_set (void)
if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0)
PRINT_ERROR ("mpc_set_ld_ld", prec, z);
+#if defined _MPC_H_HAVE_COMPLEX
+ mpc_set_lc (z, I*1.23456789L+1.23456789L, MPC_RNDNN);
+ if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0)
+ PRINT_ERROR ("mpc_set_lc", prec, z);
+#endif
mpc_set_ui_ui (z, prec, prec, MPC_RNDNN);
if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
|| mpfr_cmp_ui (MPC_IM(z), prec) != 0)
@@ -271,6 +286,20 @@ check_set (void)
PRINT_ERROR ("mpc_set_sj_sj (2)", im, z);
}
#endif /* _MPC_H_HAVE_INTMAX_T */
+
+#if defined _MPC_H_HAVE_COMPLEX
+ {
+ double _Complex c = 1.0 - 2.0*I;
+ long double _Complex lc = c;
+
+ mpc_set_c (z, c, MPC_RNDNN);
+ if (mpc_get_c (z, MPC_RNDNN) != c)
+ PRINT_ERROR ("mpc_get_c", prec, z);
+ mpc_set_lc (z, lc, MPC_RNDNN);
+ if (mpc_get_lc (z, MPC_RNDNN) != lc)
+ PRINT_ERROR ("mpc_get_lc", prec, z);
+ }
+#endif
}
mpz_clear (mpz);