f()+ g()* h() [英] f() + g() * h()

查看:80
本文介绍了f()+ g()* h()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


http://c-faq.com/ expr / precvsooe.html


回答comp.lang.c常见问题列表·问题3.4


出现以下内容:

运算符优先级和显式括号在表达式求值时仅对部分

进行排序。在表达式中


f()+ g()* h()


虽然我们知道乘法将在

另外,没有人知道这三个函数中的哪一个将首先调用
。换句话说,优先级仅部分指定

评估顺序,其中部分'''强调不包括

操作数评估。


我的问题:

只有在同一个优先级组内,才会指定

操作数的评估顺序。这里乘法具有更高的优先级

并且加法具有更低的优先级。

所以不应该在f()之前调用函数g()和h()来执行

首先乘法?


我哪里错了?


谢谢

In
http://c-faq.com/expr/precvsooe.html

in the answer to "comp.lang.c FAQ list · Question 3.4"

The following appears:
Operator precedence and explicit parentheses impose only a partial
ordering on the evaluation of an expression. In the expression

f() + g() * h()

although we know that the multiplication will happen before the
addition, there is no telling which of the three functions will be
called first. In other words, precedence only partially specifies
order of evaluation, where ``partially'''' emphatically does not cover
evaluation of operands.

MY QUESTION:
Only within the same precedence group, the order of evaluation of
operands is not specified. Here multiplication has higher precedence
and addition has lower precedence.
So shouldn''t the functions g() and h() be called before f() to do the
multiplication first ?

Where am I going wrong ?

Thanks

推荐答案

su **** **********@yahoo.com ,印度说:
su**************@yahoo.com, India said:


http://c-faq.com/expr/precvsooe.html


回答comp.lang.c FAQ列表·问题3.4


出现以下内容:

运算符优先级并且显式括号仅对表达式的求值强制部分

排序。在表达式中


f()+ g()* h()


虽然我们知道乘法将在

另外,
In
http://c-faq.com/expr/precvsooe.html

in the answer to "comp.lang.c FAQ list · Question 3.4"

The following appears:
Operator precedence and explicit parentheses impose only a partial
ordering on the evaluation of an expression. In the expression

f() + g() * h()

although we know that the multiplication will happen before the
addition,



等一下。我们*实际*知道的只是没有操作员可以做其工作直到其操作数的值已知。

Wait a minute. What we *actually* know is only that no operator can do
its work until the values of its operands are known.


我的问题:

仅在同一优先级组内,未指定

操作数的评估顺序。这里乘法具有更高的优先级

并且加法具有更低的优先级。
MY QUESTION:
Only within the same precedence group, the order of evaluation of
operands is not specified. Here multiplication has higher precedence
and addition has lower precedence.



优先顺序*不*确定评估顺序。它确定哪个操作符属于哪个操作符。

Precedence does *not* determine order of evaluation. It determines which
operands belong to which operators.


所以不应该调用函数g()和h()之前f()首先做

乘法?


我哪里错了?
So shouldn''t the functions g() and h() be called before f() to do the
multiplication first ?

Where am I going wrong ?



考虑以下代码,计算f()* g()+ h():


t1 = f ();

t2 = g();

t3 = h();


result = t1 * t2 * t3 ;


好​​的,这个怎么样?


t3 = h();

t2 = g() ;

t1 = f();


结果= t1 * t2 * t3;

或者,当然,任何其他四种组合。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Consider the following code, which calculates f() * g() + h():

t1 = f();
t2 = g();
t3 = h();

result = t1 * t2 * t3;

Okay, how about this?

t3 = h();
t2 = g();
t1 = f();

result = t1 * t2 * t3;
Or, of course, any of four other combinations.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


su ************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:


http:// c-faq.com/expr/precvsooe.html


回答comp.lang.c FAQ列表?·问题3.4


出现以下内容:

运算符优先级和显式括号在表达式求值时仅对部分

进行排序。在表达式中


f()+ g()* h()


虽然我们知道乘法将在

另外,没有人知道这三个函数中的哪一个将首先调用
。换句话说,优先级仅部分指定

评估顺序,其中部分'''强调不包括

操作数评估。


我的问题:

只有在同一个优先级组内,才会指定

操作数的评估顺序。这里乘法具有更高的优先级

并且加法具有更低的优先级。

所以不应该在f()之前调用函数g()和h()来执行

乘法优先?
In
http://c-faq.com/expr/precvsooe.html

in the answer to "comp.lang.c FAQ list ?· Question 3.4"

The following appears:
Operator precedence and explicit parentheses impose only a partial
ordering on the evaluation of an expression. In the expression

f() + g() * h()

although we know that the multiplication will happen before the
addition, there is no telling which of the three functions will be
called first. In other words, precedence only partially specifies
order of evaluation, where ``partially'''' emphatically does not cover
evaluation of operands.

MY QUESTION:
Only within the same precedence group, the order of evaluation of
operands is not specified. Here multiplication has higher precedence
and addition has lower precedence.
So shouldn''t the functions g() and h() be called before f() to do the
multiplication first ?



No.没有什么能阻止`f`先被调用。事实上,在这个表达式的无聊和明显的直接翻译中,首先调用`f` / is /




因为明显的翻译产生的代码通常可以通过稍后调用`f`来改善(因此不需要保存它的

结果而`g()* h( )``被评估),C表示订单是

未指定,因此允许编译器有更大的空间来操作[1]。

No. Nothing stops `f` being called first. Indeed, in the boring and
obvious straightforward translation of this expression, `f` /is/
called first.

Because the obvious translation produces code that can often be
bettered by calling `f` later (and hence not having to save its
result while `g() * h()` is evaluated), C says the order is
unspecified and hence allows the compiler more room to manoeuver [1].


我错了吗?
Where am I going wrong ?



你的评价顺序令人困惑。


[1]我的/个人/观点是那么少如果

语言指定了评估订单并且编译器可以重新排序,只有当它可以证明它对
$ b $没有任何影响时,会咬你的公共汽车。结果。但这可以假设编译器具有多少计算功能,以及代码生成效率低于优秀代价的成本是多少,二十年来真的不那么真实。之前

比他们现在。虽然我有这个意见二十年

以前...


-

克里斯像精美的葡萄酒一样老化 Dollin

含义在定义之前。

You''re confusing precedence with order of evaluation.

[1] My /personal/ view is that less things bite your bus if the
language specifies the evaluation order and the compiler can
reorder only if it can prove it makes no difference to the
result. But that makes assumptions about how much computing
power the compiler has, and what the costs of less-than-excellent
code generation are, that were far less true twenty years ago
than they are now. Although I had this opinion twenty years
ago too ...

--
Chris "aging like fine wine" Dollin
Meaning precedes definition.


su ************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:


http://c-faq.com/ expr / precvsooe.html


回答comp.lang.c常见问题列表·问题3.4


出现以下内容:

运算符优先级和显式括号在表达式求值时仅对部分

进行排序。在表达式中


f()+ g()* h()


虽然我们知道乘法将在

另外,没有人知道这三个函数中的哪一个将首先调用
。换句话说,优先级仅部分指定

评估顺序,其中部分'''强调不包括

操作数评估。


我的问题:

只有在同一个优先级组内,才会指定

操作数的评估顺序。这里乘法具有更高的优先级

并且加法具有更低的优先级。

所以不应该在f()之前调用函数g()和h()来执行

首先乘法?


我哪里错了?
In
http://c-faq.com/expr/precvsooe.html

in the answer to "comp.lang.c FAQ list · Question 3.4"

The following appears:
Operator precedence and explicit parentheses impose only a partial
ordering on the evaluation of an expression. In the expression

f() + g() * h()

although we know that the multiplication will happen before the
addition, there is no telling which of the three functions will be
called first. In other words, precedence only partially specifies
order of evaluation, where ``partially'''' emphatically does not cover
evaluation of operands.

MY QUESTION:
Only within the same precedence group, the order of evaluation of
operands is not specified. Here multiplication has higher precedence
and addition has lower precedence.
So shouldn''t the functions g() and h() be called before f() to do the
multiplication first ?

Where am I going wrong ?



见其他人的回复。有一个具体的例子说明为什么

可能是首先调用f()的一个令人信服的理由,让''s's's's $

有点事情:


y = 0;

for(i = 0; i< 1000000; ++ i)

y + = sqrt(2)+ g( i)* h(i);


这里,匿名f()已被更多的东西取代

可识别。但是看看:编译器知道(或者可以知道)

sqrt()是一个纯粹的函数,没有副作用的函数和
总是为相同的参数返回相同的值。因此,如果编译器将其重写为

y = 0,则可以加快

代码;

double __temp == sqrt( 2);

for(i = 0; i< 1000000; ++ i)

y + = __temp + g(i)* h(i);


通过在g()和h()之前调用f() - 即sqrt(),

编译器已经消除了近百万个函数调用。

你希望你的编译器为你做什么,嗯?


一个足够聪明的编译器甚至可以计算sqrt(2)
编译时
,因此呼叫程序之前的f()甚至

开始运行。一个非常智能的编译器甚至可以重写

代码为


y = 1.4142135623730950488016887242097e6;

for(i = 0; i< ; 1000000; ++ i)

y + = g(i)* h(i);


....虽然这种转变有点可疑因为浮点算术的近似性质。


无论如何,这个插图的重点是要显示那里

是你*希望编译器有一个很好的自由来重新安排评估顺序的情况。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


这篇关于f()+ g()* h()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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