2015-07-12 32 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