简体   繁体   English

变量通过相同的操作获得不同的值

[英]Variables get different values with same operations

This is a basic understanding problem. 这是一个基本的理解问题。

I tried reordering some code, and though the operations are supposedly equivalent I get different values. 我尝试对一些代码重新排序,尽管据说这些操作是等效的,但我得到了不同的值。 I started with this line: 我从这一行开始:

q, r, m = 10*q, 10*(r-m*t), (10*(3*q+r))//t - 10*m

and changed it to: 并将其更改为:

q*=10;  r=10*(r-m*t);   m= (10*(3*q+r))//t - 10*m;

(With initial values being q= 1, r= 6, t= 3, m= 3). (初始值为q = 1,r = 6,t = 3,m = 3)。

When I run only the second line, m gets value -30 (which is accurate if I followed the order-of-operations correctly), while running the first yields m= 0, which is what the program calls for. 当我仅运行第二行时,m获得值-30(如果我正确遵循操作顺序,这将是正确的),而运行第一行则得到m = 0,这就是程序所要求的。

What am I missing here? 我在这里想念什么? Does the comma method assign the value after all other assignments are done? 在所有其他分配完成之后,逗号方法是否分配值?

The assignments in q, r, m = 10*q, 10*(rm*t), (10*(3*q+r))//t - 10*m are done independently by evaluating the right hand side of all the assignments without anyone affecting the others, meaning that when (10*(3*q+r))//t - 10*m is evaluated, the old value of q is used, not the new 10*q (same with r ). q, r, m = 10*q, 10*(rm*t), (10*(3*q+r))//t - 10*m的赋值是通过评估所有元素的右侧独立完成的分配,而没有任何人影响其他分配,这意味着当评估(10*(3*q+r))//t - 10*m ,将使用q的旧值,而不是新的10*q (与r相同) )。 Notice that the only difference is in the value of m , which depends on the values of r and q , which aren't changing while m is being assigned. 注意,唯一的区别在于m的值,这取决于rq的值,它们在分配m不会改变。

The issue is that your first snippet of code evaluates each expression on the right before assigning the results to your q , r , and m variables. 问题是您的第一个代码片段在将结果分配给qrm变量之前,先评估了右侧的每个表达式。 Your second snippet instead assigns: 您的第二个片段将分配:

q*=10
r=10*(r-m*t)

-before evaluating: -在评估之前:

m= (10*(3*q+r))//t - 10*m

which changes the result. 这会改变结果。 If you must use the latter snippet, you'll need to introduce a temporary variable to store the original q and r variable values for usage in the final expression. 如果必须使用后一个代码段,则需要引入一个临时变量来存储原始qr变量值,以供在最终表达式中使用。

In fact, since q does not depend on either of the other variables, you could in fact assign its value last, keeping the other two in tract, to slightly simplify the expression: 实际上,由于q不依赖于其他任何一个变量,因此实际上您可以最后分配其值,将其他两个变量保留为小写,以略微简化表达式:

r, m = 10*(r-m*t), (10*(3*q+r))//t - 10*m
q *= 10

In the first case, all the assignments use the values of the variables before any assignments are done. 在第一种情况下,在完成任何分配之前 ,所有分配都使用变量的值。

In the second case, you're changing some variables before others (your later assignments are using the new values of the variables). 在第二种情况下,您要先更改一些变量(您以后的分配使用的是变量的新值)。

Compare a, b = b, a with a = b; b = a 比较a, b = b, aa = b; b = a a = b; b = a . a = b; b = a This first will swap the values, the second will not. 第一个将交换值,第二个将不交换值。

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

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