简体   繁体   English

C++ 和 python 中的代码相同,但输出不同

[英]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 )).我已经将我的 C++ 代码转换为 python 但是虽然我分别为函数有相同的输出T(l,m,v,s,r) C++ 中的T(l,m,v,s,r) = pyhton 中的 T(l,m,v,s,r) 或t[l][m][w][i][j] in C++= t[l][m][w][i][j] in python )但在代码的下面部分,输出不是相同的( C++ 中的 T( T(l,m,v,s,r) =! python 中T(l,m,v,s,r)t[l][m][w][i][j]在 C++=! t[l][m][w][i][j]在 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:和 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++: C++:

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

m will have the values [1,2,3,4,5] m将具有值 [1,2,3,4,5]

Python: Python:

            for m in range(l,5) 

m will have the values [1,2,3,4]. m将具有值 [1,2,3,4]。 5 is not included . 5 不包括在内 You have to use range(1,6) to represent the values [1,2,3,4,5]您必须使用range(1,6)来表示值 [1,2,3,4,5]

The python in range function is equivalent to a < loop. function 范围内的 python 等效于 < 循环。 for(i=0;i<5;i++) == for i in range(0,5) 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)但是 for(l=0;l<=5;l++),= for l in range(0,5) 所以你可能想把它改成 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)第 3 和第 4 个 for 循环在 C++ 中具有 = to,因此将 python 中的一个更改为第 3 和第 4 个范围(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.试试这个代码问题在 range(0,5) 表示 0,1,2,3,4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++和python中的算法相同,但输出不同 - Same algorithm in c++ and python but different output python 中的素数相同的程序不同的输出 - prime numbers in python same programs different outputs C ++输出与Python不同的消息(从串口读取) - C++ outputs different message than Python (Reading from serial port) 相同的python代码块在不同的时间给出不同的输出 - Same python code block gives different outputs at different time 相同的算法实现在Python和C ++中有不同的结果? - Same algorithm implementation has different retults in Python and C++? 相同算法的C ++和Python版本给出不同的结果 - C++ and Python version of the same algorithm giving different result 相同的算术运算在C ++和Python中给出了不同的结果 - The same arithmetic operations give different results in C++ and Python 打印相同的HTTPResponse对象将返回不同的输出-Python - Printing same HTTPResponse Object returns different outputs - Python 在 Python 中为同一模块中的不同输出创建多个记录器 - Creating multiple loggers in Python to different outputs in the same module 对 python function 具有相同参数的多次调用输出不同的返回值 - mutiple calls with same args to python function outputs different return values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM