簡體   English   中英

C ++如何將雙精度全精度寫入.dat文件

[英]C++ How to write full precision of doubles into a .dat file

我有一個執行辛ODE積分(物理/數學)的程序,我想將時間序列導出到.dat文件>但是,在dat文件中寫入的數字精度只有6位數字。 我寫了setprecision(15); 在寫之前,但它什么也沒有改變。 我還發布了部分代碼,沒有實際的ODE求解器:

#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;
}

因此,當我打開file1(未命名為file1)時,其中的值是6位數字。 如何寫完整的16位數字? 謝謝。 為了完整起見,我還發布了稱為以下內容的void函數:

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;

    }
}

您需要將設置精度注入流中:

file1 << setprecision(15)<<

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM