如何在C 中找到具有20位精度的平方根
How to find square root with 20 digit precision in C++?
嗨,大家好,我一直坚持这项作业,我需要使用 bisection方法 precision 10^-20 aka 0.0000000000000000000001找到方程的根我虽然是因为我没有使用长double,也没有在数字结束时使用l,但是即使我使用它,我的最后3位数字也不正确,对于下面给出的代码,我要求您给您提供数字我的案例为5,所以我得到2.3227751229355622087
正确的答案应为2.3227751229355622988,但我真的找不到我的错误,但如果某些人帮助我解决这个问题,我会很高兴。
供您参考,以下是二等方法的描述和插图。
这是我的代码:
#include<iostream>
#include<cmath>
#include<math.h>
#include<iomanip>
using namespace std;
long double f(long double x, long double a);
long double F = 123456L % 100L;
long double f(long double x, long double a)
{
long double sum = pow(x, 5) - a*x - F;
return sum;
}
int main()
{
cout.setf(ios::fixed);
long double a, b, c, fa, fb, fc;
long double e;
long double aa;
bool flag = true;
while (cin >> aa)
{
cout.precision(19);
flag = true;
a = 0L;
b = 10L;
e = 0.00000000000000000001L;
if (f(a, aa)*f(b, aa)>0)
{
flag = false;
}
while(fabs(a-b)>=e){
c = (a + b) / 2.0L;
fa = f(a, aa);
fb = f(b, aa);
fc = f(c, aa);
if (fc == 0)
{
break;
}
if (fa*fc>0)
{
a = c;
}
else if (fa*fc<0)
{
b = c;
}
}
if (flag == true)
{
cout << c << endl;
}
else
{
cout << "NO SOLUTION" << endl;
}
}
return 0;
}
问题是我使用了错误的编译器(Visual Studio)。
安装另一个编译器后,20位数精度没有任何问题。随着我进行的更多调查,似乎有些编译器在十进制之后的限制为14或15。如果C 拒绝将您的号码舍入更高的精度,则更改编译器:)
相关文章:
- 如何打印boost多精度128位无符号整数
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 位设置为浮点数或双精度值 c++
- 浮动距离为80位长双精度
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度
- 预 C++20 位字段零初始化
- c++只关心2位小数,没有设置精度
- 此代码中的双精度仅为 6 位
- 64 位整数到双精度冲突
- 将 32 位浮点数和不强制转换的 32 位整数与双精度进行比较,当其中一个值可能太大而无法完全适合另一种类型时
- 有没有办法构造一个 constexpr 函数来获取双精度的位表示
- 为什么 std::setprecision 显示不存在的精度位以及如何查看实际浮点数
- 分配两个双精度值,保证产生相同的位集模式
- 如何将精度设置为C 的100位数字
- 将双精度转换为本地8位
- 如何在不丢失第一位数字的情况下打印出双精度值
- 如何在C 中找到具有20位精度的平方根
- 如何从64位整数获得更高的20位
- 存储超过20位值的数据类型