最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fortran指数运算顺序

SEO心得admin98浏览0评论
本文介绍了Fortran指数运算顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将一些Fortran转换为Javascript,对于特定的方程组,指数的运算顺序对我来说是不透明的.

I'm translating some Fortran into Javascript and the order of operations for exponents is pretty opaque to me for a particular class of equations.

这是Fortran公式的示例:

Here's an example of the Fortran equation:

x = 1+a*b**c*c**d

Fortran中的指数使用**运算符指定.此页面给出了一些提示:

Exponents in Fortran are designated with the ** operator. This page gives some hints:

算术表达式根据以下优先级规则求值:

Arithmetic expressions are evaluated in accordance with the following priority rules:

  • 首先执行所有幂运算;从右到左执行连续的幂运算.
  • 接下来将按照从左到右的顺序显示所有乘法和除法.

所以我觉得我想把它翻译成Python,最终会得到类似的东西:

So it feels to me like to translate this into, say, Python, you'd end up with something like:

x = 1+a*pow(b,c)*pow(c,d)

但是那并没有让我得到我期望的答案,所以我想检查一下这是否看起来很理智(因为即使在最好的情况下,操作顺序也从来都不是我的强项.而不是我不太熟悉的语言).

But that isn't getting me the answers I'd expect it to, so I wanted to check if that seemed sane or not (because order of operations was never a strong suit of mine even in the best of circumstances, certainly not with languages I am not very familiar with).

这是另一个难题:

x = a*(1-(1-a)**b)+(1-a)*(a)**(1/b)

好!这伤了我的头.(将单独的(a)放在括号内重要吗?)至少这个有括号,这表明:

Oy! This hurts my head. (Does putting that lone (a) in parens matter?) At least this one has parens, which suggests:

x = a*(1-pow(1-a,b))+(1-a)*pow(a,1/b)

但是我仍然不确定我是否理解这一点.

But I'm still not sure I understand this.

推荐答案

您可以按照运算符的优先级来查看它. ** 的优先级高于 * 的优先级,高于二进制 + 的优先级.表达式 1 + a * b ** c * c ** d 类似于 1 + a *(b ** c)*(c ** d).

You can look at this in terms of precedence of operators. ** is a higher precedence than *, higher than binary +. The expression 1+a*b**c*c**d is then like 1+a*(b**c)*(c**d).

更多正式...

表达式由包含 primaries 和 level-1 到 level-5 表达式的语法描述.让我们用问题的例子来看这些

Expressions in Fortran are described by a grammar consisting of primaries and level-1 to level-5 expressions. Let's look at these using the example of the question

x = 1+a*b**c*c**d

这是利益表达.由于这是所谓的2级表达方式,因此仅在此之前进行解释.

the right-hand side of this is the expression of interest. As this is a so-called level-2 expression I'll explain only in terms up to that.

此表达式的主变量是常量 1 和变量 a , b , c 和 d .我们还有许多运算符 + , ** 和 * .

The primaries of this expression are the constant 1 and the variables a, b, c and d. We also have a number of operators +, ** and *.

主变量都是1级表达式,由于没有定义的一元运算符,我们将直接转到2级表达式.

The primaries are all level-1 expressions, and as there is no defined unary operator here we'll go straight to the level-2 expressions.

规则定义了2级表达式(引用F2008 7.1.2.4):

A level-2 expression is defined by the rules (quoting F2008 7.1.2.4):

  • mult-operand为level-1 expr [power-op mult-operand]
  • add-operand是[add-operand mult-op] mult-operand
  • level-2-expr是[[level-2-expr] add-op] add-operand

其中 power-op 是 ** , mult-op * (或/)和 add-op + (或-).

where power-op is **, mult-op * (or /) and add-op + (or -).

将分析汇总在一起:

  • 1 + add-operand
  • 1 +(add-operand * mult-operand)
  • 1 +((a *多重操作数)*(多重操作数))
  • 1 +((a *(b ** c))*(c ** d))

最后一点,用括号括起来的表达式是主要的.这样可以确保保留带括号的优先顺序.

As a final note, an expression enclosed in parentheses is a primary. This ensures that the expectation of precedence with parentheses is preserved.

此语法还解释了为什么将 a ** b ** c 评估为 a **(b ** c).

This grammar also explains why a**b**c is evaluated as a**(b**c).

发布评论

评论列表(0)

  1. 暂无评论