2015-07-12 34 views
1

我有一个程序,做一个辛ODE集成(物理/数学),我想将时间序列导出到一个.dat文件>然而,写入的数字dat文件只有6位精度。我写了setprecision(15);写之前,但它没有改变。我还张贴代码的一部分,没有实际的ODE求解:C++如何写一个.dat文件的双精度的全精度

#include <iostream> 
#include <ctime>   
#include <cstdlib> 
#include <string> 
#include <sstream>    
#include <iomanip>    
#include <cmath>    
#define pi 3.14159265358979 
using namespace std; 

int main(int argc, char **argv) { 
// many stuff here, probably irrelevant, 

ostringstream osE, osb, ospx; 
    osb<<b; // using this, I can use some numbers into the file's name 
    osE<<E; 
    ospx<<px0; 
filenamex = "Antidot_v4_x(t)_E=" + osE.str() + "_px0=" + ospx.str() + "_b=" + osb.str() + ".dat"; 
ofstream file1(filenamex.c_str()); 
file1<<t<<"\t"<<x0<<endl; 
while(i<=N){ 
     i++; 
     McLachanAtela(x, y, px, py, h); 
// Does the 4-step ODE solver. x are initial values and after the 
// function call x are final values after h time 

     setprecision(15); 
     file1<<t<<"\t"<<x<<endl; //I use this to write values in file 
    } 
return 0; 
} 

所以,当我打开文件1(它没有命名文件1)里面的值是6位数字。我怎样才能写完整的16位准确数字?谢谢。 对于完整性我也发表名为虚空功能:

void McLachanAtela (double& previousx, double& previousy, double& previouspx, double& previouspy, double timestep){ 
    // Atela Coefficients 
    double c[4]={0.134496199277431089, -0.224819803079420805, 0.756320000515668291, 0.334003603286321425}; 
    double d[4]={0.515352837431122936, -0.085782019412973646, 0.411583023616466525, 0.128846158365384185}; 
    // Symplectic Algorithm (at dimensionless form) 
    for(int j=0; j<4; j++){ 
                 //this is the derivative of the potential : 
     previouspx = previouspx - d[j]*timestep*b*pi*sin(pi*previousx)*cos(pi*previousy)*(pow(cos(pi*previousx)*cos(pi*previousy),b-1)); 
     previouspy = previouspy - d[j]*timestep*b*pi*sin(pi*previousy)*cos(pi*previousx)*(pow(cos(pi*previousx)*cos(pi*previousy),b-1)); 
     previousx = previousx + c[j]*previouspx*timestep; 
     previousy = previousy + c[j]*previouspy*timestep; 
     //cout<<" dpx = "<<d[j]*timestep*b*pi*sin(pi*previousx)*cos(pi*previousy)*(pow(cos(pi*previousx)*cos(pi*previousy),b-1))<<endl; 

    } 
} 
+0

您的个人信息的定义将导致如果您曾经用过Solaris long double,那么您会遇到麻烦:它不够精确。 –

+0

是的,这是真的,我不再使用它了!谢谢。 –

回答

1

你需要一套精确注入到流:

文件1 < < setprecision(15)< <

+0

非常感谢。 另一个工作是file1.setprecision(15);在添加值之前。 –

+0

您可以使用['DBL_DIG'](http://stackoverflow.com/a/9999508/3087952),它是从'' – nonsensation