tags:

views:

92

answers:

1

Consider the following code

// BOGP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "gmp-static\gmp.h"
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>


#define F(x) mpf_t x; mpf_init( x );

int main(int argc, char* argv[])
{
 F(foo);
 char * buff;
 mp_exp_t exp;

 mpf_init_set_str( foo, "123", 10 );
 buff = mpf_get_str(NULL, &exp, 10, 0, foo);
 puts( buff );
 puts("\n");
 free(buff); 

 mpf_init_set_str( foo, "-123", 10 );
 buff = mpf_get_str(NULL, &exp, 10, 0, foo);
 puts( buff );
 puts("\n");
 free(buff); 

 mpf_init_set_str( foo, "+123", 10 );
 buff = mpf_get_str(NULL, &exp, 10, 0, foo);
 puts( buff );
 puts("\n");
 free( buff );

}

In the first instance, after the mpf_get_str call, buff contains "123". In the second, buff contains "-123". But in the third, buff contains an empty string ("").

This is using GMP 4.2.4. Maybe I need to look in the manual again, but I would have thought that a leading "+" would have been handled as readily as a leading "-".

+2  A: 

To the best of my knowledge, your issue is not mentioned anywhere in the GMP manual. You can, however, examine the code for mpf_set_str directly to see that it doesn't handle '+'.

I don't know what situation you need this in, but if you really need a character to indicate positive/negative, you might be able to take advantage of the fact that these functions ignore leading whitespace, thus using " 123".

A. Rex
I looked at the source. It doesn't handle leading plus. (Sigh). Now to find a way of (re)building it on Windows.
boost