Sarajn
Sarajn

Reputation: 1

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.

Upvotes: 0

Views: 172

Answers (4)

Hiren Namera
Hiren Namera

Reputation: 424

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.

Upvotes: 0

Ramanjaneyalu T
Ramanjaneyalu T

Reputation: 36

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

Upvotes: 0

Pedro Holanda
Pedro Holanda

Reputation: 301

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)

Upvotes: 0

Aziz
Aziz

Reputation: 20705

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]

Upvotes: 2

Related Questions