使用BIGNUM GMP的平方根 [英] Square root of bignum using GMP
问题描述
我需要精确地获得一个210位数的平方根,我还以为是GMP对工作的工具,我究竟做错了什么?
的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括gmp.hINT
主(INT ARGC,CHAR *的argv [])
{
mpz_t sq_me,sq_out,测试;
mpz_init(sq_me);
mpz_init(sq_out);
mpz_init(试验);
mpz_set_str(sq_me,argv的[1],10); mpz_sqrt(sq_out,sq_me);
mpz_mul(测试,sq_out,sq_out); gmp_printf(已将%zd \\ n \\ n,sq_out);
gmp_printf(已将%zd \\ n \\ n,测试); 返回0;
}
输入:
24524664490027821197651766357308801846702678767833275974341445171506160083003858
72169522083993320715491036268271916798640797767232430056005920356312465612184658
17904100131859299619933817012149335034875870551067
块引用>输出:
49522383313031109809242226159886283348695660460381271324714928680654813093947239
9634016783775955618921028
24524664490027821197651766357308801846702678767833275974341445171506160083003858
72169522083993320715491034366358025027526868495267716284867043049443779615862887
47102011391915422793532619329760963626718900576784
块引用>解决方案这里的code你需要的浮点平方根,你可以看到,最初的输入和最终输出是相同的。
的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括gmp.hINT主(INT ARGC,CHAR *的argv []){
mpf_t sq_me,sq_out,测试;
mpf_set_default_ preC(10000);
mpf_init(sq_me);
mpf_init(sq_out);
mpf_init(试验);
mpf_set_str(sq_me,argv的[1],10); mpf_sqrt(sq_out,sq_me);
mpf_mul(测试,sq_out,sq_out); gmp_printf(输入:%FF \\ n \\ n,sq_me);
gmp_printf(平方根%.200Ff \\ n \\ n,sq_out);
gmp_printf(再平方:FF%\\ n \\ n,测试); 返回0;
}下面与您的参数输出:
输入:2452466449002782119765176635730880184670267876783327597434144
51715061600830038587216952208399332071549103626827191679864079776723243005
60059203563124656121846581790410013185929961993381701214933503487587055106
7.000000平方根:4952238331303110980924222615988628334869566046038127132471492
86806548130939472399634016783775955618921028.19202568258368255653837168412
92356432661548614332014106174638951390596672950394981098992388116308833260
04535647648563996144250924277757344248059826024201642748515325655438898558
17807282091590722890002重新平方:2452466449002782119765176635730880184670267876783327597434144
51715061600830038587216952208399332071549103626827191679864079776723243005
60059203563124656121846581790410013185929961993381701214933503487587055106
7.000000I need to get the square root of a 210 digit number accurately, I thought GMP was the right tool for the job, what am I doing wrong?
#include <stdlib.h> #include <stdio.h> #include "gmp.h" int main (int argc, char *argv[]) { mpz_t sq_me, sq_out, test; mpz_init(sq_me); mpz_init(sq_out); mpz_init(test); mpz_set_str (sq_me, argv[1], 10); mpz_sqrt(sq_out, sq_me); mpz_mul(test,sq_out,sq_out); gmp_printf ("%Zd\n\n", sq_out); gmp_printf ("%Zd\n\n", test); return 0; }
Input:
24524664490027821197651766357308801846702678767833275974341445171506160083003858 72169522083993320715491036268271916798640797767232430056005920356312465612184658 17904100131859299619933817012149335034875870551067
Output:
49522383313031109809242226159886283348695660460381271324714928680654813093947239 9634016783775955618921028
24524664490027821197651766357308801846702678767833275974341445171506160083003858 72169522083993320715491034366358025027526868495267716284867043049443779615862887 47102011391915422793532619329760963626718900576784
解决方案Here's the code you need for floating point square root, you can see that the initial input and final output are identical.
#include <stdlib.h> #include <stdio.h> #include "gmp.h" int main (int argc, char *argv[]) { mpf_t sq_me, sq_out, test; mpf_set_default_prec (10000); mpf_init(sq_me); mpf_init(sq_out); mpf_init(test); mpf_set_str (sq_me, argv[1], 10); mpf_sqrt(sq_out, sq_me); mpf_mul(test,sq_out,sq_out); gmp_printf ("Input: %Ff\n\n", sq_me); gmp_printf ("Square root: %.200Ff\n\n", sq_out); gmp_printf ("Re-squared: %Ff\n\n", test); return 0; }
Here's the output with your parameter:
Input: 2452466449002782119765176635730880184670267876783327597434144 51715061600830038587216952208399332071549103626827191679864079776723243005 60059203563124656121846581790410013185929961993381701214933503487587055106 7.000000 Square root: 4952238331303110980924222615988628334869566046038127132471492 86806548130939472399634016783775955618921028.19202568258368255653837168412 92356432661548614332014106174638951390596672950394981098992388116308833260 04535647648563996144250924277757344248059826024201642748515325655438898558 17807282091590722890002 Re-squared: 2452466449002782119765176635730880184670267876783327597434144 51715061600830038587216952208399332071549103626827191679864079776723243005 60059203563124656121846581790410013185929961993381701214933503487587055106 7.000000
这篇关于使用BIGNUM GMP的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!