diff options
-rw-r--r-- | src/inp_str.c | 18 | ||||
-rw-r--r-- | src/out_str.c | 13 | ||||
-rw-r--r-- | tests/test.c | 14 |
3 files changed, 37 insertions, 8 deletions
diff --git a/src/inp_str.c b/src/inp_str.c index eaade41..c53e92d 100644 --- a/src/inp_str.c +++ b/src/inp_str.c @@ -30,6 +30,7 @@ mpc_inp_str (mpc_ptr rop, FILE *stream, int base, mpc_rnd_t rnd_mode) { size_t size, size_im; int c; + int sign; if (stream == NULL) stream = stdin; @@ -47,8 +48,18 @@ mpc_inp_str (mpc_ptr rop, FILE *stream, int base, mpc_rnd_t rnd_mode) } while (isspace (c)); - if (c != '+') - return 0; /* error */ + switch (c) + { + case '+': + sign = 0; + break; + case '-': + sign = 1; + break; + default: + /* error */ + return 0; + } c = getc (stream); size ++; @@ -63,6 +74,9 @@ mpc_inp_str (mpc_ptr rop, FILE *stream, int base, mpc_rnd_t rnd_mode) size_im = mpfr_inp_str (MPC_IM(rop), stream, base, MPC_RND_IM(rnd_mode)); if (size_im == 0) /* error while reading the imaginary part */ return 0; + + if (sign) + mpfr_setsign (MPC_IM (rop), MPC_IM (rop), sign, GMP_RNDN); return size + size_im; } diff --git a/src/out_str.c b/src/out_str.c index ab56b94..582630b 100644 --- a/src/out_str.c +++ b/src/out_str.c @@ -28,14 +28,23 @@ MA 02111-1307, USA. */ size_t mpc_out_str (FILE *stream, int base, size_t n, mpc_srcptr op, mpc_rnd_t rnd) { + mpfr_t positive_imag; size_t size; if (stream == NULL) stream = stdout; /* fprintf does not allow NULL as first argument */ size = mpfr_out_str (stream, base, n, MPC_RE(op), MPC_RND_RE(rnd)); - size += fprintf (stream, "+I*"); - size += mpfr_out_str (stream, base, n, MPC_IM(op), MPC_RND_IM(rnd)); + + positive_imag[0] = MPC_IM (op)[0]; + if (mpfr_signbit (MPC_IM (op))) + { + size += fprintf (stream, " -I*"); + mpfr_setsign (positive_imag, positive_imag, 0, GMP_RNDN); + } + else + size += fprintf (stream, " +I*"); + size += mpfr_out_str (stream, base, n, positive_imag, MPC_RND_IM(rnd)); return size; } diff --git a/tests/test.c b/tests/test.c index 35ce1e8..bfa5da8 100644 --- a/tests/test.c +++ b/tests/test.c @@ -38,7 +38,6 @@ main() mp_prec_t prec, pr, pi; mpfr_t f, g; FILE *file; - int nread; const char *filename = "mpc_test"; test_start (); @@ -144,17 +143,24 @@ main() fprintf (stderr, "Could not open file %s\n", filename); exit (1); }; - fprintf (file, "1+I*1\n"); + fprintf (file, "1 +I*1\n"); fclose (file); if (!(file = fopen (filename, "r"))) { fprintf (stderr, "Could not open file %s\n", filename); exit (1); }; - nread = mpc_inp_str (z, file, 10, MPC_RNDUZ); + if (mpc_inp_str (z, file, 10, MPC_RNDUZ) == 0) + { + fprintf (stderr, "mpc_inp_str cannot correctly re-read number " + "in file %s\n", filename); + exit (1); + } fclose (file); mpc_set_si_si (x, 1, 1, MPC_RNDNN); - if (nread && mpc_cmp (z, x)) + mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is + a NaN */ + if (mpc_cmp (z, x) != 0 || mpfr_erangeflag_p()) { fprintf (stderr, "inp_str o out_str <> Id\n"); mpc_out_str (stderr, 10, 0, z, MPC_RNDNN); |