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

问题描述 投票:1回答:1

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

#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;
}
c++ c++11 scientific-notation
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.