找到2的平方根的尽可能多的数字 [英] find as many digits of the square root of 2 as possible

查看:48
本文介绍了找到2的平方根的尽可能多的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
 double a = sqrt(2);
 cout << a << endl;
}

这是找到2的sqrt的程序,它仅在输出中输出1.41421,如何实现该方式,以便它将在小数点后打印200000位

hi this is the program to find sqrt of 2 it prints just 1.41421 in the output how to implement it in way such that it will print 200000 digits after decimal point

1.41421 ..........最多20万个数字

1.41421..........upto 200 000 digits

有什么方法可以打印吗?

Is there any approach to print like this?

推荐答案

以下是使用GNU GMP库的问题代码.该代码将打印出1000位数的sqrt(2),并在行中增加带注释的数字,以满足您的要求.

Here is the code for your question which using GNU GMP library. The code will print 1000 digits of sqrt(2), increase the number in the lines with comment to satisfy your request.

#include <stdio.h>
#include <gmp.h>

int main(int argc, char *argv[])
{
  mpf_t res,two;
  mpf_set_default_prec(1000000); // Increase this number.
  mpf_init(res);
  mpf_init(two);
  mpf_set_str(two, "2", 10);
  mpf_sqrt (res, two); 
  gmp_printf("%.1000Ff\n\n", res); // increase this number.
  return 0;
}

请使用以下命令对其进行编译:

Please compile it with the following command:

$gcc gmp.c  -lgmp -lm -O0 -g3

这篇关于找到2的平方根的尽可能多的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆