I'm trying to write a wrapper for GNU MP for the upcoming version of Python, but I'm stuck at figuring out how to get the component parts of an mpf_t.<br><br>The current code I have works for most things, but values such as
0.01 don't seem to exist at all!<br><br>Small example, which I've been using to try and figure things out:<br><br><blockquote>#include <stdio.h><br>#include <string.h><br>#include <stdlib.h><br>#include <
gmp.h><br><br>#define MPF_DEFAULT_PREC 128<br>#define MPF_DEFAULT_BASE 10<br>#define MPF_DEFAULT_SIGFIGS (size_t) 0<br><br>int main(int argc, char *argv[]) {<br> mpf_t x;<br> size_t sigfigs = MPF_DEFAULT_SIGFIGS;
<br> unsigned long int base = MPF_DEFAULT_BASE;<br> unsigned long int whole = 0;<br> unsigned long int decimal = 0;<br> mp_exp_t exponent;<br> char *value = NULL;<br> char *s_whole = NULL;
<br><br> mpf_set_default_prec(MPF_DEFAULT_PREC);<br> mpf_init(x);<br> mpf_set_d(x, (double) 0.01);<br><br> value = mpf_get_str(NULL, &exponent, base, sigfigs, x);<br><br> if (strncmp(value, (char *) "", 1) > 0) {
<br> if (exponent > 0) {<br> s_whole = malloc(exponent);<br> strncpy(s_whole, value, exponent);<br> printf("%s", s_whole);<br> }<br><br> value += exponent;
<br> if (strlen(value) > 0) printf(".%s\n", value);<br> }<br><br> mpf_clear(x);<br><br> return 0;<br>}<br></blockquote>For the wrapper, I'd ideally like to return the base, the whole component, and the decimal component. mpf_get_str seems to be the only way to do this, but I'm having very little luck. mpf_get_d_2exp doesn't seem like it will help particularly, since one of the reasons for wrapping GNU MP in the first place was to avoid a loss of precision. If I multiplied things out to get the components I want, there would be a loss of precision, as well as speed.
<br><br>Help?<br><br>Thank you for your time in advance.<br><br>Rob<br><br>