问题描述
0
下来投票
最爱
我处理的代码使用非常少量的订单10 ^ -15到10 ^ -25,i尝试使用双倍和长双,但我得到一个错误的答案,因为0.000000000000000000001舍入为0或像0.00000000000000002这样的数字表示为0.00000000000000001999999999999,因为即使是1/1000000的一小部分在我的最终答案中有显着差异,请建议我适当的解决方案。谢谢
我尝试过:
#include < iostream >
#include < math.h >
#include < stdlib.h >
#include < iomanip >
使用 命名空间标准;
int main()
{
double sum,a, b,c,d;
a = 1 ;
b = 1 * pow( 10 , - 15 跨度>);
c = 2 * pow( 10 , - 14 跨度>);
d = 3 * pow( 10 , - 14 跨度>);
sum = a + b + c + d;
cout<< fixed;
cout<< setprecision( 30 );
cout<< a:<< a<< endl< < b:<< b<< endl<< c:<< c<< endl
<< d:<< d<< endl;
cout<< sum:<< sum<< endl< < ENDL;
a = a / sum;
b = b / sum;
c = c / sum;
d = d / sum;
sum = a + b + c + d;
cout<< a:<< a<< endl< < b:<< b<< endl<< c:<< c<< endl
<< d:<< d<< endl;
cout<< sum2:<< sum<< ENDL;
return 0 ;
}
预期产量应为
a:1.000000000000000000000000000000
b:0.000000000000000000000000000000000
c:0.000000000000020000000000000000
d:0.000000000000030000000000000000
总和:1.000000000000051000000000000000
a :1.000000000000000000000000000000
b:0.00000000000000100000000000000000
c:0.000000000000020000000000000000
d:0.000000000000030000000000000000
sum1:1.000000000000051000000000000000
但是,我得到的输出是
a:1.000000000000000000000000000000
b:0.00000000000000100000000000000000
c:0.000000000000020000000000000000
d:0.000000000000029999999999999998
总和:1.000000000000051100000000000000
a:0.999999999999998787999878998887
b:0.000000000000000999999997897899
c:0.000000000000019999999999999458
d:0.000000000000029999999999996589
sum1:0.999999999999989000000000000000
我尝试过double,long double甚至boost_dec_float,但我得到的输出类似。
0down vote
favorite
Im dealing with a code which uses very small numbers of order 10^-15 to 10^-25, i tried using double and long double but i get a wrong answer as either 0.000000000000000000001 is rounded off to 0 or a number like 0.00000000000000002 is represented as 0.00000000000000001999999999999, as even a small fraction of 1/1000000 makes a significant difference in my final answers, please suggest me an appropriate fix. Thank you
What I have tried:
#include <iostream> #include<math.h> #include<stdlib.h> #include<iomanip> using namespace std; int main() { double sum, a, b, c,d; a=1; b=1*pow(10,-15); c=2*pow(10,-14); d=3*pow(10,-14); sum=a+b+c+d; cout<<fixed; cout<<setprecision(30); cout<<" a : "<<a<<endl<<" b : "<<b<<endl<<" c : "<<c<<endl <<" d : "<<d<<endl; cout<<" sum : "<<sum<<endl<<endl; a=a/sum; b=b/sum; c=c/sum; d=d/sum; sum=a+b+c+d; cout<<" a : "<<a<<endl<<" b : "<<b<<endl<<" c : "<<c<<endl <<" d : "<<d<<endl; cout<<" sum2: "<<sum<< endl; return 0; }
The expected output should be
a : 1.000000000000000000000000000000
b : 0.000000000000001000000000000000
c : 0.000000000000020000000000000000
d : 0.000000000000030000000000000000
sum : 1.000000000000051000000000000000
a : 1.000000000000000000000000000000
b : 0.000000000000001000000000000000
c : 0.000000000000020000000000000000
d : 0.000000000000030000000000000000
sum1: 1.000000000000051000000000000000
But, the output i get is
a : 1.000000000000000000000000000000
b : 0.000000000000001000000000000000
c : 0.000000000000020000000000000000
d : 0.000000000000029999999999999998
sum : 1.000000000000051100000000000000
a : 0.999999999999998787999878998887
b : 0.000000000000000999999997897899
c : 0.000000000000019999999999999458
d : 0.000000000000029999999999996589
sum1: 0.999999999999989000000000000000
I tried double, long double and even boost_dec_float, but the output which i get is similar.
这篇关于在C ++中处理非常小的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!