【发布时间】:2020-06-12 22:41:44
【问题描述】:
当我尝试将数字从 1e+050 转换为其自然形式时,它会显示 100000000000000007629769841091887003294964970946560 而不是 10000000000000000000000000000000000000000000000这是代码;
#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;
}
【问题讨论】:
-
一个
long double可能(取决于您的具体环境)不能代表100000000000000000000000000000000000000000000000000。您将需要使用基于十进制的浮点值(即 this 假设编译器支持)或可以处理如此大整数的大整数库,例如 GNU MP 或上面讨论的 Boost 库。 -
@nanofarad:有时可以。如果
double使用 base2 或 base10,则在 C++ 中未指定。 base2 不能代表它,但是 base10 可以:( -
@MooingDuck 啊,我不知道作为规范的一部分支持 base-10 浮点数。很高兴知道!
-
规范定义了十次方格式,但这并不意味着任何有用的实现都支持它。
标签: c++ c++11 scientific-notation