【问题标题】:Nested parenthesis order of evaluation - Depth vs Ltr评估的嵌套括号顺序 - 深度与 Ltr
【发布时间】:2018-05-03 02:43:43
【问题描述】:
public static float BackInOut(float from, float to, float time)
{
    const float s = 1.70158f * 1.525f;
    to -= from;
    if ((time /= .5f) < 1f)
        //     5    6      3      4        0         1      2     7
        return to * .5f * (time * time * ((s + 1f) * time - s)) + from;
    //                  3                     0         1      2
    return to * .5f * ((time -= 2) * time * ((s + 1f) * time + s) + 2f) + from;
}

在第二次返回中是在修改之前(和之后)使用的时间吗?是吗?

    //                  1                     0         2      3
    return to * .5f * ((time -= 2) * time * ((s + 1f) * time + s) + 2f) + from;

或者只有在之后,像这样?不知道吗?

谢谢。

来源:http://robertpenner.com/easing/

编辑:

试图简化:

using System;
namespace New_folder
{
    class Program
    {
        static public int s { get; set; } = 2;
        static private int _test = 10;
        static public int time
        {
            get
            {
                Console.WriteLine(_test);
                return _test;
            }
            set { _test = value; }
        }
        static public void Main(string[] args)
        {
            var test = (time -= 2) * time * ((s + 1f) * time + s);
        }
    }
}

这表明: 10 8 8

这表明我的第二个猜测是正确的,我认为只有在修改后才使用时间

我想我只是糊涂了。 它进入正确的分支只是为了评估最深的嵌套然后当回到更高级别时回到 ltr 并且不在乎它在哪里duh

谢谢

【问题讨论】:

  • 我真的建议你重写这些语句,那些单行语句在可读性方面对任何人都没有好处,这显然是目前的优先事项。

标签: c# math tween operator-precedence


【解决方案1】:

简化示例

回答您的问题“在第二次返回中,是在修改之前(和之后)使用的时间吗?是吗?”

这个怎么样?

public int ReturnZeroIfValueIsNegative(int x)
{
    if ((x += 100) <= 100) // the "+=" operator changed the value of x
    {
        return 0;
    }

    return x; // this will return the altered value of x
}

以上内容可改写为

    x = x + 100;

    if (x <= 100)
    {
        return 0;
    }

    return x;

操作顺序

您可以查看here,了解有关编程语言操作顺序的一般说明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2018-11-05
    • 2016-12-22
    • 2018-08-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多