c++ - 如何使从科学计数法的转换更精确?

标签 c++ c++11 scientific-notation

当我尝试将数字从1e + 050转换为其自然形式时,它显示我100000000000000007629769841091887003294964970946560,而不是100000000000000000000000000000000000000000000000000000000。如何使转换更精确?这是代码;

#include <iostream>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <iomanip>
using namespace std;
long double range, a, b;
int main(int argc, char** argv) {
range = pow(10,50);
cout << "enter a and b" << endl;
cin >> a >> b;  
cout.setf(ios::fixed); 
if((a >= range)||(b >= range)||(a <= -range)||(b <= -range)){
    cout << "the number is too large" << endl;
}
else{
    cout << "a+b= " << a + b << endl;
    cout << "a-b= " << a - b << endl;
    cout << "a*b= " << a * b << endl;
}
cout << range << endl;
return 0;
}

最佳答案

double可以精确表示的唯一数字是可以通过将+/-2⁵³范围内的整数乘以或除以2的整数来产生的。值1E22为2,384,185,791,015,625乘以4,194,304,即该形式的值(请注意2×3为9,007,199,254,740,992)。比那大十的幂不是那种形式。

关于c++ - 如何使从科学计数法的转换更精确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62353478/

相关文章:

将二进制大整数转换为科学计数法并截断为 1 位尾数的算法

c++ - 如何使用模块在 C 中编译应用程序?

c++ - 减少 C++ 中的 std::regex 编译时间

c++ - shared_ptr<> 的自定义删除器给出 'no context error'

c++ - 如何确保成员函数被调用一次

c++ - 如何从文件 C++ 中读取带有科学记数法的 float ?

Python用科学计数法表示5.26E-325是0,但是5.26E-324是5e-324,为什么?

c++ - 我的一些函数出现 "undefined reference"错误,我不知道为什么

c++ - 使用opencv检测水面上的白球的最佳方法是什么?

c++ - 我可以对仅 move 类型的 vector 进行列表初始化吗?