GMP无法应对领先的“”?
考虑以下代码
// 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 );
}
在第一个实例中,在 mpf_get_str 调用之后, buff 包含“123”。 在第二个中,buff包含“-123”。 但在第三个中, buff 包含一个空字符串(“”)。
这是使用 GMP 4.2.4。 也许我需要再次查看手册,但我认为前导“+”会像前导“-”一样容易处理。
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 "-".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,GMP 手册中没有提及您的问题。 不过,您可以检查 mpf_set_str可以看到它不处理'+'。
我不知道你在什么情况下需要这个,但如果你确实需要一个字符来指示正/负,你也许可以利用这些函数忽略前导空格的事实,从而使用
" 123"
。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"
.