char * mpc_get_str (int base, size_t n, mpc_srcptr op, mpc_rnd_t rnd) { size_t needed_size; char *real_str; char *imag_str; char *complex_str = NULL; if (base < 2 || base > 36) return NULL; real_str = get_pretty_str (base, n, MPC_RE (op), MPC_RND_RE (rnd)); imag_str = get_pretty_str (base, n, MPC_IM (op), MPC_RND_IM (rnd)); needed_size = strlen (real_str) + strlen (imag_str) + 4; complex_str = mpc_alloc_str (needed_size); MPC_ASSERT (complex_str != NULL); strcpy (complex_str, "("); strcat (complex_str, real_str); strcat (complex_str, " "); strcat (complex_str, imag_str); strcat (complex_str, ")"); mpc_free_str (real_str); mpc_free_str (imag_str); return complex_str; }
string mpcomplex::to_string(int precision) { int base = 10; char * stringRepresentation = mpc_get_str( base , precision , mpc_val , mpc_rnd ); string returnValue = stringRepresentation; mpc_free_str( stringRepresentation ); return returnValue; }
/* Extract from the stream the longest string of characters which are neither whitespace nor brackets (except for an optional bracketed n-char_sequence directly following nan or @nan@ independently of case). The user must free the returned string. */ static char * extract_string (FILE *stream) { int c; size_t nread = 0; size_t strsize = 100; char *str = mpc_alloc_str (strsize); size_t lenstr; c = getc (stream); while (c != EOF && c != '\n' && !isspace ((unsigned char) c) && c != '(' && c != ')') { str [nread] = (char) c; nread++; if (nread == strsize) { str = mpc_realloc_str (str, strsize, 2 * strsize); strsize *= 2; } c = getc (stream); } str = mpc_realloc_str (str, strsize, nread + 1); strsize = nread + 1; str [nread] = '\0'; if (nread == 0) return str; lenstr = nread; if (c == '(') { size_t n; char *suffix; int ret; /* (n-char-sequence) only after a NaN */ if ((nread != 3 || tolower ((unsigned char) (str[0])) != 'n' || tolower ((unsigned char) (str[1])) != 'a' || tolower ((unsigned char) (str[2])) != 'n') && (nread != 5 || str[0] != '@' || tolower ((unsigned char) (str[1])) != 'n' || tolower ((unsigned char) (str[2])) != 'a' || tolower ((unsigned char) (str[3])) != 'n' || str[4] != '@')) { ungetc (c, stream); return str; } suffix = extract_suffix (stream); nread += strlen (suffix) + 1; if (nread >= strsize) { str = mpc_realloc_str (str, strsize, nread + 1); strsize = nread + 1; } /* Warning: the sprintf does not allow overlap between arguments. */ ret = sprintf (str + lenstr, "(%s", suffix); MPC_ASSERT (ret >= 0); n = lenstr + (size_t) ret; MPC_ASSERT (n == nread); c = getc (stream); if (c == ')') { str = mpc_realloc_str (str, strsize, nread + 2); strsize = nread + 2; str [nread] = (char) c; str [nread+1] = '\0'; nread++; } else if (c != EOF) ungetc (c, stream); mpc_free_str (suffix); } else if (c != EOF) ungetc (c, stream); return str; }
int mpc_inp_str (mpc_ptr rop, FILE *stream, size_t *read, int base, mpc_rnd_t rnd_mode) { size_t white, nread = 0; int inex = -1; int c; char *str; if (stream == NULL) stream = stdin; white = skip_whitespace (stream); c = getc (stream); if (c != EOF) { if (c == '(') { char *real_str; char *imag_str; size_t n; int ret; nread++; /* the opening parenthesis */ white = skip_whitespace (stream); real_str = extract_string (stream); nread += strlen(real_str); c = getc (stream); if (!isspace ((unsigned int) c)) { if (c != EOF) ungetc (c, stream); mpc_free_str (real_str); goto error; } else ungetc (c, stream); white += skip_whitespace (stream); imag_str = extract_string (stream); nread += strlen (imag_str); str = mpc_alloc_str (nread + 2); ret = sprintf (str, "(%s %s", real_str, imag_str); MPC_ASSERT (ret >= 0); n = (size_t) ret; MPC_ASSERT (n == nread + 1); mpc_free_str (real_str); mpc_free_str (imag_str); white += skip_whitespace (stream); c = getc (stream); if (c == ')') { str = mpc_realloc_str (str, nread +2, nread + 3); str [nread+1] = (char) c; str [nread+2] = '\0'; nread++; } else if (c != EOF) ungetc (c, stream); } else { if (c != EOF) ungetc (c, stream); str = extract_string (stream); nread += strlen (str); } inex = mpc_set_str (rop, str, base, rnd_mode); mpc_free_str (str); } error: if (inex == -1) { mpfr_set_nan (MPC_RE(rop)); mpfr_set_nan (MPC_IM(rop)); } if (read != NULL) *read = white + nread; return inex; }
static void check_set_str (mpfr_exp_t exp_max) { mpc_t expected; mpc_t got; char *str; mpfr_prec_t prec; mpfr_exp_t exp_min; int base; mpc_init2 (expected, 1024); mpc_init2 (got, 1024); exp_min = mpfr_get_emin (); if (exp_max <= 0) exp_max = mpfr_get_emax (); else if (exp_max > mpfr_get_emax ()) exp_max = mpfr_get_emax(); if (-exp_max > exp_min) exp_min = - exp_max; for (prec = 2; prec < 1024; prec += 7) { mpc_set_prec (got, prec); mpc_set_prec (expected, prec); base = 2 + (int) gmp_urandomm_ui (rands, 35); /* uses external variable rands from random.c */ mpfr_set_nan (MPC_RE (expected)); mpfr_set_inf (MPC_IM (expected), prec % 2 - 1); str = mpc_get_str (base, 0, expected, MPC_RNDNN); if (mpfr_nan_p (MPC_RE (got)) == 0 || mpfr_cmp (MPC_IM (got), MPC_IM (expected)) != 0) { printf ("Error: mpc_set_str o mpc_get_str != Id\n" "in base %u with str=\"%s\"\n", base, str); MPC_OUT (expected); printf (" "); MPC_OUT (got); exit (1); } mpc_free_str (str); test_default_random (expected, exp_min, exp_max, 128, 25); str = mpc_get_str (base, 0, expected, MPC_RNDNN); if (mpc_set_str (got, str, base, MPC_RNDNN) == -1 || mpc_cmp (got, expected) != 0) { printf ("Error: mpc_set_str o mpc_get_str != Id\n" "in base %u with str=\"%s\"\n", base, str); MPC_OUT (expected); printf (" "); MPC_OUT (got); exit (1); } mpc_free_str (str); } #ifdef HAVE_SETLOCALE { /* Check with ',' as a decimal point */ char *old_locale; old_locale = setlocale (LC_ALL, "de_DE"); if (old_locale != NULL) { str = mpc_get_str (10, 0, expected, MPC_RNDNN); if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1 || mpc_cmp (got, expected) != 0) { printf ("Error: mpc_set_str o mpc_get_str != Id\n" "with str=\"%s\"\n", str); MPC_OUT (expected); printf (" "); MPC_OUT (got); exit (1); } mpc_free_str (str); setlocale (LC_ALL, old_locale); } } #endif /* HAVE_SETLOCALE */ /* the real part has a zero exponent in base ten (fixed in r439) */ mpc_set_prec (expected, 37); mpc_set_prec (got, 37); mpc_set_str (expected, "921FC04EDp-35 ", 16, GMP_RNDN); str = mpc_get_str (10, 0, expected, MPC_RNDNN); if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1 || mpc_cmp (got, expected) != 0) { printf ("Error: mpc_set_str o mpc_get_str != Id\n" "with str=\"%s\"\n", str); MPC_OUT (expected); printf (" "); MPC_OUT (got); exit (1); } mpc_free_str (str); str = mpc_get_str (1, 0, expected, MPC_RNDNN); if (str != NULL) { printf ("Error: mpc_get_str with base==1 should fail\n"); exit (1); } mpc_clear (expected); mpc_clear (got); }