1 /* mpc_get_dc, mpc_get_ldc -- Transform mpc number into C complex number
2 mpc_get_str -- Convert a complex number into a string.
4 Copyright (C) 2009, 2010, 2011 INRIA
6 This file is part of GNU MPC.
8 GNU MPC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
18 You should have received a copy of the GNU Lesser General Public License
19 along with this program. If not, see http://www.gnu.org/licenses/ .
32 #include <stdio.h> /* for sprintf, fprintf */
39 mpc_get_dc (mpc_srcptr op, mpc_rnd_t rnd) {
40 return I * mpfr_get_d (mpc_imagref (op), MPC_RND_IM (rnd))
41 + mpfr_get_d (mpc_realref (op), MPC_RND_RE (rnd));
45 mpc_get_ldc (mpc_srcptr op, mpc_rnd_t rnd) {
46 return I * mpfr_get_ld (mpc_imagref (op), MPC_RND_IM (rnd))
47 + mpfr_get_ld (mpc_realref (op), MPC_RND_RE (rnd));
52 /* Code for mpc_get_str. The output format is "(real imag)", the decimal point
53 of the locale is used. */
55 /* mpfr_prec_t can be either int or long int */
56 #if (__GMP_MP_SIZE_T_INT == 1)
57 #define MPC_EXP_FORMAT_SPEC "i"
58 #elif (__GMP_MP_SIZE_T_INT == 0)
59 #define MPC_EXP_FORMAT_SPEC "li"
61 #error "mpfr_exp_t size not supported"
65 pretty_zero (mpfr_srcptr zero)
69 pretty = mpc_alloc_str (3);
71 pretty[0] = mpfr_signbit (zero) ? '-' : '+';
79 prettify (const char *str, const mp_exp_t expo, int base, int special)
88 sz = strlen (str) + 1; /* + terminal '\0' */
92 /* special number: nan or inf */
93 pretty = mpc_alloc_str (sz);
101 sign = (str[0] == '-' || str[0] == '+');
103 x = expo - 1; /* expo is the exponent value with decimal point BEFORE
104 the first digit, we wants decimal point AFTER the first
107 x <<= 2; /* the output exponent is a binary exponent */
109 ++sz; /* + decimal point */
113 /* augment sz with the size needed for an exponent written in base
117 sz += 3; /* + exponent char + sign + 1 digit */
121 /* avoid overflow when changing sign (assuming that, for the
122 mp_exp_t type, (max value) is greater than (- min value / 10)) */
134 /* compute sz += floor(log(expo)/log(10)) without using libm
143 pretty = mpc_alloc_str (sz);
146 /* 1. optional sign plus first digit */
152 /* 2. decimal point */
153 #ifdef HAVE_LOCALECONV
154 *p++ = *localeconv ()->decimal_point;
160 /* 3. other significant digits */
163 /* 4. exponent (in base ten) */
167 p = pretty + strlen (str) + 1;
184 sprintf (p, "%+"MPC_EXP_FORMAT_SPEC, x);
190 get_pretty_str (const int base, const size_t n, mpfr_srcptr x, mpfr_rnd_t rnd)
197 return pretty_zero (x);
199 ugly = mpfr_get_str (NULL, &expo, base, n, x, rnd);
200 MPC_ASSERT (ugly != NULL);
201 pretty = prettify (ugly, expo, base, !mpfr_number_p (x));
202 mpfr_free_str (ugly);
208 mpc_get_str (int base, size_t n, mpc_srcptr op, mpc_rnd_t rnd)
213 char *complex_str = NULL;
215 if (base < 2 || base > 36)
218 real_str = get_pretty_str (base, n, mpc_realref (op), MPC_RND_RE (rnd));
219 imag_str = get_pretty_str (base, n, mpc_imagref (op), MPC_RND_IM (rnd));
221 needed_size = strlen (real_str) + strlen (imag_str) + 4;
223 complex_str = mpc_alloc_str (needed_size);
224 MPC_ASSERT (complex_str != NULL);
226 strcpy (complex_str, "(");
227 strcat (complex_str, real_str);
228 strcat (complex_str, " ");
229 strcat (complex_str, imag_str);
230 strcat (complex_str, ")");
232 mpc_free_str (real_str);
233 mpc_free_str (imag_str);