从double到const int的C ++类型转换无法正常工作

时间:2013-02-08 23:49:43

标签: c++ types casting static-cast

我有一个const int类型的变量,但它所依赖的参数是double类型。当我尝试将它从'double'转换为'const int'时,它无法正常工作。例如,当N应该是991时,它输入为990.我已经尝试了几种方法,只有一种方法有效,但我不确定这种方法是否会一直有效。以下是我尝试过的一些方法:

第一种方法:

const int N = (Ls-1)/dx + 1;

第二种方法:

const int N = static_cast<const int>((Ls-1)/dx) + 1;

第三种方法:

double Z = (Ls-1)/dx + 1;
const int N = Z;

第四种方法(仅工作方法):

double Z = (Ls-1)/dx;
const int N = Z + 1;

请注意,dx是一个值,使得(Ls-1)/ dx的余数总是为零(即它总是一个整数值)。无论如何可以解释为什么其他方法不起作用,以便我可以更好地理解类型转换?

编辑:根据要求,我上传整个代码以显示一切是如何运作的:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <cmath>
#include <algorithm>

#define pi 3.14159265

using namespace std;

//Define Fluid Properties
double rho_L = 998; //Liquid Density
double rho_LG = 828.9; //Liquid-Gas Density Ratio
double mu_L = 0.000798; //Liquid Viscosity
double mu_LG = 40.24; //Liquid-Gas Viscosity Ratio
double sigma = 0.0712; //Surface Tension
double nu_G = (mu_L/mu_LG)/(rho_L/rho_LG);

//Define Injector Properties
double Uinj = 56.7; //Injection Velocity
double Dinj = 0.0998; //Injector Diameter
double theta = 15.0*pi/180.0; //Spray Cone Angle
double L = 500.0*Dinj; //Atomization Length
double Ls = L/Dinj; //Normalized Atomization Length

//Define Solver Parameters
double K = 5294; //Viscous Dissipation Coefficient
double Eps = pow(10,-5); //Residual Error
double dx = 0.0001; //Step Size
double Ui = 10; //Initial Guess
//const int Z = static_cast<const int>((Ls-1)/dx + 1) + 1;
const int N = (Ls-1)/dx + 1;//Z;

double deriv (double U, double X, double delta, double m)
{
    double dudx;
    dudx = -(1.0/delta)*(1.0/U)*(U - sqrt(1.0 - U)/sqrt(m*X*X))*(U - sqrt(1.0 - U)/sqrt(m*X*X));
    return (dudx);
}

int main()
{
    //Declare Variables
    int max_step;
    double ERR;
    int step;
    double DEN;
    double SMD;
    double m;
    double Ug;
    double Re;
    double Cd;
    double delta;
    double K1;
    double K2;
    double K3;
    double K4;

    //Allocate Memory From Heap
    double *U = new double [N];
    double *X = new double [N];

    //Initialize Vectors and Variables
    DEN = 0.5*rho_L - (4.0/3.0)*K*(mu_L)/(Uinj*Dinj*Dinj)*L;

    m = 4.0/rho_LG*tan(theta)*tan(theta);

    for (int i = 0; i < N; i++)
    {
        X[i] = 1.0 + dx*i;
    }
    U[0] = 1.0;

    max_step = 1;
    ERR = 1;
    step = 0;
    while(abs(ERR) > Eps && step < max_step)
    {

        //Calculate Ug
        Ug = sqrt(1.0 - (Ui/Uinj))/sqrt(m*Ls*Ls)*Uinj;

        //Calculate SMD
        SMD = 6.0*sigma/(DEN*(Uinj*Uinj - Ui*Ui));

        //Calculate Re # and Drag Coefficient
        Re = abs(Ui-Ug)*SMD/nu_G;

        if(Re <= 0.01)
        {
            Cd = (0.1875) + (24.0/Re);
        }
        else if(Re > 0.01 && Re <= 260.0)
        {
            Cd = (24.0/Re)*(1.0 + 0.1315*pow(Re,(0.32 - 0.05*log10(Re))));
        }
        else
        {
            Cd = (24.0/Re)*(1.0 + 0.1935*pow(Re,0.6305));
        }

        //Determine New U
        delta = (4.0/3.0)*(1.0/Cd)*(rho_LG)*(SMD/Dinj);

        //RK4
        for (int i = 0; i < N-1; i++)
        {
            K1 = deriv(U[i],X[i],delta,m);
            K2 = deriv(U[i]+0.5*dx*K1,X[i]+0.5*dx,delta,m);
            K3 = deriv(U[i]+0.5*dx*K2,X[i]+0.5*dx,delta,m);
            K4 = deriv(U[i]+dx*K3,X[i+1],delta,m);
            U[i+1] = U[i] + dx/6.0*(K1 + 2.0*K2 + 2.0*K3 + K4);
            //if(i >= 0 && i <= 3)
                //cout << i << " " << K1 << " " << K2 << " " << K3 << " " << K4 << " " << U[i] << endl;
        }

        ERR = abs(U[N-1]*Uinj - Ui)/Ui;

        Ui = U[N-1]*Uinj;

        step = step + 1;
    }

    SMD = 6.0*sigma/(DEN*(Uinj*Uinj - Ui*Ui));

    cout << "U = " << Ui << endl;
    cout << "SMD = " << SMD << endl;
    cout << "DEN = " << DEN << endl;
    cout << "Ug = " << Ug << endl;
    cout << "m = " << m << endl;
    cout << "delta = " << delta << endl;
    cout << "Re = " << Re << endl;
    cout << "Cd = " << Cd << endl;
    cout << "U* = " << U[N-1] << endl;
    cout << "Error = " << ERR << endl;
    cout << "step = " << step << endl;

    //Output Data Into Text File
    ofstream outputdata("result-500-15.txt");
    for (int i = 0; i < N; i++)
    {
        outputdata << X[i] << " " << U[i] << '\n';
    }
    outputdata.close();

    delete [] U;
    delete [] X;

    return 0;
}

1 个答案:

答案 0 :(得分:3)

你的猜测是正确的:0.1在二进制中没有有限的表达式。这是一个相当复杂的问题,并且许多极端情况通常会通过添加0.01来解决,如评论中所述。 (这在很大程度上取决于您期望的值等。)

你的问题表明,商总是应该是一个整数。在这种情况下,保持正确结果的正确方法是不要使用任何double开头(对于LsdxZ)。使用小数类型(没有内置在C ++中,使用您自己的或库),任意精度的十进制类型(再次,使用像gmp这样的库 - 如果您知道所有数字都有一个终止是明智的十进制表达式,或者,最简单:如果Lsdx都保证在小数点后最多有n个数字,则将它们乘以10^n并使用整数类型


好的,你的代码与我的预期完全不同。在这种情况下,在我看来,正确的做法是确定步骤数N并从中计算dx而不是相反:

const int N = 10000;
double dx = (Ls-1.0)/(double)(N-1);

如果你想从dx的值开始并选择N以便计算dx的值,请在程序启动时询问用户:

#include <cmath>

double dxestim;
cout << "dx should be close to: ";
cin >> dxestim;
cout << "Candidate values for N: " << endl;
int N1 = (int) floor((Ls-1)/dx + 1.0);
int N2 = (int) ceil((Ls-1)/dx + 1.0);
cout << N1 << " gives dx = " << (Ls-1.0)/(double)(N1-1) << endl;
cout << N2 << " gives dx = " << (Ls-1.0)/(double)(N2-1) << endl;
cout << "Please choose N: ";
cin >> N;
...