0

我不确定这是 C 还是 C++ 代码,但我认为这并不重要。

在这段代码中:

x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));

t = u 是什么意思?它不是将 t 分配给你,是吗?因为它没有意义,因为它实际上被设置为上一行中的其他内容:

t = exp(a1*log(x)+b1*log(1.-x) + afac);
u = err/t;
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
if (x <= 0.) x = 0.5*(x + t);
if (x >= 1.) x = 0.5*(x + t + 1.);
4

7 回答 7

8

t = u 是什么意思?

它是一个更大的子表达式的一部分,

t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))))

它不是将 t 分配给你,是吗?

不,它是将那个复杂表达式的值分配给t. 然后在完整的表达式中使用该赋值的结果:

x -= (t = <stuff> );

从概念上讲,这与以下内容相同:

t = <stuff>
x = x - t

因为它没有意义,因为它实际上被设置为上一行中的其他内容

坦率地说,整套陈述对我来说没有意义。无论如何,t在第 1 行设置,在第 2 行使用,并在第 3 行再次设置。

于 2012-04-05T14:05:12.467 回答
7

Quite simply:

x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
|     |   \_____________________________________/|
|     |        Calculate this monstrosity        |
|     \_________________________________________/|
|                   Assign it to t               |
\________________________________________________/
                  Subtract that from x

In C (and C-like languages) the "result" of an assignment can be used for other things. So, for example:

x = (a = a - 1);   // decrements a and assigns that to x as well
x += (a = 1 - a);  // toggles a between 1 and 0 and adds to x (x increases
                   //   every second time).

The relevant bit of the C standard is C11, 6.5.16 Assignment operators, paras 2 and 3:

2/ An assignment operator shall have a modifiable lvalue as its left operand.

3/ An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.

于 2012-04-05T14:07:58.360 回答
2

在 C 和 C++ 中,赋值返回一个值,即左侧的值。

a = 1 + (b = 4);

相当于:

b = 4;
a = 1 + b;

所以:

x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));

是相同的:

t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));
x -= t;
于 2012-04-05T14:03:48.363 回答
1

在您的代码中:

  x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));

让我假设

  A=(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));

然后该代码将变为

  x -= (t = u/A);

然后它可以读作:

  t = u/A;
  x -= t;

如果您有任何问题,请随时提出。

于 2012-04-05T14:09:43.350 回答
0

Using = will indeed assign a value, and will return the value you are assigning. In your case, t is being assigned the result of u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))).

于 2012-04-05T14:07:18.393 回答
0

The overall calculation : u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))) is assigned to t and what ever value of t is there ,this is performed.

x=x-t
于 2012-04-05T14:07:46.747 回答
0

该语句可以分为两个子语句,为了便于阅读,它可能应该。

t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));
x -= t;

本质上,作者将整个公式的一个子部分分配给一个中间变量(t)以供以后使用。

于 2012-04-05T14:09:37.557 回答