gpt4 book ai didi

C++ 如何将 double 的全精度写入 .dat 文件

转载 作者:行者123 更新时间:2023-11-30 05:40:52 25 4
gpt4 key购买 nike

我有一个执行辛 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;

}
}

最佳答案

您需要将设置的精度注入(inject)到流中:

文件1 << 设置精度(15) <<

关于C++ 如何将 double 的全精度写入 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31367368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com