gpt4 book ai didi

c - C 中的 Metropolis 函数的意外输出

转载 作者:行者123 更新时间:2023-11-30 15:47:24 26 4
gpt4 key购买 nike

我目前正在用 C 编写一些用于模拟退火的代码。我在使用此函数时遇到了问题:

int metrop (E, E1, T)
{
int j;
if (exp((-E-E1)/T) > ran())
j = 2;
printf("Random# %lf\n",ran());
if (E-E1<0)
j = 2;

else
j = 0;

if (j=1)
printf("Accepted %d",j);
if (j=0)
printf("Rejected");
return(j);
}

无论 E、E1 和 T 的输入如何,“if (j=1) printf("Accepted %lf",j);"似乎已被执行;输出为“Accepted 1”。即使在这段代码中 j 不可能等于 1。我怀疑我可能引用了错误的数据类型或类似的东西。大家有什么想法吗?

谢谢!

最佳答案

你想要

if (j == 1)  // j == 1 evaluates to true if j is 1, evaluates to false otherwise

使用:

if (j = 1)

将 j 设置为 1,本质上是

if (1)  // always true

关于c - C 中的 Metropolis 函数的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com