左到右结合性是什么意思? [英] What does left-to-right associativity mean?

查看:303
本文介绍了左到右结合性是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对左到右和从右到左的结合性的定义感到困惑。我也看到他们被称为左结合性和右结合性,并想知道哪个对应于哪个。

I am confused about the definition of left-to-right and right-to-left associativity. I have also seen them called left associativity and right associativity and would like to know which corresponds to which.

我知道它涉及到具有相同优先级的操作的顺序如同a = x * y * z是指a = x *(y * z)还是a =(x * y)* z。我不知道哪一个是从左到右的关联,哪个是从右到左的关联。

I know that it relates to the order that operations with the same precedence are preformed, as in whether a = x * y * z means a = x * (y * z) or a = (x * y) * z. I don't know which one is left-to-right associative and which is right-to-left associative.

我试过Googleing,但我所有的找到是不同运算符在c ++中的关联性的表。看看所有的例子,让我更困惑。

I have tried Googleing it but all I have been able to find is tables of what the associativity of different operators are in c++. Looking at all the examples has just made me more confused.

更令我困惑的是:

glm::vec4 transformedVector = translationMatrix * rotationMatrix * scaleMatrix * originalVector;

首先执行缩放矩阵乘法,然后是旋转矩阵,接着是平移。在这个例子中,矩阵都是glm :: mat4类型,向量是glm :: vec4类型。这是从左到右还是从右到左的结合性?这和正常的乘法是一样的还是与glm类型不同的乘法?

preforms the scaling matrix multiplication first, followed by the rotation matrix followed by the translation. In this example the matrices are all of type glm::mat4 and the vectors are of type glm::vec4. Is this left-to-right or right-to-left associativity? Is this the same as normal multiplication or is multiplication of glm types different?

推荐答案

通常从左到右阅读。你通常做数学从左到右。

You normally read left to right. You normally do math left to right. This is left to right associativity and it is most common.

大多数人会解决

x = 23 + 34 + 45



<
$ b

by grouping it

x = (23 + 34) + 45


b $ b

这是从左到右的结合性。

this is left-to-right associativity. You can remember it because you read and do math left to right.

在数学中添加 没有什么太大的意义。你总是得到相同的结果。这是因为添加是关联。说操作是关联意味着从左到右和从右到左的关联是相同的事情。对于在编程中添加,它仍然是重要的,因为溢出和浮点算术(但不会对任何合理的语言中的正常大小的整数),所以当你有一个2 AM的bug,大数字和flippant使用 a + b b + a ,记住添加发生的顺序。

For addition in mathematics it does't matter too much. You always get the same result either way. This is because addition is associative. To say an operation is associative means left to right and right to left association are the same thing. For addition in programming it still matters because of overflows and floating point arithmetic (but won't for normal-sized integers in any reasonable language), so when you have a 2 AM bug with large numbers and flippant use of a+b and b+a, remember what order the addition happened in.

在您的示例中:

glm::vec4 transformedVector = translationMatrix * rotationMatrix * scaleMatrix * originalVector

从概念上来说,在那里你所做的事情是。然而,在C ++中, * 通常是从左到右的关联,并且不可能覆盖此。 glm可以以多种方式处理这个问题:它可以建立一个事物的高速缓存以乘法等待最终向量到达然后做右到左乘法。它也可以(更可能)使用代数的定理,矩阵乘法是完全关联的,并且只是从左到右乘以,然后确保读者在文档中它是相同的,认为它是从右到左。但是,您需要了解实现,因为如前所述,实现选择将浮点乘数字在一起

You conceptually chomp through from the right-side first, since that is where the thing you are acting on is. However in C++, * is normally left-to-right associative and it is not possible to override this. glm may handle this in a number of ways: it may build up a cache of things to multiply waiting for the final vector to arrive then do right to left multiplication. It may also (more likely) use the theorem of algebra that matrix multiplication is fully associative, and just multiply out left-to-right, then assure the reader in the documentation that it's the same as thinking of it as right to left. However, you need to understand the implementation because as previously discussed it matters which way the implementation chooses to multiplying floating point numbers together.

为了完整性,请考虑减法。什么是 a - b - c ?这里真的是重要的,无论是左还是右关联。当然,在数学中我们将它定义为b (a - b) - c ,但是一些奇怪的编程语言可能喜欢减法是正确的关联,并且取 a - b - c 始终表示 a - (b - c)。这个外来语言更好地有一个文档页面指定 - 是右关联,因为它是操作规范的一部分,而不是你可以告诉简单地从操作员的使用。

For completeness, consider subtraction. What is a - b - c? Here it really does matter whether it is left or right associative. Of course in math we define it to b (a - b) - c, but some strange programming language might prefer subtraction to be right associative, and take a - b - c to always mean a - (b - c). This alien language had better have a documentation page specifying that - is right-associative, because it is part of the operation specification, not something you can tell simply from looking at the operator's use.

这篇关于左到右结合性是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆