[英]Why does C++ show the wrong number?
I have a little calculation. 我有一点计算。 Right now I calculate p_o and the value I get is correct, but somehow p_TR, where it gets the value from, is shown wrong.
现在,我计算出p_o,并且得到的值是正确的,但是以某种方式从中获取值的p_TR显示为错误。 The values I get are: p_o= 0.3067666187328126 (right) p_TR= 6.94807050163253e-310 (wrong)
我得到的值是:p_o = 0.3067666187328126(正确)p_TR = 6.94807050163253e-310(错误)
Like you can see close to the end of the code, it says: p_o = p_TR 就像您在代码末尾看到的那样,它说:p_o = p_TR
I compile the code on Opensuse in the shell withe the g++ command. 我使用g ++命令在外壳中的Opensuse上编译代码。
I read that floating point numbers vary in decimal points, but this is not the same I would say. 我读到浮点数的小数点不同,但这与我说的不一样。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
//Header der angezeigt wird bei Pragrammstart
cout << "----------------------------------------------------------------------\n";
cout << "**********************************************************************\n";
cout << "----------------------------------------------------------------------\n";
cout << "===>Exakter\n";
cout << "======>Riemann\n";
cout << "=========>Loeser" << endl;
cout.flush();
//physikalische Groessen
const float k = 1.4; //isentropen Exponent
const float M = 28.9; //Molare Masse [g/kmol] des Fluids
const float R = 8314.51; //universelle Gaskonstante [J/(kmol*K)]
cout << "\nStoff-/Physikalische Groessen\n" << "k=" << k << "; M=" << M << "; R=" << R << endl;
//Anfangswerte Links
const float rho_L = 1; //Dichte [kg/m^3]
const float u_L = 0; //Geschwindigkeit [m/s]
const float p_L = 1; //Druck [Pa]
const double T_L = (p_L*M)/(R*rho_L); //Temperatur[K]
const double RmT_L = p_L/rho_L; //Zwischen Groesse fuer a_L
const double a_L = pow((k*(R/M)*T_L),(0.5)); //Schallgeschwindigkeit [m/s]
cout << "\nWerte auf der rechten Seite:\n" << "rho_L=" << rho_L << "; u_L=" << u_L << "; p_L=" << p_L << "; T_L=" << T_L << "; a_L=" << a_L << "; RmT_L=" << RmT_L << endl;
//Anfangswerte Rechts
const float rho_R = 0.125;
const int u_R = 0;
const float p_R = 0.1;
const double T_R = (p_R*M)/(R*rho_R);
const double RmT_R = p_R/rho_R; //Zwischen Groesse fuer a_R
const double a_R = pow((k*(R/M)*T_R),(0.5));
cout << "\nWerte auf der linken Seite:\n" << "rho_R=" << rho_R << "; u_R=" << u_R << "; p_R=" << p_R << "; T_R=" << T_R << "; a_R=" << a_R << "; RmT_R=" << RmT_R << endl;
//Allgemeine Anfangswerte
const float du = u_R-u_L; //Geschwindigkeitsdifferenz [m/s]
const double du_krit = ((2*a_L)/(k-1)) + ((2*a_R)/(k-1)); //kritische Geschwindigkeitsdifferenz [m/s] (positive Druck Bedingung)
const double TOL = 1e-6; //Toleranz fuer p*
cout << "\nWeitere Groessen:\n" << "du=" << du << "; du_krit=" << du_krit << "; TOL=" << TOL << endl;
cout.flush();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Variablen Iteration p*
//Berechnung des Schätzwertes
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
const double p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
const double p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
//Variablen fuer Iteration
int n = 1;
double p_n = p_o; //p fuer Iterationen
float CHA = 1;
//Hilfsvariablen zum einfacheren rechnen
const double AL = 2/((k+1)*rho_L);
const double AR = 2/((k+1)*rho_R);
const double BL = ((k-1)*p_L)/(k+1);
const double BR = ((k-1)*p_R)/(k+1);
cout << "\np_TR=" << std::setprecision(16)<< p_TR;
cout << "\nWerte fuer Iteration:" << "\np_o=" << std::setprecision(16) << p_o << "\nAL=" << AL << "; BL=" << BL << "\nAR=" << AR << "; BR=" << BR << endl;
cout.flush();
Because you are declaring p_TR two times in different scopes, so you basically have two different variables in two different scopes having the same name p_TR. 因为您要在不同的作用域中两次声明p_TR,所以基本上在两个不同的作用域中有两个不同的变量,它们具有相同的名称p_TR。 One is instanced in the main scope, the other is instanced in your if/else block.
一个在主作用域中被实例化,另一个在您的if / else块中被实例化。
Same problem with p_o in the else block. else块中的p_o同样存在问题。
Just remove the "const double" inside your if/else block, it should do the trick. 只需删除if / else块中的“ const double”,就可以解决问题。
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.