简体   繁体   中英

same codes in C++ and python but different outputs

I have converted my C++ code into python but although I have the same outputs for the functions separately( T(l,m,v,s,r) in C++= T(l,m,v,s,r) in pyhton or t[l][m][w][i][j] in C++= t[l][m][w][i][j] in python ) but in the beneath part of the codes the outputs are not the same ( T(l,m,v,s,r) in C++=! T(l,m,v,s,r) in python or t[l][m][w][i][j] in C++=! t[l][m][w][i][j] in python )).

void P(){
    int i,j,l,m;
    for(i=0;i<5;i++){
        s=smin+i*deltas;
        r=rmin;
        for(j=0;j<634;j++){
            r*=deltar;
            for(l=0;l<=5;l++){ 
                for(m=l;m<=5;m++){
                    t[l][m][v][i][j]=T(l,m,v,s,r);
                    t[m][l][v][i][j]=t[l][m][v][i][j];
                    t[l][m][w][i][j]=T(l,m,w,s,r);
                    t[m][l][w][i][j]=t[l][m][w][i][j];
                    if(t[l][m][v][i][j]<1e-20 && t[m][l][w][i][j]<1e-20)break;
                }
            }
        }

    }
}

and python:

def P():
    for i in range(0,5):
        s=smin+i*deltas
        r=rmin
        for j in range(0,634):
            r*=deltar
            for l in range(0,6):
                for m in range(l,6):    

                    t[l][m][v][i][j]=T(l,m,v,s,r)
                    t[m][l][v][i][j]=t[l][m][v][i][j]
                    t[l][m][w][i][j]=T(l,m,w,s,r)
                    t[m][l][w][i][j]=t[l][m][w][i][j]

                    if t[l][m][v][i][j]<1e-20 and t[m][l][w][i][j]<1e-20:
                        break

I will really appreciate if someone would help.

The inner-most loop is different :

C++:

            for(m=l;m<=5;m++)

m will have the values [1,2,3,4,5]

Python:

            for m in range(l,5) 

m will have the values [1,2,3,4]. 5 is not included . You have to use range(1,6) to represent the values [1,2,3,4,5]

The python in range function is equivalent to a < loop. for(i=0;i<5;i++) == for i in range(0,5)

However for(l=0;l<=5;l++),= for l in range(0,5) so you might want to change it to for l in range(0,6)

3rd and 4th for loop has = to in C++, so change the one in python ie 3rd and 4th to range(0,6)

def P():
for i in range(0,6):
    s=smin+i*deltas
    r=rmin
    for j in range(0,635):
        r*=deltar
        for l in range(0,6):
            for m in range(l,6):    

                t[l][m][v][i][j]=T(l,m,v,s,r)
                t[m][l][v][i][j]=t[l][m][v][i][j]
                t[l][m][w][i][j]=T(l,m,w,s,r)
                t[m][l][w][i][j]=t[l][m][w][i][j]

                if t[l][m][v][i][j]<1e-20 and t[m][l][w][i][j]<1e-20:
                    break

try This code problem is in range(0,5) means 0,1,2,3,4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM