gpt4 book ai didi

c++ - 2的平方根,点后需要x位数字

转载 作者:行者123 更新时间:2023-11-30 20:24:22 25 4
gpt4 key购买 nike

尝试了一切,但我不知道该怎么做。我是编程新手,试图管理 C 和 C++ 语言,但有时我停留在零。谁能帮我这个?我需要得到 2 的平方后的 X 位数字。听起来很容易,但它不适合我。感谢您的关注。这是一个想法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
double a = 2;
double b = 1;//somehow i read about dividing, and square of 2 is between 1 and 2;
double c = (a-b*b)/(2*b);
double d = b+c;
double c1= (a-d*d)/(2*d);
double d1 = c1+d;
printf("%.16f\n%.16f\n",d1, d);

}

最佳答案

有什么可以阻止您在点后简单地打印 X 个数字吗?

像这样:(对于普通 c)

#include <stdio.h>
#include <math.h>
int main (void)
{
double myRoot = sqrt(2.00);
printf("%.3f\n", myRoot);
return 0;
}

double 是精度超过整数的数字。 sqrt() 函数返回一个 double 值并接受 double 类型的输入。它是一个 C 标准函数,其定义包含在头文件 math.h 中。 printf() 调用中的 %3.f 表示它应该打印点后面 3 位的数字。

值得注意的是,c 和 c++ 是完全不同的语言,因此您需要选择要处理的语言。

关于c++ - 2的平方根,点后需要x位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com