0

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.

6
  • The ranges are the same on the two outer loops, but not the inner ones. (Stick to half-open intervals in C++. It makes life easier, not harder.) Commented Jun 5, 2020 at 7:56
  • for m in range(l,5) should be for m in range(l,6) as if you want to include 5 (<=5 inc++), you will have to add one more to python, similar to what you did for outer loops Commented Jun 5, 2020 at 8:00
  • I’m voting to close this question because you've applied changes to your question in reaction to comments without explaining what changed. It has to be assumed that the bug is gone. Commented Jun 5, 2020 at 8:16
  • @Sarajn It would be better to close this and reask. Commented Jun 5, 2020 at 8:33
  • Without knowing what the function T() does .. it is not possible to answer your question. Please either share this function code or provide a reproducible example where both codes give different results. Commented Jun 5, 2020 at 9:57

4 Answers 4

2

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]

Sign up to request clarification or add additional context in comments.

1 Comment

Without knowing what the function T() does .. it is not possible to answer your question. Please either share this function code or provide a reproducible example where both codes give different results.
0

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)

Comments

0

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

Comments

0
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.

1 Comment

Maybe the problem is not in this code it is in the conversation of float and int. can you check for all the variables for int or float are the same or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.