2012-10-22 57 views
0

我刚刚注意到,当我将代码片段中的最后一行从potential =+ rep_pot更改为potential = potential + rep_pot时,我得到完全不同的行为。有没有人知道为什么会发生这种情况?为什么这两个代码片段不同

double potential = euclideanDistance(i, goal); 
for (IntPoint h: hits){ 
    double dist = euclideanDistance(i, h); 
    double a = range - dist; 
    double rep_pot = (Math.exp(-1/a))/dist; 
    potential =+ rep_pot; 
} 
+0

你看到两种不同的行为是什么? – Azodious

+0

什么是= +?你的意思是+ =? – giorashc

+0

它应该是'+ =',而不是'= +':) – Azodious

回答

1

是的,因为这两个东西是不等价的。

potential =+ rep_pot; 

在这里,我们有潜在的分配表达

你intendet以不同的方式写看起来事情的“一元加rep_pot”的值:

potential += rep_pot; 

这相当于

potential = potential + rep_pot; 
1

这是因为

potential = potential + rep_pot 

类似于

potential += rep_pot 

potential =+ rep_pot; 

相同

potential = rep_pot; 
1

你可能意思是+=。在你的情况下,它被解释为x = +x这是x = x

使用+=

potential += rep_pot; 
相关问题